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
////////////////////////////////////////////////////////////////////
// Copyright (C) Alexander Telyatnikov, Ivan Keliukh, Yegor Anchishkin, SKIF Software, 1999-2013. Kiev, Ukraine
// All rights reserved
// This file was released under the GPLv2 on June 2015.
////////////////////////////////////////////////////////////////////
/*************************************************************************
*
* File: Flush.cpp
*
* Module: UDF File System Driver (Kernel mode execution only)
*
* Description:
*   Contains code to handle the "Flush Buffers" dispatch entry point.
*
*************************************************************************/

#include            "udffs.h"

// define the file specific bug-check id
#define         UDF_BUG_CHECK_ID                UDF_FILE_FLUSH



/*************************************************************************
*
* Function: UDFFlush()
*
* Description:
*   The I/O Manager will invoke this routine to handle a flush buffers
*   request
*
* Expected Interrupt Level (for execution) :
*
*  IRQL_PASSIVE_LEVEL (invocation at higher IRQL will cause execution
*   to be deferred to a worker thread context)
*
* Return Value: STATUS_SUCCESS/Error
*
*************************************************************************/
NTSTATUS
NTAPI
UDFFlush(
    PDEVICE_OBJECT      DeviceObject,       // the logical volume device object
    PIRP                Irp)                // I/O Request Packet
{
    NTSTATUS            RC = STATUS_SUCCESS;
    PtrUDFIrpContext    PtrIrpContext = NULL;
    BOOLEAN             AreWeTopLevel = FALSE;

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

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

    // set the top level context
    AreWeTopLevel = UDFIsIrpTopLevel(Irp);
    ASSERT(!UDFIsFSDevObj(DeviceObject));

    _SEH2_TRY {

        // get an IRP context structure and issue the request
        PtrIrpContext = UDFAllocateIrpContext(Irp, DeviceObject);
        if(PtrIrpContext) {
            RC = UDFCommonFlush(PtrIrpContext, Irp);
        } else {
            RC = STATUS_INSUFFICIENT_RESOURCES;
            Irp->IoStatus.Status = RC;
            Irp->IoStatus.Information = 0;
            // complete the IRP
            IoCompleteRequest(Irp, IO_DISK_INCREMENT);
        }

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

        RC = UDFExceptionHandler(PtrIrpContext, Irp);

        UDFLogEvent(UDF_ERROR_INTERNAL_ERROR, RC);
    } _SEH2_END;

    if (AreWeTopLevel) {
        IoSetTopLevelIrp(NULL);
    }

    FsRtlExitFileSystem();

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



/*************************************************************************
*
* Function: UDFCommonFlush()
*
* Description:
*   The actual work is performed here. This routine may be invoked in one'
*   of the two possible contexts:
*   (a) in the context of a system worker thread
*   (b) in the context of the original caller
*
* Expected Interrupt Level (for execution) :
*
*  IRQL_PASSIVE_LEVEL
*
* Return Value: STATUS_SUCCESS/Error
*
*************************************************************************/
NTSTATUS
UDFCommonFlush(
    PtrUDFIrpContext PtrIrpContext,
    PIRP             Irp
    )
{
    NTSTATUS            RC = STATUS_SUCCESS;
    PIO_STACK_LOCATION  IrpSp = NULL;
    PFILE_OBJECT        FileObject = NULL;
    PtrUDFFCB           Fcb = NULL;
    PtrUDFCCB           Ccb = NULL;
    PVCB                Vcb = NULL;
    PtrUDFNTRequiredFCB NtReqFcb = NULL;
    BOOLEAN             AcquiredVCB = FALSE;
    BOOLEAN             AcquiredFCB = FALSE;
    BOOLEAN             PostRequest = FALSE;
    BOOLEAN             CanWait = TRUE;

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

    _SEH2_TRY {

        // Get some of the parameters supplied to us
        CanWait = ((PtrIrpContext->IrpContextFlags & UDF_IRP_CONTEXT_CAN_BLOCK) ? TRUE : FALSE);
        // If we cannot wait, post the request immediately since a flush is inherently blocking/synchronous.
        if (!CanWait) {
            PostRequest = TRUE;
            try_return(RC);<--- Unused variable value 'RC'
        }

        // First, get a pointer to the current I/O stack location
        IrpSp = IoGetCurrentIrpStackLocation(Irp);
        ASSERT(IrpSp);

        FileObject = IrpSp->FileObject;
        ASSERT(FileObject);

        // Get the FCB and CCB pointers
        Ccb = (PtrUDFCCB)(FileObject->FsContext2);
        ASSERT(Ccb);
        Fcb = Ccb->Fcb;
        ASSERT(Fcb);
        NtReqFcb = Fcb->NTRequiredFCB;

        // Check the type of object passed-in. That will determine the course of
        // action we take.
        if ((Fcb->NodeIdentifier.NodeType == UDF_NODE_TYPE_VCB) || (Fcb->FCBFlags & UDF_FCB_ROOT_DIRECTORY)) {

            if (Fcb->NodeIdentifier.NodeType == UDF_NODE_TYPE_VCB) {
                Vcb = (PVCB)(Fcb);
            } else {
                Vcb = Fcb->Vcb;
            }
            Vcb->VCBFlags |= UDF_VCB_SKIP_EJECT_CHECK;

#ifdef UDF_DELAYED_CLOSE
            UDFCloseAllDelayed(Vcb);
#endif //UDF_DELAYED_CLOSE

            UDFAcquireResourceExclusive(&(Vcb->VCBResource), TRUE);
            AcquiredVCB = TRUE;<--- AcquiredVCB is assigned
            // The caller wishes to flush all files for the mounted
            // logical volume. The flush volume routine below should simply
            // walk through all of the open file streams, acquire the
            // VCB resource, and request the flush operation from the Cache
            // Manager. Basically, the sequence of operations listed below
            // for a single file should be executed on all open files.

            UDFFlushLogicalVolume(PtrIrpContext, Irp, Vcb, 0);

            UDFReleaseResource(&(Vcb->VCBResource));
            AcquiredVCB = FALSE;<--- AcquiredVCB is overwritten

            try_return(RC);<--- Unused variable value 'RC'
        } else
        if (!(Fcb->FCBFlags & UDF_FCB_DIRECTORY)) {
            // This is a regular file.
            Vcb = Fcb->Vcb;
            ASSERT(Vcb);
            if(!ExIsResourceAcquiredExclusiveLite(&(Vcb->VCBResource)) &&
               !ExIsResourceAcquiredSharedLite(&(Vcb->VCBResource))) {
                UDFAcquireResourceShared(&(Vcb->VCBResource), TRUE);
                AcquiredVCB = TRUE;
            }
            UDF_CHECK_PAGING_IO_RESOURCE(NtReqFcb);
            UDFAcquireResourceExclusive(&(NtReqFcb->MainResource), TRUE);
            AcquiredFCB = TRUE;

            // Request the Cache Manager to perform a flush operation.
            // Further, instruct the Cache Manager that we wish to flush the
            // entire file stream.
            UDFFlushAFile(Fcb, Ccb, &(Irp->IoStatus), 0);
            RC = Irp->IoStatus.Status;

            // Some log-based FSD implementations may wish to flush their
            // log files at this time. Finally, we should update the time-stamp
            // values for the file stream appropriately. This would involve
            // obtaining the current time and modifying the appropriate directory
            // entry fields.
        } else {
            Vcb = Fcb->Vcb;
        }

try_exit:   NOTHING;

    } _SEH2_FINALLY {

        if (AcquiredFCB) {
            UDF_CHECK_PAGING_IO_RESOURCE(NtReqFcb);
            UDFReleaseResource(&(NtReqFcb->MainResource));
            AcquiredFCB = FALSE;
        }
        if (AcquiredVCB) {
            UDFReleaseResource(&(Vcb->VCBResource));
            AcquiredVCB = FALSE;
        }

        if(!_SEH2_AbnormalTermination()) {
            if (PostRequest) {
                // Nothing to lock now.
                BrutePoint();
                RC = UDFPostRequest(PtrIrpContext, Irp);
            } else {
                // Some applications like this request very much
                // (ex. WinWord). But it's not a good idea for CD-R/RW media
                if(Vcb->FlushMedia) {
                    PIO_STACK_LOCATION      PtrNextIoStackLocation = NULL;
                    NTSTATUS                RC1 = STATUS_SUCCESS;

                    // Send the request down at this point.
                    // To do this, we must set the next IRP stack location, and
                    // maybe set a completion routine.
                    // Be careful about marking the IRP pending if the lower level
                    // driver returned pending and we do have a completion routine!
                    PtrNextIoStackLocation = IoGetNextIrpStackLocation(Irp);
                    *PtrNextIoStackLocation = *IrpSp;

                    // Set the completion routine to "eat-up" any
                    // STATUS_INVALID_DEVICE_REQUEST error code returned by the lower
                    // level driver.
                    IoSetCompletionRoutine(Irp, UDFFlushCompletion, NULL, TRUE, TRUE, TRUE);

                    RC1 = IoCallDriver(Vcb->TargetDeviceObject, Irp);

                    RC = ((RC1 == STATUS_INVALID_DEVICE_REQUEST) ? RC : RC1);

                    // Release the IRP context at this time.
                    UDFReleaseIrpContext(PtrIrpContext);
                } else {
                    Irp->IoStatus.Status = RC;
                    Irp->IoStatus.Information = 0;
                    // Free up the Irp Context
                    UDFReleaseIrpContext(PtrIrpContext);
                    // complete the IRP
                    IoCompleteRequest(Irp, IO_DISK_INCREMENT);
                }
            }
        }
    } _SEH2_END;

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


/*************************************************************************
*
* Function: UDFFlushAFile()
*
* Description:
*   Tell the Cache Manager to perform a flush.
*
* Expected Interrupt Level (for execution) :
*
*  IRQL_PASSIVE_LEVEL
*
* Return Value: None
*
*************************************************************************/
ULONG
UDFFlushAFile(
    IN PtrUDFFCB           Fcb,
    IN PtrUDFCCB           Ccb,
    OUT PIO_STATUS_BLOCK   PtrIoStatus,
    IN ULONG               FlushFlags
    )
{
    BOOLEAN SetArchive = FALSE;
//    BOOLEAN PurgeCache = FALSE;
    ULONG ret_val = 0;

    UDFPrint(("UDFFlushAFile: \n"));
    if(!Fcb)
        return 0;

    _SEH2_TRY {
        if(Fcb->Vcb->VCBFlags & UDF_VCB_FLAGS_RAW_DISK)
            return 0;
    } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
        BrutePoint();
    } _SEH2_END;
#ifndef UDF_READ_ONLY_BUILD
    // Flush Security if required
    _SEH2_TRY {
        UDFWriteSecurity(Fcb->Vcb, Fcb, &(Fcb->NTRequiredFCB->SecurityDesc));
    } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
        BrutePoint();
    } _SEH2_END;
#endif //UDF_READ_ONLY_BUILD
    // Flush SDir if any
    _SEH2_TRY {
        if(UDFHasAStreamDir(Fcb->FileInfo) &&
           Fcb->FileInfo->Dloc->SDirInfo &&
           !UDFIsSDirDeleted(Fcb->FileInfo->Dloc->SDirInfo) ) {
            ret_val |=
                UDFFlushADirectory(Fcb->Vcb, Fcb->FileInfo->Dloc->SDirInfo, PtrIoStatus, FlushFlags);
        }
    } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
        BrutePoint();
    } _SEH2_END;
    // Flush File
    _SEH2_TRY {
        if((Fcb->CachedOpenHandleCount || !Fcb->OpenHandleCount) &&
            Fcb->NTRequiredFCB->SectionObject.DataSectionObject) {
            if(!(Fcb->NTRequiredFCB->NtReqFCBFlags & UDF_NTREQ_FCB_DELETED)
                                         &&
                ((Fcb->NTRequiredFCB->NtReqFCBFlags & UDF_NTREQ_FCB_MODIFIED) ||
                 (Ccb && !(Ccb->CCBFlags & UDF_CCB_FLUSHED)) )) {
                MmPrint(("    CcFlushCache()\n"));
                CcFlushCache(&(Fcb->NTRequiredFCB->SectionObject), NULL, 0, PtrIoStatus);
            }
            // notice, that we should purge cache
            // we can't do it now, because it may cause last Close
            // request & thus, structure deallocation
//            PurgeCache = TRUE;

#ifndef UDF_READ_ONLY_BUILD
            if(Ccb) {
                if( (Ccb->FileObject->Flags & FO_FILE_MODIFIED) &&
                   !(Ccb->CCBFlags & UDF_CCB_WRITE_TIME_SET)) {
                    if(Fcb->Vcb->CompatFlags & UDF_VCB_IC_UPDATE_MODIFY_TIME) {
                        LONGLONG NtTime;
                        KeQuerySystemTime((PLARGE_INTEGER)&NtTime);
                        UDFSetFileXTime(Fcb->FileInfo, NULL, NULL, NULL, &NtTime);
                        Fcb->NTRequiredFCB->LastWriteTime.QuadPart = NtTime;
                    }
                    SetArchive = TRUE;
                    Ccb->FileObject->Flags &= ~FO_FILE_MODIFIED;
                }
                if(Ccb->FileObject->Flags & FO_FILE_SIZE_CHANGED) {
                    LONGLONG ASize = UDFGetFileAllocationSize(Fcb->Vcb, Fcb->FileInfo);
                    UDFSetFileSizeInDirNdx(Fcb->Vcb, Fcb->FileInfo, &ASize);
                    Ccb->FileObject->Flags &= ~FO_FILE_SIZE_CHANGED;
                }
            }
#endif //UDF_READ_ONLY_BUILD
        }
    } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
        BrutePoint();
    } _SEH2_END;

    _SEH2_TRY {
#ifndef UDF_READ_ONLY_BUILD
        if(SetArchive &&
           (Fcb->Vcb->CompatFlags & UDF_VCB_IC_UPDATE_ARCH_BIT)) {
            ULONG Attr;
            PDIR_INDEX_ITEM DirNdx;
            DirNdx = UDFDirIndex(UDFGetDirIndexByFileInfo(Fcb->FileInfo), Fcb->FileInfo->Index);
            // Archive bit
            Attr = UDFAttributesToNT(DirNdx, Fcb->FileInfo->Dloc->FileEntry);
            if(!(Attr & FILE_ATTRIBUTE_ARCHIVE))
                UDFAttributesToUDF(DirNdx, Fcb->FileInfo->Dloc->FileEntry, Attr | FILE_ATTRIBUTE_ARCHIVE);
        }
#endif //UDF_READ_ONLY_BUILD
        UDFFlushFile__( Fcb->Vcb, Fcb->FileInfo, FlushFlags);
    } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
        BrutePoint();
    } _SEH2_END;

