none
ZwCreateFile for Asynchronous handle

    Question

  • Hi,

    I'm trying to get a asynchronous handle to a file in kernel mode.
    When the calling function, EvtDevicePrepareHardware (running in
    PASSIVE_LEVEL), that opens the handle returns, it causes a bugcheck in
    ntfs.  I can open a synchronous handle just fine (by adding
    FILE_SYNCHRONOUS_IO_NONALERT).  Here's the call.

        status =
            ZwCreateFile(&FileHandle,
                GENERIC_READ | GENERIC_WRITE,
                &ObjectAttributes,
                &IoStatus,
                NULL,
                FILE_ATTRIBUTE_NORMAL,
                    FILE_SHARE_READ |
                    FILE_SHARE_DELETE | FILE_SHARE_WRITE,
                FILE_OPEN,
                FILE_NON_DIRECTORY_FILE |
                    FILE_RANDOM_ACCESS
                    | FILE_NO_INTERMEDIATE_BUFFERING,
                NULL,
                0);

    Regards,
    CG

     

    Friday, February 11, 2011 11:39 PM

Answers

  • Don't use GENERIC_READ | GENERIC_WRITE for files these map into FILE_GENERIC_READ | FILE_GENERIC_WRITE which maps into:

    STANDARD_RIGHTS_READ | FILE_READ_DATA | FILE_READ_ATTRIBUTES | FILE_READ_EA | SYNCHRONIZE

    STANDARD_RIGHTS_WRITE | FILE_WRITE_DATA | FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA | FILE_APPEND_DATA | SYNCHRONIZE

    Since FILE_NO_INTERMEDIATE_BUFFERING is incompatibe with FILE_APPEND_DATA you have a problem.  Also using the SYNCHRONIZE is not a great idea for async I/O.

     


    Don Burn (MVP, Windows DKD) Windows Filesystem and Driver Consulting Website: http://www.windrvr.com Blog: http://msmvps.com/blogs/WinDrvr
    • Marked as answer by cgguy Monday, February 14, 2011 9:59 PM
    Saturday, February 12, 2011 1:13 AM

All replies

  • How did you set ObjectAttributes?
    Gary G. Little NanoTelesis Systems, LLC
    Saturday, February 12, 2011 1:12 AM
  • Don't use GENERIC_READ | GENERIC_WRITE for files these map into FILE_GENERIC_READ | FILE_GENERIC_WRITE which maps into:

    STANDARD_RIGHTS_READ | FILE_READ_DATA | FILE_READ_ATTRIBUTES | FILE_READ_EA | SYNCHRONIZE

    STANDARD_RIGHTS_WRITE | FILE_WRITE_DATA | FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA | FILE_APPEND_DATA | SYNCHRONIZE

    Since FILE_NO_INTERMEDIATE_BUFFERING is incompatibe with FILE_APPEND_DATA you have a problem.  Also using the SYNCHRONIZE is not a great idea for async I/O.

     


    Don Burn (MVP, Windows DKD) Windows Filesystem and Driver Consulting Website: http://www.windrvr.com Blog: http://msmvps.com/blogs/WinDrvr
    • Marked as answer by cgguy Monday, February 14, 2011 9:59 PM
    Saturday, February 12, 2011 1:13 AM
  • All of that is true, however the complaint is a bugcheck, and ntfs does not use bugcheck as an error return from createfile, so I suspect these problems are irrelevant.

     

    What bugcheck?

    Where is the output from analyze -v?


    Mark Roddy Windows Driver and OS consultant www.hollistech.com
    Saturday, February 12, 2011 3:13 AM
    Moderator
  • Hi Donald,

    DOH!  Didn't notice there was so much behind those flags.

    I changed it to (including InitializeObjectAttributes):

        InitializeObjectAttributes(
            &ObjectAttributes,
            &pDeviceExtension->DiskRegInfo.BackingFile,
            OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
            NULL,
            NULL
            );

        status =
            ZwCreateFile(&FileHandle,
                FILE_READ_DATA | FILE_WRITE_DATA,
                &ObjectAttributes,
                &IoStatus,
                NULL,
                FILE_ATTRIBUTE_NORMAL,
                    FILE_SHARE_READ |
                    FILE_SHARE_DELETE | FILE_SHARE_WRITE,
                FILE_OPEN,
                FILE_NON_DIRECTORY_FILE |
                    FILE_RANDOM_ACCESS
                    | FILE_NO_INTERMEDIATE_BUFFERING,
                NULL,
                0);

    Seems to be working.

    Thanks,

    CG

    Monday, February 14, 2011 10:02 PM