summaryrefslogtreecommitdiffstats
path: root/private/ntos/cntfs/cleanup.c
blob: 9cf876c7d0177a77b9e7b1b285c7888621105eb2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
/*++

Copyright (c) 1991  Microsoft Corporation

Module Name:

    Cleanup.c

Abstract:

    This module implements the File Cleanup routine for Ntfs called by the
    dispatch driver.

Author:

    Your Name       [Email]         dd-Mon-Year

Revision History:

--*/

#include "NtfsProc.h"

//
//  The Bug check file id for this module
//

#define BugCheckFileId                   (NTFS_BUG_CHECK_CLEANUP)

//
//  The local debug trace level
//

#define Dbg                              (DEBUG_TRACE_CLEANUP)

VOID
NtfsContractQuotaToFileSize (
    IN PIRP_CONTEXT IrpContext,
    IN PSCB Scb
    );

#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, NtfsCommonCleanup)
#pragma alloc_text(PAGE, NtfsFsdCleanup)
#pragma alloc_text(PAGE, NtfsContractQuotaToFileSize)
#endif


NTSTATUS
NtfsFsdCleanup (
    IN PVOLUME_DEVICE_OBJECT VolumeDeviceObject,
    IN PIRP Irp
    )

/*++

Routine Description:

    This routine implements the FSD part of Cleanup.

Arguments:

    VolumeDeviceObject - Supplies the volume device object where the
        file exists

    Irp - Supplies the Irp being processed

Return Value:

    NTSTATUS - The FSD status for the IRP

--*/

{
    TOP_LEVEL_CONTEXT TopLevelContext;
    PTOP_LEVEL_CONTEXT ThreadTopLevelContext;

    NTSTATUS Status = STATUS_SUCCESS;
    PIRP_CONTEXT IrpContext = NULL;
    ULONG LogFileFullCount = 0;

    ASSERT_IRP( Irp );

    PAGED_CODE();

    //
    //  If we were called with our file system device object instead of a
    //  volume device object, just complete this request with STATUS_SUCCESS
    //

    if (VolumeDeviceObject->DeviceObject.Size == (USHORT)sizeof(DEVICE_OBJECT)) {

        Irp->IoStatus.Status = STATUS_SUCCESS;
        Irp->IoStatus.Information = FILE_OPENED;

        IoCompleteRequest( Irp, IO_DISK_INCREMENT );

        return STATUS_SUCCESS;
    }

    DebugTrace( +1, Dbg, ("NtfsFsdCleanup\n") );

    //
    //  Call the common Cleanup routine
    //

    FsRtlEnterFileSystem();

    ThreadTopLevelContext = NtfsSetTopLevelIrp( &TopLevelContext, FALSE, FALSE );

    //
    //  Do the following in a loop to catch the log file full and cant wait
    //  calls.
    //

    do {

        try {

            //
            //  We are either initiating this request or retrying it.
            //

            if (IrpContext == NULL) {

                IrpContext = NtfsCreateIrpContext( Irp, CanFsdWait( Irp ) );
                NtfsUpdateIrpContextWithTopLevel( IrpContext, ThreadTopLevelContext );

            } else if (Status == STATUS_LOG_FILE_FULL) {

                NtfsCheckpointForLogFileFull( IrpContext );

                if (++LogFileFullCount >= 2) {

                    SetFlag( IrpContext->Flags, IRP_CONTEXT_FLAG_EXCESS_LOG_FULL );
                }
            }

            Status = NtfsCommonCleanup( IrpContext, Irp );
            break;

        } except(NtfsExceptionFilter( IrpContext, GetExceptionInformation() )) {

            //
            //  We had some trouble trying to perform the requested
            //  operation, so we'll abort the I/O request with
            //  the error status that we get back from the
            //  execption code
            //

            Status = NtfsProcessException( IrpContext, Irp, GetExceptionCode() );
        }

    } while (Status == STATUS_CANT_WAIT ||
             Status == STATUS_LOG_FILE_FULL);

    if (ThreadTopLevelContext == &TopLevelContext) {
        NtfsRestoreTopLevelIrp( ThreadTopLevelContext );
    }

    FsRtlExitFileSystem();

    //
    //  And return to our caller
    //

    DebugTrace( -1, Dbg, ("NtfsFsdCleanup -> %08lx\n", Status) );

    return Status;
}


NTSTATUS
NtfsCommonCleanup (
    IN PIRP_CONTEXT IrpContext,
    IN PIRP Irp
    )

/*++

Routine Description:

    This is the common routine for Cleanup called by both the fsd and fsp
    threads.

Arguments:

    Irp - Supplies the Irp to process

Return Value:

    NTSTATUS - The return status for the operation

--*/