/*    if(PurgeCache) {
        _SEH2_TRY {
            MmPrint(("    CcPurgeCacheSection()\n"));
            CcPurgeCacheSection( &(Fcb->NTRequiredFCB->SectionObject), NULL, 0, FALSE );
        } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
            BrutePoint();
        } _SEH2_END;
    }*/

    return ret_val;
} // end UDFFlushAFile()

/*************************************************************************
*
* Function: UDFFlushADirectory()
*
* Description:
*   Tell the Cache Manager to perform a flush for all files
*   in current directory & all subdirectories and flush all metadata
*
* Expected Interrupt Level (for execution) :
*
*  IRQL_PASSIVE_LEVEL
*
* Return Value: None
*
*************************************************************************/
ULONG
UDFFlushADirectory(
    IN PVCB                Vcb,
    IN PUDF_FILE_INFO      FI,
    OUT PIO_STATUS_BLOCK   PtrIoStatus,
    IN ULONG               FlushFlags
    )
{
    UDFPrint(("UDFFlushADirectory: \n"));
//    PDIR_INDEX_HDR hDI;
    PDIR_INDEX_ITEM DI;
//    BOOLEAN Referenced = FALSE;
    ULONG ret_val = 0;

    if(Vcb->VCBFlags & UDF_VCB_FLAGS_RAW_DISK)
        return 0;

    if(!FI || !FI->Dloc || !FI->Dloc->DirIndex) goto SkipFlushDir;
//    hDI = FI->Dloc->DirIndex;

    // Flush Security if required
    _SEH2_TRY {
        UDFWriteSecurity(Vcb, FI->Fcb, &(FI->Fcb->NTRequiredFCB->SecurityDesc));
    } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
        BrutePoint();
    } _SEH2_END;
    // Flush SDir if any
    _SEH2_TRY {
        if(UDFHasAStreamDir(FI) &&
           FI->Dloc->SDirInfo &&
           !UDFIsSDirDeleted(FI->Dloc->SDirInfo) ) {
            ret_val |=
                UDFFlushADirectory(Vcb, FI->Dloc->SDirInfo, PtrIoStatus, FlushFlags);
        }
    } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
        BrutePoint();
    } _SEH2_END;

    // Flush Dir Tree
    _SEH2_TRY {
        UDF_DIR_SCAN_CONTEXT ScanContext;
        PUDF_FILE_INFO      tempFI;

        if(UDFDirIndexInitScan(FI, &ScanContext, 2)) {
            while((DI = UDFDirIndexScan(&ScanContext, &tempFI))) {
                // Flush Dir entry
                _SEH2_TRY {
                    if(!tempFI) continue;
                    if(UDFIsADirectory(tempFI)) {<--- Assuming that condition 'tempFI' is not redundant
                        UDFFlushADirectory(Vcb, tempFI, PtrIoStatus, FlushFlags);
                    } else {
                        UDFFlushAFile(tempFI->Fcb, NULL, PtrIoStatus, FlushFlags);<--- Null pointer dereference
                    }
                } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
                    BrutePoint();
                } _SEH2_END;
                if(UDFFlushIsBreaking(Vcb, FlushFlags)) {
                    ret_val |= UDF_FLUSH_FLAGS_INTERRUPTED;
                    break;
                }
            }
        }
    } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
        BrutePoint();
    } _SEH2_END;
