https://git.reactos.org/?p=reactos.git;a=commitdiff;h=e19e907a2c40fa90a238e78b146b5dfc3fe715a3

commit e19e907a2c40fa90a238e78b146b5dfc3fe715a3
Author:     Pierre Schweitzer <[email protected]>
AuthorDate: Thu Oct 4 10:42:13 2018 +0200
Commit:     Pierre Schweitzer <[email protected]>
CommitDate: Thu Oct 4 10:45:10 2018 +0200

    [NTOSKRNL] Quickly check for alignment in NtRead/WriteFile
    
    This quick check based on bits and operation is for 2^ based
    sector sizes (most of the cases) and will perform faster than
    the modulo operation which is still used in fallback in case
    the sector size wouldn't be a power of 2.
---
 ntoskrnl/io/iomgr/iofunc.c | 32 ++++++++++++++++++++++----------
 1 file changed, 22 insertions(+), 10 deletions(-)

diff --git a/ntoskrnl/io/iomgr/iofunc.c b/ntoskrnl/io/iomgr/iofunc.c
index 01a956505f..61fd88f8fd 100644
--- a/ntoskrnl/io/iomgr/iofunc.c
+++ b/ntoskrnl/io/iomgr/iofunc.c
@@ -2612,13 +2612,19 @@ NtReadFile(IN HANDLE FileHandle,
             /* Perform additional checks for non-cached file access */
             if (FileObject->Flags & FO_NO_INTERMEDIATE_BUFFERING)
             {
-                /* Fail if Length is not sector size aligned */
+                /* Fail if Length is not sector size aligned
+                 * Perform a quick check for 2^ sector sizes
+                 * If it fails, try a more standard way
+                 */
                 if ((DeviceObject->SectorSize != 0) &&
-                    (Length % DeviceObject->SectorSize != 0))
+                    ((DeviceObject->SectorSize - 1) & Length) != 0)
                 {
-                    /* Release the file object and and fail */
-                    ObDereferenceObject(FileObject);
-                    return STATUS_INVALID_PARAMETER;
+                    if (Length % DeviceObject->SectorSize != 0)
+                    {
+                        /* Release the file object and and fail */
+                        ObDereferenceObject(FileObject);
+                        return STATUS_INVALID_PARAMETER;
+                    }
                 }
 
                 /* Fail if buffer doesn't match alignment requirements */
@@ -3649,13 +3655,19 @@ NtWriteFile(IN HANDLE FileHandle,
             /* Perform additional checks for non-cached file access */
             if (FileObject->Flags & FO_NO_INTERMEDIATE_BUFFERING)
             {
-                /* Fail if Length is not sector size aligned */
+                /* Fail if Length is not sector size aligned
+                 * Perform a quick check for 2^ sector sizes
+                 * If it fails, try a more standard way
+                 */
                 if ((DeviceObject->SectorSize != 0) &&
-                    (Length % DeviceObject->SectorSize != 0))
+                    ((DeviceObject->SectorSize - 1) & Length) != 0)
                 {
-                    /* Release the file object and and fail */
-                    ObDereferenceObject(FileObject);
-                    return STATUS_INVALID_PARAMETER;
+                    if (Length % DeviceObject->SectorSize != 0)
+                    {
+                        /* Release the file object and and fail */
+                        ObDereferenceObject(FileObject);
+                        return STATUS_INVALID_PARAMETER;
+                    }
                 }
 
                 /* Fail if buffer doesn't match alignment requirements */

Reply via email to