{
    NTSTATUS Status;
    PIO_STACK_LOCATION IrpSp;
    PFILE_OBJECT FileObject;

    TYPE_OF_OPEN TypeOfOpen;
    PVCB Vcb;
    PFCB Fcb;
    PSCB Scb;
    PCCB Ccb;
    PLCB Lcb;
    PLCB LcbForUpdate;
    PLCB LcbForCounts;
    PSCB ParentScb = NULL;
    PFCB ParentFcb = NULL;

    PLCB ThisLcb;
    PSCB ThisScb;
    ATTRIBUTE_ENUMERATION_CONTEXT AttrContext;

    PLONGLONG TruncateSize = NULL;
    LONGLONG LocalTruncateSize;

    BOOLEAN DeleteFile = FALSE;
    BOOLEAN DeleteStream = FALSE;
    BOOLEAN OpenById;
    BOOLEAN RemoveLink;

    BOOLEAN AcquiredParentScb = FALSE;
    BOOLEAN AcquiredScb = FALSE;

    BOOLEAN CleanupAttrContext = FALSE;

    BOOLEAN UpdateDuplicateInfo = FALSE;
    BOOLEAN AddToDelayQueue = TRUE;

    USHORT TotalLinkAdj = 0;
    PLIST_ENTRY Links;

    NAME_PAIR NamePair;

    ASSERT_IRP_CONTEXT( IrpContext );
    ASSERT_IRP( Irp );

    PAGED_CODE();

    NtfsInitializeNamePair(&NamePair);

    //
    //  Get the current Irp stack location
    //

    IrpSp = IoGetCurrentIrpStackLocation( Irp );

    DebugTrace( +1, Dbg, ("NtfsCommonCleanup\n") );
    DebugTrace( 0, Dbg, ("IrpContext = %08lx\n", IrpContext) );
    DebugTrace( 0, Dbg, ("Irp        = %08lx\n", Irp) );

    //
    //  Extract and decode the file object
    //

    FileObject = IrpSp->FileObject;

    TypeOfOpen = NtfsDecodeFileObject( IrpContext, FileObject, &Vcb, &Fcb, &Scb, &Ccb, FALSE );

    Status = STATUS_SUCCESS;

    //
    //  Special case the unopened file object and stream files.
    //

    if ((TypeOfOpen == UnopenedFileObject) ||
        (TypeOfOpen == StreamFileOpen)) {

        //
        //  Just set the FO_CLEANUP_COMPLETE flag, and get outsky...
        //

        SetFlag( FileObject->Flags, FO_CLEANUP_COMPLETE );

        //
        //  Theoretically we should never hit this case.  It means an app
        //  tried to close a handle he didn't open (call NtClose with a handle
        //  value that happens to be in the handle table).  It is safe to
        //  simply return SUCCESS in this case.
        //
        //  Trigger an assert so we can find the bad app though.
        //

        ASSERT( TypeOfOpen != StreamFileOpen );

        NtfsCompleteRequest( &IrpContext, &Irp, Status );

        DebugTrace( -1, Dbg, ("NtfsCommonCleanup -> %08lx\n", Status) );

        return Status;
    }

    //
    //  Let's make sure we can wait.
    //

    if (!FlagOn(IrpContext->Flags, IRP_CONTEXT_FLAG_WAIT)) {

        Status = NtfsPostRequest( IrpContext, Irp );

        DebugTrace( -1, Dbg, ("NtfsCommonCleanup -> %08lx\n", Status) );

        return Status;
    }

    //
    //  Remember if this is an open by file Id open.
    //

    OpenById = BooleanFlagOn( Ccb->Flags, CCB_FLAG_OPEN_BY_FILE_ID );

    //
    //  Acquire exclusive access to the Vcb and enqueue the irp if we didn't
    //  get access
    //

    if (TypeOfOpen == UserVolumeOpen) {

        SetFlag( IrpContext->Flags, IRP_CONTEXT_FLAG_ACQUIRE_VCB_EX );
    }

    if (FlagOn( IrpContext->Flags, IRP_CONTEXT_FLAG_ACQUIRE_VCB_EX )) {

        NtfsAcquireExclusiveVcb( IrpContext, Vcb, TRUE );

    } else {

        NtfsAcquireSharedVcb( IrpContext, Vcb, TRUE );
    }

    //
    //  Use a try-finally to facilitate cleanup.
    //

    try {

        LcbForUpdate = LcbForCounts = Lcb = Ccb->Lcb;

        if (Lcb != NULL) {

            ParentScb = Lcb->Scb;

            if (ParentScb != NULL) {

                ParentFcb = ParentScb->Fcb;
            }
        }

        //
        //  Acquire Paging I/O first, since we may be deleting or truncating.
        //  Testing for the PagingIoResource is not really safe without
        //  holding the main resource, so we correct for that below.
        //

        if (Fcb->PagingIoResource != NULL) {

            NtfsAcquireExclusivePagingIo( IrpContext, Fcb );
            NtfsAcquireExclusiveScb( IrpContext, Scb );

        } else {

            NtfsAcquireExclusiveScb( IrpContext, Scb );

            //
            //  If we now do not see a paging I/O resource we are golden,
            //  othewise we can absolutely release and acquire the resources
            //  safely in the right order, since a resource in the Fcb is
            //  not going to go away.
            //

            if (Fcb->PagingIoResource != NULL) {
                NtfsReleaseScb( IrpContext, Scb );
                NtfsAcquireExclusivePagingIo( IrpContext, Fcb );
                NtfsAcquireExclusiveScb( IrpContext, Scb );
            }
        }

        AcquiredScb = TRUE;

        //
        //  Update the Lcb/Scb to reflect the case where this opener had
        //  specified delete on close.
        //

        if (FlagOn( Ccb->Flags, CCB_FLAG_DELETE_ON_CLOSE )) {

            if (FlagOn( Ccb->Flags, CCB_FLAG_OPEN_AS_FILE )) {

                BOOLEAN LastLink;
                BOOLEAN NonEmptyIndex;

                //
                //  It is ok to get rid of this guy.  All we need to do is
                //  mark this Lcb for delete and decrement the link count
                //  in the Fcb.  If this is a primary link, then we
                //  indicate that the primary link has been deleted.
                //

                if (!LcbLinkIsDeleted( Lcb ) &&
                    (!IsDirectory( &Fcb->Info ) ||
                    NtfsIsLinkDeleteable( IrpContext, Fcb, &NonEmptyIndex, &LastLink))) {

                    if (FlagOn( Lcb->FileNameAttr->Flags, FILE_NAME_DOS | FILE_NAME_NTFS )) {

                        SetFlag( Fcb->FcbState, FCB_STATE_PRIMARY_LINK_DELETED );
                    }

                    Fcb->LinkCount -= 1;

                    SetFlag( Lcb->LcbState, LCB_STATE_DELETE_ON_CLOSE );

                    //
                    //  Call into the notify package to close any handles on
                    //  a directory being deleted.
                    //

                    if (IsDirectory( &Fcb->Info )) {

                        FsRtlNotifyFullChangeDirectory( Vcb->NotifySync,
                                                        &Vcb->DirNotifyList,
                                                        FileObject->FsContext,
                                                        NULL,
                                                        FALSE,
                                                        FALSE,
                                                        0,
                                                        NULL,
                                                        NULL,
                                                        NULL );
                    }

                }

            //
            //  Otherwise we are simply removing the attribute.
            //

            } else {

                SetFlag( Scb->ScbState, SCB_STATE_DELETE_ON_CLOSE );
            }

            //
            //  Clear the flag so we will ignore it in the log file full case.
            //

            ClearFlag( Ccb->Flags, CCB_FLAG_DELETE_ON_CLOSE );
        }

        //
        //  If we are going to try and delete something, anything, knock the file
        //  size and valid data down to zero.  Then update the snapshot
        //  so that the sizes will be zero even if the operation fails.
        //
        //  If we're deleting the file, go through all of the Scb's.
        //

        if ((Fcb->CleanupCount == 1) &&
            (Fcb->LinkCount == 0)) {

            DeleteFile = TRUE;
            NtfsFreeSnapshotsForFcb( IrpContext, Scb->Fcb );

            for (Links = Fcb->ScbQueue.Flink;
                 Links != &Fcb->ScbQueue;
                 Links = Links->Flink) {

                ThisScb = CONTAINING_RECORD( Links, SCB, FcbLinks );

                //
                //  Set the Scb sizes to zero except for the attribute list.
                //

                if (ThisScb->AttributeTypeCode != $ATTRIBUTE_LIST) {

                    ThisScb->Header.FileSize =
                    ThisScb->Header.ValidDataLength = Li0;
                }

                if (FlagOn( ThisScb->ScbState, SCB_STATE_FILE_SIZE_LOADED )) {

                    NtfsSnapshotScb( IrpContext, ThisScb );
                }
            }

        //
        //  Otherwise we may only be deleting this stream.
        //

        } else if ((Scb->CleanupCount == 1) &&
                   FlagOn( Scb->ScbState, SCB_STATE_DELETE_ON_CLOSE )) {

            DeleteStream = TRUE;
            Scb->Header.FileSize =
            Scb->Header.ValidDataLength = Li0;

            NtfsFreeSnapshotsForFcb( IrpContext, Scb->Fcb );

            if (FlagOn( Scb->ScbState, SCB_STATE_FILE_SIZE_LOADED )) {

                NtfsSnapshotScb( IrpContext, Scb );
            }
        }

        //
        //  Let's do a sanity check.
        //

        ASSERT( Fcb->CleanupCount != 0 );
        ASSERT( Scb->CleanupCount != 0 );

        //
        //  If the cleanup count on the file will go to zero and there is
        //  a large security descriptor and we haven't exceeded the security
        //  creation count for this Fcb then dereference and possibly deallocate
        //  the security descriptor for the Fcb.  This is to prevent us from
        //  holding onto pool while waiting for closes to come in.
        //

        if ((Fcb->CleanupCount == 1) &&
            (Fcb->SharedSecurity != NULL) &&
            (Fcb->CreateSecurityCount < FCB_CREATE_SECURITY_COUNT) &&
            (GetSharedSecurityLength( Fcb->SharedSecurity ) > FCB_LARGE_ACL_SIZE)) {

            NtfsAcquireFcbSecurity( Fcb->Vcb );
            NtfsDereferenceSharedSecurity( Fcb );
            NtfsReleaseFcbSecurity( Fcb->Vcb );
        }

        //
        //  Case on the type of open that we are trying to cleanup.
        //

        switch (TypeOfOpen) {

        case UserVolumeOpen :

            DebugTrace( 0, Dbg, ("Cleanup on user volume\n") );

            //
            //  First set the FO_CLEANUP_COMPLETE flag.
            //

            SetFlag( FileObject->Flags, FO_CLEANUP_COMPLETE );

            //
            //  For a volume open, we check if this open locked the volume.
            //  All the other work is done in common code below.
            //

            if (FlagOn( Vcb->VcbState, VCB_STATE_LOCKED ) &&
                ((Vcb->FileObjectWithVcbLocked == FileObject) ||
                 ((ULONG)Vcb->FileObjectWithVcbLocked == ((ULONG)FileObject)+1))) {

                if ((ULONG)Vcb->FileObjectWithVcbLocked == ((ULONG)FileObject)+1) {

                    NtfsPerformDismountOnVcb( IrpContext, Vcb, TRUE );

                //
                //  Purge the volume for the autocheck case.
                //

                } else if (FlagOn( FileObject->Flags, FO_FILE_MODIFIED )) {

                    //
                    //  Drop the Scb for the volume Dasd around this call.
                    //

                    NtfsReleaseScb( IrpContext, Scb );
                    AcquiredScb = FALSE;

                    NtfsFlushVolume( IrpContext, Vcb, FALSE, TRUE, TRUE, FALSE );

                    NtfsAcquireExclusiveScb( IrpContext, Scb );
                    AcquiredScb = TRUE;

                    //
                    //  If this is not the boot partition then dismount the Vcb.
                    //

                    if ((Vcb->CleanupCount == 1) &&
                        ((Vcb->CloseCount - Vcb->SystemFileCloseCount) == 1)) {

                        NtfsPerformDismountOnVcb( IrpContext, Vcb, TRUE );
                    }
                }

                ClearFlag( Vcb->VcbState, VCB_STATE_LOCKED | VCB_STATE_EXPLICIT_LOCK );
                Vcb->FileObjectWithVcbLocked = NULL;

#ifdef _CAIRO_

                //
                //  If the quota tracking has been requested and the quotas
                //  need to be repaired then try to repair them now.
                //

                if (FlagOn( Vcb->QuotaFlags, QUOTA_FLAG_TRACKING_REQUESTED) &&
                    FlagOn( Vcb->QuotaFlags, QUOTA_FLAG_OUT_OF_DATE |
                                             QUOTA_FLAG_CORRUPT |
                                             QUOTA_FLAG_PENDING_DELETES)) {

                    NtfsPostRepairQuotaIndex( IrpContext, Vcb );
                }

#endif // _CAIRO_

            }

            break;

        case UserDirectoryOpen :

            DebugTrace( 0, Dbg, ("Cleanup on user directory/file\n") );

            NtfsSnapshotScb( IrpContext, Scb );

            //
            //  Capture any changes to the time stamps for this file.
            //

            NtfsUpdateScbFromFileObject( IrpContext, FileObject, Scb, TRUE );

            //
            //  Now set the FO_CLEANUP_COMPLETE flag.
            //

            SetFlag( FileObject->Flags, FO_CLEANUP_COMPLETE );

            //
            //  To perform cleanup on a directory, we first complete any
            //  Irps watching from this directory.  If we are deleting the
            //  file then we remove all prefix entries for all the Lcb's going
            //  into this directory and delete the file.  We then report to
            //  dir notify that this file is going away.
            //

            //
            //  Complete any Notify Irps on this file handle.
            //

            if (FlagOn( Ccb->Flags, CCB_FLAG_DIR_NOTIFY )) {

                FsRtlNotifyCleanup( Vcb->NotifySync, &Vcb->DirNotifyList, Ccb );
                ClearFlag( Ccb->Flags, CCB_FLAG_DIR_NOTIFY );
                InterlockedDecrement( &Vcb->NotifyCount );
            }

            //
            //  When cleaning up a user directory, we always remove the
            //  share access and modify the file counts.  If the Fcb
            //  has been marked as delete on close and this is the last
            //  open file handle, we remove the file from the Mft and
            //  remove it from it's parent index entry.
            //

            if (FlagOn( Vcb->VcbState, VCB_STATE_VOLUME_MOUNTED ) &&
                (NodeType( Scb ) == NTFS_NTC_SCB_INDEX)) {

                if (DeleteFile) {

                    ASSERT( (Lcb == NULL) ||
                            (LcbLinkIsDeleted( Lcb ) && Lcb->CleanupCount == 1 ));

                    //
                    //  If we don't have an Lcb and there is one on the Fcb then
                    //  let's use it.
                    //

                    if ((Lcb == NULL) && !IsListEmpty( &Fcb->LcbQueue )) {

                        Lcb = CONTAINING_RECORD( Fcb->LcbQueue.Flink,
                                                 LCB,
                                                 FcbLinks );

                        ParentScb = Lcb->Scb;
                        if (ParentScb != NULL) {

                            ParentFcb = ParentScb->Fcb;
                        }
                    }

                    //
                    //  Now acquire the Parent Scb exclusive while still holding
                    //  the Vcb, to avoid deadlocks.  The Parent Scb is required
                    //  since we will be deleting index entries in it.
                    //

                    if (ParentScb != NULL) {

                        NtfsAcquireExclusiveScb( IrpContext, ParentScb );
                        AcquiredParentScb = TRUE;
                    }

                    try {

                        NtfsDeleteFile( IrpContext, Fcb, ParentScb, NULL);
                        TotalLinkAdj += 1;

                        //
                        //  Remove all tunneling entries for this directory
                        //

                        FsRtlDeleteKeyFromTunnelCache(&Vcb->Tunnel, *(PULONGLONG)&Fcb->FileReference);

                        if (ParentFcb != NULL) {

                            NtfsUpdateFcb( ParentFcb );
                        }

                    } except( (((Status = GetExceptionCode()) == STATUS_LOG_FILE_FULL) ||
                               (Status == STATUS_CANT_WAIT) ||
                               !FsRtlIsNtstatusExpected( Status ))
                              ? EXCEPTION_CONTINUE_SEARCH
                              : EXCEPTION_EXECUTE_HANDLER ) {

                        NOTHING;
                    }

                    if (!OpenById && (Vcb->NotifyCount != 0)) {

                        NtfsReportDirNotify( IrpContext,
                                             Vcb,
                                             &Ccb->FullFileName,
                                             Ccb->LastFileNameOffset,
                                             NULL,
                                             ((FlagOn( Ccb->Flags, CCB_FLAG_PARENT_HAS_DOS_COMPONENT ) &&
                                               Ccb->Lcb != NULL &&
                                               Ccb->Lcb->Scb->ScbType.Index.NormalizedName.Buffer != NULL) ?
                                              &Ccb->Lcb->Scb->ScbType.Index.NormalizedName :
                                              NULL),
                                             FILE_NOTIFY_CHANGE_DIR_NAME,
                                             FILE_ACTION_REMOVED,
                                             ParentFcb );
                    }

                    SetFlag( Fcb->FcbState, FCB_STATE_FILE_DELETED );

                    //
                    //  We need to mark all of the links on the file as gone.
                    //  If there is a parent Scb then it will be the parent
                    //  for all of the links.
                    //

                    for (Links = Fcb->LcbQueue.Flink;
                         Links != &Fcb->LcbQueue;
                         Links = Links->Flink) {

                        ThisLcb = CONTAINING_RECORD( Links, LCB, FcbLinks );

                        //
                        //  Remove all remaining prefixes on this link.
                        //

                        NtfsRemovePrefix( ThisLcb );

                        SetFlag( ThisLcb->LcbState, LCB_STATE_LINK_IS_GONE );

                        //
                        //  We don't need to report any changes on this link.
                        //

                        ThisLcb->InfoFlags = 0;
                    }

                    //
                    //  We need to mark all of the Scbs as gone.
                    //

                    for (Links = Fcb->ScbQueue.Flink;
                         Links != &Fcb->ScbQueue;
                         Links = Links->Flink) {

                        ThisScb = CONTAINING_RECORD( Links, SCB, FcbLinks );

                        ClearFlag( Scb->ScbState,
                                   SCB_STATE_NOTIFY_ADD_STREAM |
                                   SCB_STATE_NOTIFY_REMOVE_STREAM |
                                   SCB_STATE_NOTIFY_RESIZE_STREAM |
                                   SCB_STATE_NOTIFY_MODIFY_STREAM );

                        if (!FlagOn( ThisScb->ScbState, SCB_STATE_ATTRIBUTE_DELETED )) {

                            NtfsSnapshotScb( IrpContext, ThisScb );

                            ThisScb->ValidDataToDisk =
                            ThisScb->Header.AllocationSize.QuadPart =
                            ThisScb->Header.FileSize.QuadPart =
                            ThisScb->Header.ValidDataLength.QuadPart = 0;

                            SetFlag( ThisScb->ScbState, SCB_STATE_ATTRIBUTE_DELETED );
                        }
                    }

                    //
                    //  We certainly don't need to any on disk update for this
                    //  file now.
                    //

                    Fcb->InfoFlags = 0;
                    ClearFlag( Fcb->FcbState, FCB_STATE_UPDATE_STD_INFO );

                    ClearFlag( Ccb->Flags,
                               CCB_FLAG_USER_SET_LAST_MOD_TIME |
                               CCB_FLAG_USER_SET_LAST_CHANGE_TIME |
                               CCB_FLAG_USER_SET_LAST_ACCESS_TIME );
                    AddToDelayQueue = FALSE;
                }

            } else {

                AddToDelayQueue = FALSE;
            }

            //
            //  Determine if we should put this on the delayed close list.
            //  The following must be true.
            //
            //  - This is not the root directory
            //  - This directory is not about to be deleted
            //  - This is the last handle and last file object for this
            //      directory.
            //  - There are no other file objects on this file.
            //  - We are not currently reducing the delayed close queue.
            //

            NtfsAcquireFsrtlHeader( Scb );
            if (AddToDelayQueue &&
                !FlagOn( Scb->ScbState, SCB_STATE_DELAY_CLOSE ) &&
                (NtfsData.DelayedCloseCount <= NtfsMaxDelayedCloseCount) &&
                (Fcb->CloseCount == 1)) {

                SetFlag( Scb->ScbState, SCB_STATE_DELAY_CLOSE );

            } else {

                ClearFlag( Scb->ScbState, SCB_STATE_DELAY_CLOSE );
            }
            NtfsReleaseFsrtlHeader( Scb );

            break;

        case UserFileOpen :
#ifdef _CAIRO_
        case UserPropertySetOpen :
#endif  //  _CAIRO_

            DebugTrace( 0, Dbg, ("Cleanup on user file\n") );

            //
            //  If the Scb is uninitialized, we read it from the disk.
            //

            if (!FlagOn( Scb->ScbState, SCB_STATE_HEADER_INITIALIZED )) {

                try {

                    NtfsUpdateScbFromAttribute( IrpContext, Scb, NULL );

                } except( (((Status = GetExceptionCode()) == STATUS_LOG_FILE_FULL) ||
                           (Status == STATUS_CANT_WAIT) ||
                           !FsRtlIsNtstatusExpected( Status ))
                          ? EXCEPTION_CONTINUE_SEARCH
                          : EXCEPTION_EXECUTE_HANDLER ) {

                    NOTHING;
                }
            }

            NtfsSnapshotScb( IrpContext, Scb );

            //
            //  Coordinate the cleanup operation with the oplock state.
            //  Cleanup operations can always cleanup immediately.
            //

            FsRtlCheckOplock( &Scb->ScbType.Data.Oplock,
                              Irp,
                              IrpContext,
                              NULL,
                              NULL );

            //
            //  In this case, we have to unlock all the outstanding file
            //  locks, update the time stamps for the file and sizes for
            //  this attribute, and set the archive bit if necessary.
            //

            if (Scb->ScbType.Data.FileLock != NULL) {

                (VOID) FsRtlFastUnlockAll( Scb->ScbType.Data.FileLock,
                                           FileObject,
                                           IoGetRequestorProcess( Irp ),
                                           NULL );
            }

            //
            //  Update the FastIoField.
            //

            NtfsAcquireFsrtlHeader( Scb );
            Scb->Header.IsFastIoPossible = NtfsIsFastIoPossible( Scb );
            NtfsReleaseFsrtlHeader( Scb );

            //
            //  If the Fcb is in valid shape, we check on the cases where we delete
            //  the file or attribute.
            //

            if (FlagOn( Vcb->VcbState, VCB_STATE_VOLUME_MOUNTED )) {

                //
                //  Capture any changes to the time stamps for this file.
                //

                NtfsUpdateScbFromFileObject( IrpContext, FileObject, Scb, TRUE );

                //
                //  Now set the FO_CLEANUP_COMPLETE flag.
                //

                SetFlag( FileObject->Flags, FO_CLEANUP_COMPLETE );

                //
                //  We are checking here for special actions we take when
                //  we have the last user handle on a link and the link has
                //  been marked for delete.  We could either be removing the
                //  file or removing a link.
                //

                if ((Lcb == NULL) || (LcbLinkIsDeleted( Lcb ) && (Lcb->CleanupCount == 1))) {

                    if (DeleteFile) {

                        //
                        //  If we don't have an Lcb and the Fcb has some entries then
                        //  grab one of these to do the update.
                        //

                        if (Lcb == NULL) {

                            for (Links = Fcb->LcbQueue.Flink;
                                 Links != &Fcb->LcbQueue;
                                 Links = Links->Flink) {

                                ThisLcb = CONTAINING_RECORD( Fcb->LcbQueue.Flink,
                                                             LCB,
                                                             FcbLinks );

                                if (!FlagOn( ThisLcb->LcbState, LCB_STATE_LINK_IS_GONE )) {

                                    Lcb = ThisLcb;

                                    ParentScb = Lcb->Scb;
                                    if (ParentScb != NULL) {

                                        ParentFcb = ParentScb->Fcb;
                                    }

                                    break;
                                }
                            }
                        }

                        //  Now acquire the Parent Scb exclusive while still holding
                        //  the Vcb, to avoid deadlocks.  The Parent Scb is required
                        //  since we will be deleting index entries in it.
                        //

                        if (ParentScb != NULL) {

                            NtfsAcquireExclusiveScb( IrpContext, ParentScb );
                            AcquiredParentScb = TRUE;
                        }

                        try {

                            AddToDelayQueue = FALSE;
                            NtfsDeleteFile( IrpContext, Fcb, ParentScb, &NamePair );
                            TotalLinkAdj += 1;

                            //
                            //  Stash property information in the tunnel if the object was
                            //  opened by name, has a parent directory caller was treating it
                            //  as a non-POSIX object and we had an good, active link
                            //

                            if (!OpenById &&
                                ParentScb &&
                                Ccb->Lcb &&
                                !FlagOn(FileObject->Flags, FO_OPENED_CASE_SENSITIVE)) {

                                FsRtlAddToTunnelCache(  &Vcb->Tunnel,
                                                        *(PULONGLONG)&ParentScb->Fcb->FileReference,
                                                        &NamePair.Short,
                                                        &NamePair.Long,
                                                        BooleanFlagOn(Ccb->Lcb->FileNameAttr->Flags, FILE_NAME_DOS),
                                                        sizeof(LONGLONG),
                                                        &Fcb->Info.CreationTime);
                            }

                            if (ParentFcb != NULL) {

                                NtfsUpdateFcb( ParentFcb );
                            }

                        } except( (((Status = GetExceptionCode()) == STATUS_LOG_FILE_FULL) ||
                                   (Status == STATUS_CANT_WAIT) ||
                                   !FsRtlIsNtstatusExpected( Status ))
                                  ? EXCEPTION_CONTINUE_SEARCH
                                  : EXCEPTION_EXECUTE_HANDLER ) {

                            NOTHING;
                        }

                        if (!OpenById && (Vcb->NotifyCount != 0)) {

                            NtfsReportDirNotify( IrpContext,
                                                 Vcb,
                                                 &Ccb->FullFileName,
                                                 Ccb->LastFileNameOffset,
                                                 NULL,
                                                 ((FlagOn( Ccb->Flags, CCB_FLAG_PARENT_HAS_DOS_COMPONENT ) &&
                                                   Ccb->Lcb != NULL &&
                                                   Ccb->Lcb->Scb->ScbType.Index.NormalizedName.Buffer != NULL) ?
                                                  &Ccb->Lcb->Scb->ScbType.Index.NormalizedName :
                                                  NULL),
                                                 FILE_NOTIFY_CHANGE_FILE_NAME,
                                                 FILE_ACTION_REMOVED,
                                                 ParentFcb );
                        }

                        SetFlag( Fcb->FcbState, FCB_STATE_FILE_DELETED );

                        //
                        //  We need to mark all of the links on the file as gone.
                        //

                        for (Links = Fcb->LcbQueue.Flink;
                             Links != &Fcb->LcbQueue;
                             Links = Links->Flink) {

                            ThisLcb = CONTAINING_RECORD( Links, LCB, FcbLinks );

                            if (ThisLcb->Scb == ParentScb) {

                                //
                                //  Remove all remaining prefixes on this link.
                                //

                                NtfsRemovePrefix( ThisLcb );
                                SetFlag( ThisLcb->LcbState, LCB_STATE_LINK_IS_GONE );

                                //
                                //  We don't need to report any changes on this link.
                                //

                                ThisLcb->InfoFlags = 0;
                            }
                        }

                        //
                        //  We need to mark all of the Scbs as gone.
                        //

                        for (Links = Fcb->ScbQueue.Flink;
                             Links != &Fcb->ScbQueue;
                             Links = Links->Flink) {

                            ThisScb = CONTAINING_RECORD( Links, SCB, FcbLinks );

                            ClearFlag( Scb->ScbState,
                                       SCB_STATE_NOTIFY_ADD_STREAM |
                                       SCB_STATE_NOTIFY_REMOVE_STREAM |
                                       SCB_STATE_NOTIFY_RESIZE_STREAM |
                                       SCB_STATE_NOTIFY_MODIFY_STREAM );

                            if (!FlagOn( ThisScb->ScbState, SCB_STATE_ATTRIBUTE_DELETED )) {

                                NtfsSnapshotScb( IrpContext, ThisScb );

                                ThisScb->ValidDataToDisk =
                                ThisScb->Header.AllocationSize.QuadPart =
                                ThisScb->Header.FileSize.QuadPart =
                                ThisScb->Header.ValidDataLength.QuadPart = 0;

                                SetFlag( ThisScb->ScbState, SCB_STATE_ATTRIBUTE_DELETED );
                            }
                        }

                        //
                        //  We certainly don't need to any on disk update for this
                        //  file now.
                        //

                        Fcb->InfoFlags = 0;
                        ClearFlag( Fcb->FcbState, FCB_STATE_UPDATE_STD_INFO );

                        ClearFlag( Ccb->Flags,
                                   CCB_FLAG_USER_SET_LAST_MOD_TIME |
                                   CCB_FLAG_USER_SET_LAST_CHANGE_TIME |
                                   CCB_FLAG_USER_SET_LAST_ACCESS_TIME );

                        //
                        //  We will truncate the attribute to size 0.
                        //

                        TruncateSize = (PLONGLONG)&Li0;

                    //
                    //  Now we want to check for the last user's handle on a
                    //  link (or the last handle on a Ntfs/8.3 pair).  In this
                    //  case we want to remove the links from the disk.
                    //

                    } else if (Lcb != NULL) {

                        ThisLcb = NULL;
                        RemoveLink = TRUE;

                        if (FlagOn( Lcb->FileNameAttr->Flags, FILE_NAME_DOS | FILE_NAME_NTFS ) &&
                            (Lcb->FileNameAttr->Flags != (FILE_NAME_NTFS | FILE_NAME_DOS))) {

                            //
                            //  Walk through all the links looking for a link
                            //  with a flag set which is not the same as the
                            //  link we already have.
                            //

                            for (Links = Fcb->LcbQueue.Flink;
                                 Links != &Fcb->LcbQueue;
                                 Links = Links->Flink) {

                                ThisLcb = CONTAINING_RECORD( Links, LCB, FcbLinks );

                                //
                                //  If this has a flag set and is not the Lcb
                                //  for this cleanup, then we check if there
                                //  are no Ccb's left for this.
                                //

                                if (FlagOn( ThisLcb->FileNameAttr->Flags, FILE_NAME_DOS | FILE_NAME_NTFS )

                                            &&

                                    (ThisLcb != Lcb)) {

                                    if (ThisLcb->CleanupCount != 0) {

                                         RemoveLink = FALSE;
                                    }

                                    break;
                                }

                                ThisLcb = NULL;
                            }
                        }

                        //
                        //  If we are to remove the link, we do so now.  This removes
                        //  the filename attributes and the entries in the parent
                        //  indexes for this link.  In addition, we mark the links
                        //  as having been removed and decrement the number of links
                        //  left on the file.
                        //

                        if (RemoveLink) {

                            NtfsAcquireExclusiveScb( IrpContext, ParentScb );
                            AcquiredParentScb = TRUE;

                            try {

                                AddToDelayQueue = FALSE;
                                NtfsRemoveLink( IrpContext,
                                                Fcb,
                                                ParentScb,
                                                Lcb->ExactCaseLink.LinkName,
                                                &NamePair );

                                //
                                //  Stash property information in the tunnel if caller opened the
                                //  object by name and was treating it as a non-POSIX object
                                //

                                if (!OpenById && !FlagOn(FileObject->Flags, FO_OPENED_CASE_SENSITIVE)) {

                                    FsRtlAddToTunnelCache(  &Vcb->Tunnel,
                                                            *(PULONGLONG)&ParentScb->Fcb->FileReference,
                                                            &NamePair.Short,
                                                            &NamePair.Long,
                                                            BooleanFlagOn(Lcb->FileNameAttr->Flags, FILE_NAME_DOS),
                                                            sizeof(LONGLONG),
                                                            &Fcb->Info.CreationTime);
                                }

                                TotalLinkAdj += 1;
                                NtfsUpdateFcb( ParentFcb );

                            } except( (((Status = GetExceptionCode()) == STATUS_LOG_FILE_FULL) ||
                                       (Status == STATUS_CANT_WAIT) ||
                                       !FsRtlIsNtstatusExpected( Status ))
                                      ? EXCEPTION_CONTINUE_SEARCH
                                      : EXCEPTION_EXECUTE_HANDLER ) {

                                NOTHING;
                            }

                            if (!OpenById && (Vcb->NotifyCount != 0)) {

                                NtfsReportDirNotify( IrpContext,
                                                     Vcb,
                                                     &Ccb->FullFileName,
                                                     Ccb->LastFileNameOffset,
                                                     NULL,
                                                     ((FlagOn( Ccb->Flags, CCB_FLAG_PARENT_HAS_DOS_COMPONENT ) &&
                                                       Ccb->Lcb != NULL &&
                                                       Ccb->Lcb->Scb->ScbType.Index.NormalizedName.Buffer != NULL) ?
                                                      &Ccb->Lcb->Scb->ScbType.Index.NormalizedName :
                                                      NULL),
                                                     FILE_NOTIFY_CHANGE_FILE_NAME,
                                                     FILE_ACTION_REMOVED,
                                                     ParentFcb );
                            }

                            //
                            //  Remove all remaining prefixes on this link.
                            //

                            NtfsRemovePrefix( Lcb );

                            //
                            //  Mark the links as being removed.
                            //

                            SetFlag( Lcb->LcbState, LCB_STATE_LINK_IS_GONE );

                            if (ThisLcb != NULL) {

                                //
                                //  Remove all remaining prefixes on this link.
                                //

                                NtfsRemovePrefix( ThisLcb );
                                SetFlag( ThisLcb->LcbState, LCB_STATE_LINK_IS_GONE );
                                ThisLcb->InfoFlags = 0;
                            }

                            //
                            //  Since the link is gone we don't want to update the
                            //  duplicate information for this link.
                            //

                            Lcb->InfoFlags = 0;
                            LcbForUpdate = NULL;

                            //
                            //  Update the time stamps for removing the link.  Clear the
                            //  FO_CLEANUP_COMPLETE flag around this call so the time
                            //  stamp change is not nooped.
                            //

                            SetFlag( Ccb->Flags, CCB_FLAG_UPDATE_LAST_CHANGE );
                            ClearFlag( FileObject->Flags, FO_CLEANUP_COMPLETE );
                            NtfsUpdateScbFromFileObject( IrpContext, FileObject, Scb, TRUE );
                            SetFlag( FileObject->Flags, FO_CLEANUP_COMPLETE );
                        }
                    }
                }

                //
                //  If the file/attribute is not going away, we update the
                //  attribute size now rather than waiting for the Lazy
                //  Writer to catch up.  If the cleanup count isn't 1 then
                //  defer the following actions.
                //

                if ((Scb->CleanupCount == 1) && (Fcb->LinkCount != 0)) {

                    //
                    //  We may also have to delete this attribute only.
                    //

                    if (DeleteStream) {

                        ClearFlag( Scb->ScbState, SCB_STATE_DELETE_ON_CLOSE );

                        try {

                            //
                            //  Delete the attribute only.
                            //

                            if (CleanupAttrContext) {

                                NtfsCleanupAttributeContext( &AttrContext );
                            }

                            NtfsInitializeAttributeContext( &AttrContext );
                            CleanupAttrContext = TRUE;

                            NtfsLookupAttributeForScb( IrpContext, Scb, NULL, &AttrContext );

                            do {

                                NtfsDeleteAttributeRecord( IrpContext, Fcb, TRUE, FALSE, &AttrContext );

                            } while (NtfsLookupNextAttributeForScb( IrpContext, Scb, &AttrContext ));

                        } except( (((Status = GetExceptionCode()) == STATUS_LOG_FILE_FULL) ||
                                   (Status == STATUS_CANT_WAIT) ||
                                   !FsRtlIsNtstatusExpected( Status ))
                                   ? EXCEPTION_CONTINUE_SEARCH
                                   : EXCEPTION_EXECUTE_HANDLER ) {

                            SetFlag( Scb->ScbState, SCB_STATE_DELETE_ON_CLOSE );
                        }

                        //
                        //  Set the Scb flag to indicate that the attribute is
                        //  gone.
                        //

                        Scb->ValidDataToDisk =
                        Scb->Header.AllocationSize.QuadPart =
                        Scb->Header.FileSize.QuadPart =
                        Scb->Header.ValidDataLength.QuadPart = 0;

                        SetFlag( Scb->ScbState, SCB_STATE_ATTRIBUTE_DELETED );

                        SetFlag( Scb->ScbState, SCB_STATE_NOTIFY_REMOVE_STREAM );

                        ClearFlag( Scb->ScbState,
                                   SCB_STATE_NOTIFY_RESIZE_STREAM |
                                   SCB_STATE_NOTIFY_MODIFY_STREAM |
                                   SCB_STATE_NOTIFY_ADD_STREAM );

                        //
                        //  Update the time stamps for removing the link.  Clear the
                        //  FO_CLEANUP_COMPLETE flag around this call so the time
                        //  stamp change is not nooped.
                        //

                        SetFlag( Ccb->Flags,
                                 CCB_FLAG_UPDATE_LAST_CHANGE | CCB_FLAG_SET_ARCHIVE );
                        ClearFlag( FileObject->Flags, FO_CLEANUP_COMPLETE );
                        NtfsUpdateScbFromFileObject( IrpContext, FileObject, Scb, TRUE );
                        SetFlag( FileObject->Flags, FO_CLEANUP_COMPLETE );

                        TruncateSize = (PLONGLONG)&Li0;

                    //
                    //  Check if we're to modify the allocation size or file size.
                    //

                    } else {

                        if (FlagOn( Scb->ScbState, SCB_STATE_CHECK_ATTRIBUTE_SIZE )) {

                            //
                            //  Acquire the parent now so we enforce our locking
                            //  rules that the Mft Scb must be acquired after
                            //  the normal file resources.
                            //

                            NtfsPrepareForUpdateDuplicate( IrpContext,
                                                           Fcb,
                                                           &LcbForUpdate,
                                                           &ParentScb,
                                                           TRUE );

                            ClearFlag( Scb->ScbState, SCB_STATE_CHECK_ATTRIBUTE_SIZE );

                            //
                            //  For the non-resident streams we will write the file
                            //  size to disk.
                            //


                            if (!FlagOn( Scb->ScbState, SCB_STATE_ATTRIBUTE_RESIDENT )) {

                                //
                                //  Setting AdvanceOnly to FALSE guarantees we will not
                                //  incorrectly advance the valid data size.
                                //

                                try {

                                    NtfsWriteFileSizes( IrpContext,
                                                        Scb,
                                                        &Scb->Header.ValidDataLength.QuadPart,
                                                        FALSE,
                                                        TRUE );

                                } except( (((Status = GetExceptionCode()) == STATUS_LOG_FILE_FULL) ||
                                           (Status == STATUS_CANT_WAIT) ||
                                           !FsRtlIsNtstatusExpected( Status ))
                                           ? EXCEPTION_CONTINUE_SEARCH
                                           : EXCEPTION_EXECUTE_HANDLER ) {

                                    SetFlag( Scb->ScbState, SCB_STATE_CHECK_ATTRIBUTE_SIZE );
                                }

                            //
                            //  For resident streams we will write the correct size to
                            //  the resident attribute.
                            //

                            } else {

                                //
                                //  We need to lookup the attribute and change
                                //  the attribute value.  We can point to
                                //  the attribute itself as the changing
                                //  value.
                                //

                                NtfsInitializeAttributeContext( &AttrContext );
                                CleanupAttrContext = TRUE;

                                try {

                                    NtfsLookupAttributeForScb( IrpContext, Scb, NULL, &AttrContext );

                                    NtfsChangeAttributeValue( IrpContext,
                                                              Fcb,
                                                              Scb->Header.FileSize.LowPart,
                                                              NULL,
                                                              0,
                                                              TRUE,
                                                              TRUE,
                                                              FALSE,
                                                              FALSE,
                                                              &AttrContext );

                                } except( (((Status = GetExceptionCode()) == STATUS_LOG_FILE_FULL) ||
                                           (Status == STATUS_CANT_WAIT) ||
                                           !FsRtlIsNtstatusExpected( Status ))
                                           ? EXCEPTION_CONTINUE_SEARCH
                                           : EXCEPTION_EXECUTE_HANDLER ) {

                                    SetFlag( Scb->ScbState, SCB_STATE_CHECK_ATTRIBUTE_SIZE );
                                }

                                //
                                //  Verify the allocation size is now correct.
                                //

                                if (QuadAlign( Scb->Header.FileSize.LowPart ) != Scb->Header.AllocationSize.LowPart) {

                                    Scb->Header.AllocationSize.LowPart = QuadAlign(Scb->Header.FileSize.LowPart);
                                }
                            }

                            //
                            //  Update the size change to the Fcb.
                            //

                            NtfsUpdateScbFromFileObject( IrpContext, FileObject, Scb, TRUE );
                        }

#ifdef _CAIRO_
                        if (NtfsPerformQuotaOperation( Fcb )) {

                            if ( FlagOn( Scb->ScbState, SCB_STATE_QUOTA_ENLARGED )) {

                                ASSERT( NtfsIsTypeCodeSubjectToQuota( Scb->AttributeTypeCode ));

                                ASSERT( FlagOn( Scb->ScbState, SCB_STATE_SUBJECT_TO_QUOTA ));

                                //
                                //  Acquire the parent now so we enforce our locking
                                //  rules that the Mft Scb must be acquired after
                                //  the normal file resources.
                                //

                                NtfsPrepareForUpdateDuplicate( IrpContext,
                                                               Fcb,
                                                               &LcbForUpdate,
                                                               &ParentScb,
                                                               TRUE );

                                NtfsContractQuotaToFileSize( IrpContext, Scb );
                            }

                            SetFlag( IrpContext->Flags,
                                     IRP_CONTEXT_FLAG_QUOTA_DISABLE );

                        }
#endif // _CAIRO_

                        if (FlagOn( Scb->ScbState, SCB_STATE_TRUNCATE_ON_CLOSE )) {

                            //
                            //  Acquire the parent now so we enforce our locking
                            //  rules that the Mft Scb must be acquired after
                            //  the normal file resources.
                            //
                            NtfsPrepareForUpdateDuplicate( IrpContext,
                                                           Fcb,
                                                           &LcbForUpdate,
                                                           &ParentScb,
                                                           TRUE );

                            ClearFlag( Scb->ScbState, SCB_STATE_TRUNCATE_ON_CLOSE );

                            //
                            //  We have two cases:
                            //
                            //      Resident:  We are looking for the case where the
                            //          valid data length is less than the file size.
                            //          In this case we shrink the attribute.
                            //
                            //      NonResident:  We are looking for unused clusters
                            //          past the end of the file.
                            //
                            //  We skip the following if we had any previous errors.
                            //

                            if (!FlagOn( Scb->ScbState, SCB_STATE_ATTRIBUTE_RESIDENT )) {

                                //
                                //  We don't need to truncate if the file size is 0.
                                //

                                if (Scb->Header.AllocationSize.QuadPart != 0) {

                                    VCN StartingCluster;
                                    VCN EndingCluster;

                                    //
                                    //  ****    Do we need to give up the Vcb for this
                                    //          call.
                                    //

                                    StartingCluster = LlClustersFromBytes( Vcb, Scb->Header.FileSize.QuadPart );
                                    EndingCluster = LlClustersFromBytes( Vcb, Scb->Header.AllocationSize.QuadPart );

                                    //
                                    //  If there are clusters to delete, we do so now.
                                    //

                                    if (EndingCluster != StartingCluster) {

                                        try {
                                            NtfsDeleteAllocation( IrpContext,
                                                                  FileObject,
                                                                  Scb,
                                                                  StartingCluster,
                                                                  MAXLONGLONG,
                                                                  TRUE,
                                                                  TRUE );

                                        } except( (((Status = GetExceptionCode()) == STATUS_LOG_FILE_FULL) ||
                                                   (Status == STATUS_CANT_WAIT) ||
                                                   !FsRtlIsNtstatusExpected( Status ))
                                                   ? EXCEPTION_CONTINUE_SEARCH
                                                   : EXCEPTION_EXECUTE_HANDLER ) {

                                            SetFlag( Scb->ScbState, SCB_STATE_TRUNCATE_ON_CLOSE );
                                        }
                                    }

                                    LocalTruncateSize = Scb->Header.FileSize.QuadPart;
                                    TruncateSize = &LocalTruncateSize;
                                }

                            //
                            //  This is the resident case.
                            //

                            } else {

                                //
                                //  Check if the file size length is less than
                                //  the allocated size.
                                //

                                if (QuadAlign( Scb->Header.FileSize.LowPart ) < Scb->Header.AllocationSize.LowPart) {

                                    //
                                    //  We need to lookup the attribute and change
                                    //  the attribute value.  We can point to
                                    //  the attribute itself as the changing
                                    //  value.
                                    //

                                    if (CleanupAttrContext) {

                                        NtfsCleanupAttributeContext( &AttrContext );
                                    }

                                    NtfsInitializeAttributeContext( &AttrContext );
                                    CleanupAttrContext = TRUE;

                                    try {

                                        NtfsLookupAttributeForScb( IrpContext, Scb, NULL, &AttrContext );

                                        NtfsChangeAttributeValue( IrpContext,
                                                                  Fcb,
                                                                  Scb->Header.FileSize.LowPart,
                                                                  NULL,
                                                                  0,
                                                                  TRUE,
                                                                  TRUE,
                                                                  FALSE,
                                                                  FALSE,
                                                                  &AttrContext );

                                    } except( (((Status = GetExceptionCode()) == STATUS_LOG_FILE_FULL) ||
                                               (Status == STATUS_CANT_WAIT) ||
                                               !FsRtlIsNtstatusExpected( Status ))
                                               ? EXCEPTION_CONTINUE_SEARCH
                                               : EXCEPTION_EXECUTE_HANDLER ) {

                                        SetFlag( Scb->ScbState, SCB_STATE_TRUNCATE_ON_CLOSE );
                                    }

                                    //
                                    //  Remember the smaller allocation size
                                    //

                                    Scb->Header.AllocationSize.LowPart = QuadAlign(Scb->Header.FileSize.LowPart);
                                    Scb->TotalAllocated = Scb->Header.AllocationSize.QuadPart;
                                }
                            }

                            NtfsUpdateScbFromFileObject( IrpContext, FileObject, Scb, TRUE );
                        }
                    }
                }

                //
                //  If this was the last cached open, and there are open
                //  non-cached handles, attempt a flush and purge operation
                //  to avoid cache coherency overhead from these non-cached
                //  handles later.  We ignore any I/O errors from the flush
                //  except for CANT_WAIT and LOG_FILE_FULL.
                //

                if (!FlagOn( FileObject->Flags, FO_NO_INTERMEDIATE_BUFFERING ) &&
                    (Scb->NonCachedCleanupCount != 0) &&
                    (Scb->CleanupCount == (Scb->NonCachedCleanupCount + 1)) &&
                    (Scb->CompressionUnit == 0) &&
                    (Scb->NonpagedScb->SegmentObject.DataSectionObject != NULL) &&
                    (Scb->NonpagedScb->SegmentObject.ImageSectionObject == NULL) &&
                    MmCanFileBeTruncated( &Scb->NonpagedScb->SegmentObject, NULL )) {

                    //
                    //  Flush and purge the stream.
                    //

                    NtfsFlushAndPurgeScb( IrpContext,
                                          Scb,
                                          NULL );

                    //
                    //  Ignore any errors in this path.
                    //

                    IrpContext->ExceptionStatus = STATUS_SUCCESS;
                }

                if (AddToDelayQueue &&
                    !FlagOn( Scb->ScbState, SCB_STATE_DELAY_CLOSE ) &&
                    NtfsData.DelayedCloseCount <= NtfsMaxDelayedCloseCount &&
                    Fcb->CloseCount == 1) {

                    SetFlag( Scb->ScbState, SCB_STATE_DELAY_CLOSE );

                } else {

                    ClearFlag( Scb->ScbState, SCB_STATE_DELAY_CLOSE );
                }

            //
            //  If the Fcb is bad, we will truncate the cache to size zero.
            //

            } else {

                //
                //  Now set the FO_CLEANUP_COMPLETE flag.
                //

                SetFlag( FileObject->Flags, FO_CLEANUP_COMPLETE );

                TruncateSize = (PLONGLONG)&Li0;
            }

            break;

        default :

            NtfsBugCheck( TypeOfOpen, 0, 0 );
        }

        //
        //  If any of the Fcb Info flags are set we call the routine
        //  to update the duplicated information in the parent directories.
        //  We need to check here in case none of the flags are set but
        //  we want to update last access time.
        //

        if (Fcb->Info.LastAccessTime != Fcb->CurrentLastAccess) {

            if (FlagOn( Fcb->FcbState, FCB_STATE_UPDATE_STD_INFO )) {

                Fcb->Info.LastAccessTime = Fcb->CurrentLastAccess;
                SetFlag( Fcb->InfoFlags, FCB_INFO_CHANGED_LAST_ACCESS );

            } else if (!FlagOn( Fcb->FcbState, FCB_STATE_FILE_DELETED )) {

                NtfsCheckLastAccess( IrpContext, Fcb );
            }
        }

        //
        //  We check if we have to the standard information attribute.
        //  We can only update attributes on mounted volumes.
        //

        if (FlagOn( Fcb->FcbState, FCB_STATE_UPDATE_STD_INFO ) &&
            (Status == STATUS_SUCCESS) &&
            !FlagOn( Scb->ScbState, SCB_STATE_VOLUME_DISMOUNTED )) {

            ASSERT( !FlagOn( Fcb->FcbState, FCB_STATE_FILE_DELETED ));
            ASSERT( TypeOfOpen != UserVolumeOpen );

            try {

                NtfsUpdateStandardInformation( IrpContext, Fcb );

            } except( (((Status = GetExceptionCode()) == STATUS_LOG_FILE_FULL) ||
                       (Status == STATUS_CANT_WAIT) ||
                       !FsRtlIsNtstatusExpected( Status ))
                      ? EXCEPTION_CONTINUE_SEARCH
                      : EXCEPTION_EXECUTE_HANDLER ) {

                NOTHING;
            }
        }

        //
        //  Now update the duplicate information as well for volumes that are still mounted.
        //

        if (!FlagOn( Vcb->VcbState, VCB_STATE_VOLUME_MOUNTED )) {

            //
            //  We shouldn't try to write the duplicate info to a dismounted volume.
            //

            UpdateDuplicateInfo = FALSE;

        } else if (FlagOn( Fcb->InfoFlags, FCB_INFO_DUPLICATE_FLAGS ) ||
                   ((LcbForUpdate != NULL) &&
                    FlagOn( LcbForUpdate->InfoFlags, FCB_INFO_DUPLICATE_FLAGS ))) {

            ASSERT( !FlagOn( Fcb->FcbState, FCB_STATE_FILE_DELETED ));

            NtfsPrepareForUpdateDuplicate( IrpContext, Fcb, &LcbForUpdate, &ParentScb, TRUE );

            //
            //  Now update the duplicate info.
            //

            try {

                NtfsUpdateDuplicateInfo( IrpContext, Fcb, LcbForUpdate, ParentScb );

            } except( (((Status = GetExceptionCode()) == STATUS_LOG_FILE_FULL) ||
                       (Status == STATUS_CANT_WAIT) ||
                       !FsRtlIsNtstatusExpected( Status ))
                      ? EXCEPTION_CONTINUE_SEARCH
                      : EXCEPTION_EXECUTE_HANDLER ) {

                NOTHING;
            }

            UpdateDuplicateInfo = TRUE;
        }

        //
        //  If we have modified the Info structure or security, we report this
        //  to the dir-notify package (except for OpenById cases).
        //

        if (!OpenById) {

            ULONG FilterMatch;

            //
            //  Check whether we need to report on file changes.
            //

            if ((Vcb->NotifyCount != 0) &&
                (UpdateDuplicateInfo || FlagOn( Fcb->InfoFlags, FCB_INFO_MODIFIED_SECURITY ))) {

                //
                //  We map the Fcb info flags into the dir notify flags.
                //

                FilterMatch = NtfsBuildDirNotifyFilter( IrpContext,
                                                        (Fcb->InfoFlags |
                                                         (LcbForUpdate ? LcbForUpdate->InfoFlags : 0) ));

                //
                //  If the filter match is non-zero, that means we also need to do a
                //  dir notify call.
                //

                if (FilterMatch != 0) {

                    NtfsReportDirNotify( IrpContext,
                                         Vcb,
                                         &Ccb->FullFileName,
                                         Ccb->LastFileNameOffset,
                                         NULL,
                                         ((FlagOn( Ccb->Flags, CCB_FLAG_PARENT_HAS_DOS_COMPONENT ) &&
                                           Ccb->Lcb != NULL &&
                                           Ccb->Lcb->Scb->ScbType.Index.NormalizedName.Buffer != NULL) ?
                                          &Ccb->Lcb->Scb->ScbType.Index.NormalizedName :
                                          NULL),
                                         FilterMatch,
                                         FILE_ACTION_MODIFIED,
                                         ParentFcb );
                }
            }

            ClearFlag( Fcb->InfoFlags, FCB_INFO_MODIFIED_SECURITY );

            //
            //  If this is a named stream with changes then report them as well.
            //

            if ((Scb->AttributeName.Length != 0) &&
                NtfsIsTypeCodeUserData( Scb->AttributeTypeCode )) {

                if ((Vcb->NotifyCount != 0) &&
                    FlagOn( Scb->ScbState,
                            SCB_STATE_NOTIFY_REMOVE_STREAM |
                            SCB_STATE_NOTIFY_RESIZE_STREAM |
                            SCB_STATE_NOTIFY_MODIFY_STREAM )) {

                    ULONG Action;

                    FilterMatch = 0;

                    //
                    //  Start by checking for a delete.
                    //

                    if (FlagOn( Scb->ScbState, SCB_STATE_NOTIFY_REMOVE_STREAM )) {

                        FilterMatch = FILE_NOTIFY_CHANGE_STREAM_NAME;
                        Action = FILE_ACTION_REMOVED_STREAM;

                    } else {

                        //
                        //  Check if the file size changed.
                        //

                        if (FlagOn( Scb->ScbState, SCB_STATE_NOTIFY_RESIZE_STREAM )) {

                            FilterMatch = FILE_NOTIFY_CHANGE_STREAM_SIZE;
                        }

                        //
                        //  Now check if the stream data was modified.
                        //

                        if (FlagOn( Scb->ScbState, SCB_STATE_NOTIFY_MODIFY_STREAM )) {

                            SetFlag( FilterMatch, FILE_NOTIFY_CHANGE_STREAM_WRITE );
                        }

                        Action = FILE_ACTION_MODIFIED_STREAM;
                    }

                    NtfsReportDirNotify( IrpContext,
                                         Vcb,
                                         &Ccb->FullFileName,
                                         Ccb->LastFileNameOffset,
                                         &Scb->AttributeName,
                                         ((FlagOn( Ccb->Flags, CCB_FLAG_PARENT_HAS_DOS_COMPONENT ) &&
                                           Ccb->Lcb != NULL &&
                                           Ccb->Lcb->Scb->ScbType.Index.NormalizedName.Buffer != NULL) ?
                                          &Ccb->Lcb->Scb->ScbType.Index.NormalizedName :
                                          NULL),
                                         FilterMatch,
                                         Action,
                                         ParentFcb );
                }

                ClearFlag( Scb->ScbState,
                           SCB_STATE_NOTIFY_ADD_STREAM |
                           SCB_STATE_NOTIFY_REMOVE_STREAM |
                           SCB_STATE_NOTIFY_RESIZE_STREAM |
                           SCB_STATE_NOTIFY_MODIFY_STREAM );
            }
        }

        if (UpdateDuplicateInfo) {

            NtfsUpdateLcbDuplicateInfo( Fcb, LcbForUpdate );
            Fcb->InfoFlags = 0;
        }

        //
        //  Always clear the update standard information flag.
        //

        ClearFlag( Fcb->FcbState, FCB_STATE_UPDATE_STD_INFO );

        //
        //  Let's give up the parent Fcb if we have acquired it.  This will
        //  prevent deadlocks in any uninitialize code below.
        //

        if (AcquiredParentScb) {

            NtfsReleaseScb( IrpContext, ParentScb );
            AcquiredParentScb = FALSE;
        }

        //
        //  Uninitialize the cache map if this file has been cached or we are
        //  trying to delete.
        //

        if ((FileObject->PrivateCacheMap != NULL) || (TruncateSize != NULL)) {

            CcUninitializeCacheMap( FileObject, (PLARGE_INTEGER)TruncateSize, NULL );
        }

        //
        //  Check that the non-cached handle count is consistent.
        //

        ASSERT( !FlagOn( FileObject->Flags, FO_NO_INTERMEDIATE_BUFFERING ) ||
                (Scb->NonCachedCleanupCount != 0 ));

        if (CleanupAttrContext) {

            NtfsCleanupAttributeContext( &AttrContext );
            CleanupAttrContext = FALSE;
        }

        //
        //  Now decrement the cleanup counts.
        //

        NtfsDecrementCleanupCounts( Scb,
                                    LcbForCounts,
                                    BooleanFlagOn( FileObject->Flags, FO_NO_INTERMEDIATE_BUFFERING ));

        //
        //  We remove the share access from the Scb.
        //

        IoRemoveShareAccess( FileObject, &Scb->ShareAccess );

        //
        //  Modify the delete counts in the Fcb.
        //

        if (FlagOn( Ccb->Flags, CCB_FLAG_DELETE_FILE )) {

            Fcb->FcbDeleteFile -= 1;
            ClearFlag( Ccb->Flags, CCB_FLAG_DELETE_FILE );
        }

        if (FlagOn( Ccb->Flags, CCB_FLAG_DENY_DELETE )) {

            Fcb->FcbDenyDelete -= 1;
            ClearFlag( Ccb->Flags, CCB_FLAG_DENY_DELETE );
        }

        //
        //  Since this request has completed we can adjust the total link count
        //  in the Fcb.
        //

        Fcb->TotalLinks -= TotalLinkAdj;

#ifdef _CAIRO_

        //
        //  Release the quota control block.  This does not have to be done
        //  here however, it allows us to free up the quota control block
        //  before the fcb is removed from the table.  This keeps the assert
        //  about quota table empty from triggering in
        //  NtfsClearAndVerifyQuotaIndex.
        //

        if (NtfsPerformQuotaOperation(Fcb) &&
            FlagOn( Fcb->FcbState, FCB_STATE_FILE_DELETED )) {
            NtfsDereferenceQuotaControlBlock( Vcb,
                                              &Fcb->QuotaControl );
        }

#endif // _CAIRO_


    } finally {

        DebugUnwind( NtfsCommonCleanup );

        ClearFlag( IrpContext->Flags, IRP_CONTEXT_FLAG_QUOTA_DISABLE );

        //
        //  Release any resources held.
        //

        NtfsReleaseVcb( IrpContext, Vcb );

        //
        //  We clear the file object pointer in the Ccb.
        //  This prevents us from trying to access this in a
        //  rename operation.
        //

        SetFlag( Ccb->Flags, CCB_FLAG_CLEANUP );

        if (AcquiredScb) {

            NtfsReleaseScb( IrpContext, Scb );
        }

        if (CleanupAttrContext) {

            NtfsCleanupAttributeContext( &AttrContext );
        }

        if (NamePair.Long.Buffer != NamePair.LongBuffer) {

            NtfsFreePool(NamePair.Long.Buffer);
        }

        if (!AbnormalTermination()) {

            NtfsCompleteRequest( &IrpContext, &Irp, Status );
        }

        //
        //  And return to our caller
        //

        DebugTrace( -1, Dbg, ("NtfsCommonCleanup -> %08lx\n", Status) );
    }

    return Status;
}