SkipFlushDir:
    // Flush Dir
    _SEH2_TRY {
        UDFFlushFile__( Vcb, FI, FlushFlags );
    } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
        BrutePoint();
    } _SEH2_END;

    return ret_val;
} // end UDFFlushADirectory()

/*************************************************************************
*
* Function: UDFFlushLogicalVolume()
*
* Description:
*   Flush everything beginning from root directory.
*   Vcb must be previously acquired exclusively.
*
* Expected Interrupt Level (for execution) :
*
*  IRQL_PASSIVE_LEVEL
*
* Return Value: None
*
*************************************************************************/
ULONG
UDFFlushLogicalVolume(
    IN PtrUDFIrpContext PtrIrpContext,
    IN PIRP             Irp,
    IN PVCB             Vcb,
    IN ULONG            FlushFlags
    )
{
    ULONG ret_val = 0;
#ifndef UDF_READ_ONLY_BUILD
    IO_STATUS_BLOCK IoStatus;

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

    _SEH2_TRY {
        if(Vcb->VCBFlags & (UDF_VCB_FLAGS_RAW_DISK/* |
                            UDF_VCB_FLAGS_MEDIA_READ_ONLY*/))
            return 0;
        if(Vcb->VCBFlags & UDF_VCB_FLAGS_VOLUME_READ_ONLY)
            return 0;
        if(!(Vcb->VCBFlags & UDF_VCB_FLAGS_VOLUME_MOUNTED))
            return 0;

        // NOTE: This function may also be invoked internally as part of
        // processing a shutdown request.
        ASSERT(Vcb->RootDirFCB);
        ret_val |= UDFFlushADirectory(Vcb, Vcb->RootDirFCB->FileInfo, &IoStatus, FlushFlags);

//        if(UDFFlushIsBreaking(Vcb, FlushFlags))
//            return;
        // flush internal cache
        if(FlushFlags & UDF_FLUSH_FLAGS_LITE) {
            UDFPrint(("  Lite flush, keep Modified=%d.\n", Vcb->Modified));
        } else {
            if(Vcb->VerifyOnWrite) {
                UDFPrint(("UDF: Flushing cache for verify\n"));
                //WCacheFlushAll__(&(Vcb->FastCache), Vcb);
                WCacheFlushBlocks__(&(Vcb->FastCache), Vcb, 0, Vcb->LastLBA);
                UDFVFlush(Vcb);
            }
            // umount (this is internal operation, NT will "dismount" volume later)
            UDFUmount__(Vcb);

            UDFPreClrModified(Vcb);
            WCacheFlushAll__(&(Vcb->FastCache), Vcb);
            UDFClrModified(Vcb);
        }

    } _SEH2_FINALLY {
        ;
    } _SEH2_END;
#endif //UDF_READ_ONLY_BUILD

    return ret_val;
} // end UDFFlushLogicalVolume()


