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
/*
 * COPYRIGHT:        See COPYRIGHT.TXT
 * PROJECT:          Ext2 File System Driver for WinNT/2K/XP
 * FILE:             cleanup.c
 * PROGRAMMER:       Matt Wu <mattwu@163.com>
 * HOMEPAGE:         http://www.ext2fsd.com
 * UPDATE HISTORY:
 */

/* INCLUDES *****************************************************************/

#include "ext2fs.h"

/* GLOBALS ***************************************************************/

extern PEXT2_GLOBAL Ext2Global;

/* DEFINITIONS *************************************************************/

NTSTATUS
Ext2Cleanup (IN PEXT2_IRP_CONTEXT IrpContext)
{
    PDEVICE_OBJECT  DeviceObject;
    NTSTATUS        Status = STATUS_SUCCESS;
    PEXT2_VCB       Vcb = NULL;
    PFILE_OBJECT    FileObject;
    PEXT2_FCB       Fcb = NULL;
    PEXT2_CCB       Ccb = NULL;
    PIRP            Irp = NULL;
    PEXT2_MCB       Mcb = NULL;


    BOOLEAN         VcbResourceAcquired = FALSE;
    BOOLEAN         FcbResourceAcquired = FALSE;
    BOOLEAN         FcbPagingIoResourceAcquired = FALSE;
    BOOLEAN         SymLinkDelete = FALSE;

    _SEH2_TRY {

        ASSERT(IrpContext != NULL);
        ASSERT((IrpContext->Identifier.Type == EXT2ICX) &&
               (IrpContext->Identifier.Size == sizeof(EXT2_IRP_CONTEXT)));

        DeviceObject = IrpContext->DeviceObject;
        if (IsExt2FsDevice(DeviceObject))  {
            Status = STATUS_SUCCESS;<--- Status is assigned
            _SEH2_LEAVE;
        }

        Irp = IrpContext->Irp;
        Vcb = (PEXT2_VCB) DeviceObject->DeviceExtension;
        ASSERT(Vcb != NULL);
        ASSERT((Vcb->Identifier.Type == EXT2VCB) &&
               (Vcb->Identifier.Size == sizeof(EXT2_VCB)));

        if (!IsVcbInited(Vcb)) {
            Status = STATUS_SUCCESS;<--- Status is assigned
            _SEH2_LEAVE;
        }

        FileObject = IrpContext->FileObject;
        Fcb = (PEXT2_FCB) FileObject->FsContext;
        if (!Fcb || (Fcb->Identifier.Type != EXT2VCB &&
                     Fcb->Identifier.Type != EXT2FCB)) {
            Status = STATUS_SUCCESS;<--- Status is assigned
            _SEH2_LEAVE;
        }
        Mcb = Fcb->Mcb;
        Ccb = (PEXT2_CCB) FileObject->FsContext2;

        if (IsFlagOn(FileObject->Flags, FO_CLEANUP_COMPLETE)) {
            Status = STATUS_SUCCESS;<--- Status is assigned
            _SEH2_LEAVE;
        }

        if (Fcb->Identifier.Type == EXT2VCB) {

            ExAcquireResourceExclusiveLite(
                    &Vcb->MainResource, TRUE);
            VcbResourceAcquired = TRUE;

            if (FlagOn(Vcb->Flags, VCB_VOLUME_LOCKED) &&
                Vcb->LockFile == FileObject ){

                ClearFlag(Vcb->Flags, VCB_VOLUME_LOCKED);
                Vcb->LockFile = NULL;
                Ext2ClearVpbFlag(Vcb->Vpb, VPB_LOCKED);
            }

            if (Ccb) {
                Ext2DerefXcb(&Vcb->OpenHandleCount);
                Ext2DerefXcb(&Vcb->OpenVolumeCount);
            }

            IoRemoveShareAccess(FileObject, &Vcb->ShareAccess);

            Status = STATUS_SUCCESS;<--- Status is assigned
            _SEH2_LEAVE;
        }

        ASSERT((Fcb->Identifier.Type == EXT2FCB) &&
               (Fcb->Identifier.Size == sizeof(EXT2_FCB)));

        FcbResourceAcquired =
            ExAcquireResourceExclusiveLite(
                &Fcb->MainResource,
                TRUE
            );

        if (IsFlagOn(FileObject->Flags, FO_CLEANUP_COMPLETE)) {
            if (IsFlagOn(FileObject->Flags, FO_FILE_MODIFIED) &&
                    IsFlagOn(Vcb->Flags, VCB_FLOPPY_DISK) &&
                    !IsVcbReadOnly(Vcb) ) {
                Status = Ext2FlushFile(IrpContext, Fcb, Ccb);<--- Status is assigned
            }
            _SEH2_LEAVE;
        }

        if (Ccb == NULL) {
            Status = STATUS_SUCCESS;<--- Status is assigned
            _SEH2_LEAVE;
        }

        if (IsDirectory(Fcb)) {
            if (IsFlagOn(Ccb->Flags, CCB_DELETE_ON_CLOSE))  {
                SetLongFlag(Fcb->Flags, FCB_DELETE_PENDING);

                FsRtlNotifyFullChangeDirectory(
                    Vcb->NotifySync,
                    &Vcb->NotifyList,
                    Ccb,
                    NULL,
                    FALSE,
                    FALSE,
                    0,
                    NULL,
                    NULL,
                    NULL );
            }

            FsRtlNotifyCleanup(Vcb->NotifySync, &Vcb->NotifyList, Ccb);
        }

        ASSERT((Ccb->Identifier.Type == EXT2CCB) &&
               (Ccb->Identifier.Size == sizeof(EXT2_CCB)));

        Ext2DerefXcb(&Vcb->OpenHandleCount);
        Ext2DerefXcb(&Fcb->OpenHandleCount);

        if (IsFlagOn(FileObject->Flags, FO_FILE_MODIFIED)) {
            Fcb->Mcb->FileAttr |= FILE_ATTRIBUTE_ARCHIVE;
        }

        if (IsDirectory(Fcb)) {

            ext3_release_dir(Fcb->Inode, &Ccb->filp);

        } else {

            if ( IsFlagOn(FileObject->Flags, FO_FILE_MODIFIED) &&
                    !IsFlagOn(Ccb->Flags, CCB_LAST_WRITE_UPDATED)) {

                LARGE_INTEGER   SysTime;
                KeQuerySystemTime(&SysTime);

                Fcb->Inode->i_atime =
                    Fcb->Inode->i_mtime = Ext2LinuxTime(SysTime);
                Fcb->Mcb->LastAccessTime =
                    Fcb->Mcb->LastWriteTime = Ext2NtTime(Fcb->Inode->i_atime);

                Ext2SaveInode(IrpContext, Vcb, Fcb->Inode);

                Ext2NotifyReportChange(
                    IrpContext,
                    Vcb,
                    Fcb->Mcb,
                    FILE_NOTIFY_CHANGE_ATTRIBUTES |
                    FILE_NOTIFY_CHANGE_LAST_WRITE |
                    FILE_NOTIFY_CHANGE_LAST_ACCESS,
                    FILE_ACTION_MODIFIED );
            }

            FsRtlCheckOplock( &Fcb->Oplock,
                              Irp,
                              IrpContext,
                              NULL,
                              NULL );

            Fcb->Header.IsFastIoPossible = Ext2IsFastIoPossible(Fcb);

            if (!IsFlagOn(FileObject->Flags, FO_CACHE_SUPPORTED)) {
                Fcb->NonCachedOpenCount--;
            }

            if (IsFlagOn(Ccb->Flags, CCB_DELETE_ON_CLOSE))  {
                if (Ccb->SymLink || IsInodeSymLink(&Mcb->Inode)) {
                    SymLinkDelete = TRUE;
                } else {
                    SetLongFlag(Fcb->Flags, FCB_DELETE_PENDING);
                }
            }

            //
            // Drop any byte range locks this process may have on the file.
            //

            FsRtlFastUnlockAll(
                &Fcb->FileLockAnchor,
                FileObject,
                IoGetRequestorProcess(Irp),
                NULL  );

            //
            // If there are no byte range locks owned by other processes on the
            // file the fast I/O read/write functions doesn't have to check for
            // locks so we set IsFastIoPossible to FastIoIsPossible again.
            //
            if (!FsRtlGetNextFileLock(&Fcb->FileLockAnchor, TRUE)) {
                if (Fcb->Header.IsFastIoPossible != FastIoIsPossible) {<--- Condition 'Fcb->Header.IsFastIoPossible!=FastIoIsPossible' is redundant
#if EXT2_DEBUG
                    DEBUG(DL_INF, (": %-16.16s %-31s %wZ\n",
                                   Ext2GetCurrentProcessName(),
                                   "FastIoIsPossible",
                                   &Fcb->Mcb->FullName
                                  ));
#endif

                    Fcb->Header.IsFastIoPossible = FastIoIsPossible;<--- Assignment 'Fcb->Header.IsFastIoPossible=FastIoIsPossible'
                }
            }

            if (Fcb->OpenHandleCount == 0 && FlagOn(Fcb->Flags, FCB_ALLOC_IN_CREATE |
                                                                FCB_ALLOC_IN_SETINFO) ){

                if (FlagOn(Fcb->Flags, FCB_ALLOC_IN_SETINFO)) {
                    if (Fcb->Header.ValidDataLength.QuadPart < Fcb->Header.FileSize.QuadPart) {
                        if (!INODE_HAS_EXTENT(Fcb->Inode)) {
                        #if EXT2_PRE_ALLOCATION_SUPPORT
                            _SEH2_TRY {
                                CcZeroData( FileObject,
                                           &Fcb->Header.ValidDataLength,
                                           &Fcb->Header.AllocationSize,
                                           TRUE);
                            } _SEH2_EXCEPT (EXCEPTION_EXECUTE_HANDLER) {
                                DbgBreak();
                            } _SEH2_END;
                        #endif
                        }
                    }
                }

                if (FlagOn(Fcb->Flags, FCB_ALLOC_IN_CREATE)) {

                    LARGE_INTEGER Size;

                    ExAcquireResourceExclusiveLite(&Fcb->PagingIoResource, TRUE);
                    FcbPagingIoResourceAcquired = TRUE;<--- FcbPagingIoResourceAcquired is assigned

                    Size.QuadPart = CEILING_ALIGNED(ULONGLONG,
                                                    (ULONGLONG)Fcb->Mcb->Inode.i_size,
                                                    (ULONGLONG)BLOCK_SIZE);
                    if (!IsFlagOn(Fcb->Flags, FCB_DELETE_PENDING)) {

                        Ext2TruncateFile(IrpContext, Vcb, Fcb->Mcb, &Size);
                        Fcb->Header.AllocationSize = Size;
                        Fcb->Header.FileSize.QuadPart = Mcb->Inode.i_size;
                        if (Fcb->Header.ValidDataLength.QuadPart > Fcb->Header.FileSize.QuadPart)
                            Fcb->Header.ValidDataLength.QuadPart = Fcb->Header.FileSize.QuadPart;
                        if (CcIsFileCached(FileObject)) {
                            CcSetFileSizes(FileObject,
                                           (PCC_FILE_SIZES)(&(Fcb->Header.AllocationSize)));
                        }
                    }
                    ClearLongFlag(Fcb->Flags, FCB_ALLOC_IN_CREATE|FCB_ALLOC_IN_WRITE|FCB_ALLOC_IN_SETINFO);
                    ExReleaseResourceLite(&Fcb->PagingIoResource);
                    FcbPagingIoResourceAcquired = FALSE;<--- FcbPagingIoResourceAcquired is overwritten
                }
            }
        }

        IoRemoveShareAccess(FileObject, &Fcb->ShareAccess);

        if (!IsDirectory(Fcb)) {

            if ( IsFlagOn(FileObject->Flags, FO_CACHE_SUPPORTED) &&
                    (Fcb->NonCachedOpenCount == Fcb->OpenHandleCount) &&
                    (Fcb->SectionObject.DataSectionObject != NULL)) {

                if (!IsVcbReadOnly(Vcb)) {
                    CcFlushCache(&Fcb->SectionObject, NULL, 0, NULL);
                    ClearLongFlag(Fcb->Flags, FCB_FILE_MODIFIED);
                }

                /* purge cache if all remaining openings are non-cached */
                if (Fcb->NonCachedOpenCount > 0 ||
                    IsFlagOn(Fcb->Flags, FCB_DELETE_PENDING)) {
                    if (ExAcquireResourceExclusiveLite(&(Fcb->PagingIoResource), TRUE)) {
                        ExReleaseResourceLite(&(Fcb->PagingIoResource));
                    }

                    /* CcPurge could generate recursive IRP_MJ_CLOSE request */
                    CcPurgeCacheSection( &Fcb->SectionObject,
                                         NULL,
                                         0,
                                         FALSE );
                }
            }

            CcUninitializeCacheMap(FileObject, NULL, NULL);
        }

        if (SymLinkDelete ||
            (IsFlagOn(Fcb->Flags, FCB_DELETE_PENDING) &&
             Fcb->OpenHandleCount == 0) ) {

            //
            // Ext2DeleteFile will acquire these lock inside
            //

            if (FcbResourceAcquired) {
                ExReleaseResourceLite(&Fcb->MainResource);
                FcbResourceAcquired = FALSE;<--- FcbResourceAcquired is assigned
            }

            //
            //  this file is to be deleted ...
            //
            if (Ccb->SymLink) {
                Mcb = Ccb->SymLink;
                FileObject->DeletePending = FALSE;
            }

            Status = Ext2DeleteFile(IrpContext, Vcb, Fcb, Mcb);

            if (NT_SUCCESS(Status)) {
                if (IsMcbDirectory(Mcb)) {
                    Ext2NotifyReportChange( IrpContext, Vcb, Mcb,
                                            FILE_NOTIFY_CHANGE_DIR_NAME,
                                            FILE_ACTION_REMOVED );
                } else {
                    Ext2NotifyReportChange( IrpContext, Vcb, Mcb,
                                            FILE_NOTIFY_CHANGE_FILE_NAME,
                                            FILE_ACTION_REMOVED );
                }
            }

            //
            // re-acquire the main resource lock
            //

            FcbResourceAcquired =<--- FcbResourceAcquired is overwritten
                ExAcquireResourceExclusiveLite(
                    &Fcb->MainResource,
                    TRUE
                );
            if (!SymLinkDelete) {
                 SetFlag(FileObject->Flags, FO_FILE_MODIFIED);
                if (CcIsFileCached(FileObject)) {
                    CcSetFileSizes(FileObject,
                                   (PCC_FILE_SIZES)(&(Fcb->Header.AllocationSize)));
                }
            }
        }

        DEBUG(DL_INF, ( "Ext2Cleanup: OpenCount=%u ReferCount=%u NonCahcedCount=%xh %wZ\n",
                        Fcb->OpenHandleCount, Fcb->ReferenceCount, Fcb->NonCachedOpenCount, &Fcb->Mcb->FullName));

        Status = STATUS_SUCCESS;<--- Status is overwritten<--- Status is overwritten<--- Status is overwritten<--- Status is overwritten<--- Status is overwritten<--- Status is overwritten<--- Status is overwritten

        if (FileObject) {
            SetFlag(FileObject->Flags, FO_CLEANUP_COMPLETE);
        }

    } _SEH2_FINALLY {

        if (FcbPagingIoResourceAcquired) {
            ExReleaseResourceLite(&Fcb->PagingIoResource);
        }

        if (FcbResourceAcquired) {
            ExReleaseResourceLite(&Fcb->MainResource);
        }

        if (VcbResourceAcquired) {
            ExReleaseResourceLite(&Vcb->MainResource);
        }

        if (!IrpContext->ExceptionInProgress) {
            if (Status == STATUS_PENDING) {
                Ext2QueueRequest(IrpContext);
            } else {
                IrpContext->Irp->IoStatus.Status = Status;
                Ext2CompleteIrpContext(IrpContext, Status);
            }
        }
    } _SEH2_END;

    return Status;
}