#ifdef _CAIRO_
VOID
NtfsContractQuotaToFileSize (
    IN PIRP_CONTEXT IrpContext,
    IN PSCB Scb
    )

/*++

Routine Description:

    This routine converts the quota charged for a stream from allocation size
    to file size.  This should only be called for cleanup.

Arguments:

    Scb - Supplies a pointer to the being changed.

Return Value:

    None.

--*/

{
    LONGLONG Delta;
    NTSTATUS Status;

    PAGED_CODE();
    ASSERT( IrpContext->MajorFunction == IRP_MJ_CLEANUP );
    ASSERT(!FlagOn( IrpContext->Flags, IRP_CONTEXT_FLAG_QUOTA_DISABLE ));

    try {

        ASSERT( NtfsIsTypeCodeSubjectToQuota( Scb->AttributeTypeCode ));
        ASSERT( FlagOn( Scb->ScbState, SCB_STATE_SUBJECT_TO_QUOTA ));

        if (FlagOn( Scb->ScbState, SCB_STATE_ATTRIBUTE_RESIDENT )) {

            Delta = (LONG) Scb->Header.FileSize.LowPart -
                           NtfsResidentStreamQuota( Scb->Vcb );
        } else {
            Delta = Scb->Header.FileSize.QuadPart -
                    Scb->Header.AllocationSize.QuadPart;
        }

        if (Delta != 0) {

            NtfsUpdateFileQuota( IrpContext,
                                 Scb->Fcb,
                                 &Delta,
                                 TRUE,
                                 FALSE );
        }

        ClearFlag( Scb->ScbState, SCB_STATE_QUOTA_ENLARGED );

    } except( (((Status = GetExceptionCode()) == STATUS_LOG_FILE_FULL) ||
               (Status == STATUS_CANT_WAIT) ||
               !FsRtlIsNtstatusExpected( Status ))
              ? EXCEPTION_CONTINUE_SEARCH
              : EXCEPTION_EXECUTE_HANDLER ) {

        NOTHING;
    }

}
#endif // _CAIRO_