/*************************************************************************
*
* Function: UDFFlushCompletion()
*
* Description:
*   Eat up any bad errors.
*
* Expected Interrupt Level (for execution) :
*
*  IRQL_PASSIVE_LEVEL
*
* Return Value: None
*
*************************************************************************/
NTSTATUS
NTAPI
UDFFlushCompletion(
    PDEVICE_OBJECT  PtrDeviceObject,
    PIRP            Irp,
    PVOID           Context
    )
{
//    NTSTATUS        RC = STATUS_SUCCESS;

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

    if (Irp->PendingReturned) {
        IoMarkIrpPending(Irp);
    }

    if (Irp->IoStatus.Status == STATUS_INVALID_DEVICE_REQUEST) {
        // cannot do much here, can we?
        Irp->IoStatus.Status = STATUS_SUCCESS;
    }

    return(STATUS_SUCCESS);
} // end UDFFlushCompletion()


/*
  Check if we should break FlushTree process
 */
BOOLEAN
UDFFlushIsBreaking(
    IN PVCB         Vcb,
    IN ULONG        FlushFlags
    )
{
    BOOLEAN ret_val = FALSE;
//    if(!(FlushFlags & UDF_FLUSH_FLAGS_BREAKABLE))
        return FALSE;
    UDFAcquireResourceExclusive(&(Vcb->FlushResource),TRUE);<--- Statements following return, break, continue, goto or throw will never be executed.
    ret_val = (Vcb->VCBFlags & UDF_VCB_FLAGS_FLUSH_BREAK_REQ) ? TRUE : FALSE;
    Vcb->VCBFlags &= ~UDF_VCB_FLAGS_FLUSH_BREAK_REQ;
    UDFReleaseResource(&(Vcb->FlushResource));
    return ret_val;
} // end UDFFlushIsBreaking()

/*
  Signal FlushTree break request. Note, this is
  treated as recommendation only
 */
VOID
UDFFlushTryBreak(
    IN PVCB         Vcb
    )
{
    UDFAcquireResourceExclusive(&(Vcb->FlushResource),TRUE);
    Vcb->VCBFlags |= UDF_VCB_FLAGS_FLUSH_BREAK_REQ;
    UDFReleaseResource(&(Vcb->FlushResource));
} // end UDFFlushTryBreak()