From 0df89bb1e84a9433418cb8045adbb1ca9f3004fb Mon Sep 17 00:00:00 2001 From: Thomas Faber Date: Sun, 29 Sep 2013 20:52:23 +0000 Subject: [PATCH] [NTOS:CC] - Simplify cache segment range checks - Remove unused function CcRosFreeCacheSegment - Minor style fixes CORE-7491 svn path=/trunk/; revision=60456 --- reactos/ntoskrnl/cc/copy.c | 45 +++++++++----------- reactos/ntoskrnl/cc/fs.c | 4 +- reactos/ntoskrnl/cc/pin.c | 2 +- reactos/ntoskrnl/cc/view.c | 59 ++++++-------------------- reactos/ntoskrnl/include/internal/cc.h | 32 +++++++++++--- 5 files changed, 63 insertions(+), 79 deletions(-) diff --git a/reactos/ntoskrnl/cc/copy.c b/reactos/ntoskrnl/cc/copy.c index 8f958140fe0..749052c42b6 100644 --- a/reactos/ntoskrnl/cc/copy.c +++ b/reactos/ntoskrnl/cc/copy.c @@ -355,13 +355,16 @@ CcCopyRead ( if (!Wait) { KeAcquireSpinLock(&Bcb->BcbLock, &oldirql); + /* FIXME: this loop doesn't take into account areas that don't have + * a segment in the list yet */ current_entry = Bcb->BcbSegmentListHead.Flink; while (current_entry != &Bcb->BcbSegmentListHead) { current = CONTAINING_RECORD(current_entry, CACHE_SEGMENT, BcbSegmentListEntry); - if (!current->Valid && current->FileOffset < ReadOffset + Length - && current->FileOffset + Bcb->CacheSegmentSize > ReadOffset) + if (!current->Valid && + DoSegmentsIntersect(current->FileOffset, Bcb->CacheSegmentSize, + ReadOffset, Length)) { KeReleaseSpinLock(&Bcb->BcbLock, oldirql); IoStatus->Status = STATUS_UNSUCCESSFUL; @@ -466,23 +469,20 @@ CcCopyWrite ( { /* testing, if the requested datas are available */ KeAcquireSpinLock(&Bcb->BcbLock, &oldirql); + /* FIXME: this loop doesn't take into account areas that don't have + * a segment in the list yet */ current_entry = Bcb->BcbSegmentListHead.Flink; while (current_entry != &Bcb->BcbSegmentListHead) { CacheSeg = CONTAINING_RECORD(current_entry, CACHE_SEGMENT, BcbSegmentListEntry); - if (!CacheSeg->Valid) + if (!CacheSeg->Valid && + DoSegmentsIntersect(CacheSeg->FileOffset, Bcb->CacheSegmentSize, + WriteOffset, Length)) { - if (((WriteOffset >= CacheSeg->FileOffset) && - (WriteOffset < CacheSeg->FileOffset + Bcb->CacheSegmentSize)) - || ((WriteOffset + Length > CacheSeg->FileOffset) && - (WriteOffset + Length <= CacheSeg->FileOffset + - Bcb->CacheSegmentSize))) - { - KeReleaseSpinLock(&Bcb->BcbLock, oldirql); - /* datas not available */ - return FALSE; - } + KeReleaseSpinLock(&Bcb->BcbLock, oldirql); + /* datas not available */ + return FALSE; } current_entry = current_entry->Flink; } @@ -687,23 +687,20 @@ CcZeroData ( { /* testing, if the requested datas are available */ KeAcquireSpinLock(&Bcb->BcbLock, &oldirql); + /* FIXME: this loop doesn't take into account areas that don't have + * a segment in the list yet */ current_entry = Bcb->BcbSegmentListHead.Flink; while (current_entry != &Bcb->BcbSegmentListHead) { CacheSeg = CONTAINING_RECORD(current_entry, CACHE_SEGMENT, BcbSegmentListEntry); - if (!CacheSeg->Valid) + if (!CacheSeg->Valid && + DoSegmentsIntersect(CacheSeg->FileOffset, Bcb->CacheSegmentSize, + WriteOffset.u.LowPart, Length)) { - if (((WriteOffset.u.LowPart >= CacheSeg->FileOffset) && - (WriteOffset.u.LowPart < CacheSeg->FileOffset + Bcb->CacheSegmentSize)) - || ((WriteOffset.u.LowPart + Length > CacheSeg->FileOffset) && - (WriteOffset.u.LowPart + Length <= - CacheSeg->FileOffset + Bcb->CacheSegmentSize))) - { - KeReleaseSpinLock(&Bcb->BcbLock, oldirql); - /* datas not available */ - return FALSE; - } + KeReleaseSpinLock(&Bcb->BcbLock, oldirql); + /* datas not available */ + return FALSE; } current_entry = current_entry->Flink; } diff --git a/reactos/ntoskrnl/cc/fs.c b/reactos/ntoskrnl/cc/fs.c index fd650bd2ddd..57d1f7ef354 100644 --- a/reactos/ntoskrnl/cc/fs.c +++ b/reactos/ntoskrnl/cc/fs.c @@ -159,7 +159,9 @@ CcSetFileSizes ( current_entry = Bcb->BcbSegmentListHead.Flink; while (current_entry != &Bcb->BcbSegmentListHead) { - current = CONTAINING_RECORD(current_entry, CACHE_SEGMENT, BcbSegmentListEntry); + current = CONTAINING_RECORD(current_entry, + CACHE_SEGMENT, + BcbSegmentListEntry); current_entry = current_entry->Flink; if (current->FileOffset > FileSizes->AllocationSize.QuadPart || (current->FileOffset == 0 && FileSizes->AllocationSize.QuadPart == 0)) diff --git a/reactos/ntoskrnl/cc/pin.c b/reactos/ntoskrnl/cc/pin.c index ca803d2f18d..40b87617dee 100644 --- a/reactos/ntoskrnl/cc/pin.c +++ b/reactos/ntoskrnl/cc/pin.c @@ -261,7 +261,7 @@ CcUnpinRepinnedBcb ( { IoStatus->Status = STATUS_SUCCESS; } - KeReleaseMutex(&iBcb->CacheSegment->Mutex, 0); + KeReleaseMutex(&iBcb->CacheSegment->Mutex, FALSE); } else { diff --git a/reactos/ntoskrnl/cc/view.c b/reactos/ntoskrnl/cc/view.c index 3b5ea8bf22b..19e90eef112 100644 --- a/reactos/ntoskrnl/cc/view.c +++ b/reactos/ntoskrnl/cc/view.c @@ -236,7 +236,7 @@ CcRosFlushDirtyPages ( /* One reference is added above */ if (current->ReferenceCount > 2) { - KeReleaseMutex(¤t->Mutex, 0); + KeReleaseMutex(¤t->Mutex, FALSE); current->Bcb->Callbacks->ReleaseFromLazyWrite( current->Bcb->LazyWriteContext); CcRosCacheSegmentDecRefCount(current); @@ -249,7 +249,7 @@ CcRosFlushDirtyPages ( Status = CcRosFlushCacheSegment(current); - KeReleaseMutex(¤t->Mutex, 0); + KeReleaseMutex(¤t->Mutex, FALSE); current->Bcb->Callbacks->ReleaseFromLazyWrite( current->Bcb->LazyWriteContext); @@ -446,7 +446,7 @@ CcRosReleaseCacheSegment ( KeReleaseSpinLock(&Bcb->BcbLock, oldIrql); KeReleaseGuardedMutex(&ViewLock); - KeReleaseMutex(&CacheSeg->Mutex, 0); + KeReleaseMutex(&CacheSeg->Mutex, FALSE); return(STATUS_SUCCESS); } @@ -474,8 +474,8 @@ CcRosLookupCacheSegment ( { current = CONTAINING_RECORD(current_entry, CACHE_SEGMENT, BcbSegmentListEntry); - if ((current->FileOffset <= FileOffset) && - ((current->FileOffset + Bcb->CacheSegmentSize) > FileOffset)) + if (IsPointInSegment(current->FileOffset, Bcb->CacheSegmentSize, + FileOffset)) { CcRosCacheSegmentIncRefCount(current); KeReleaseSpinLock(&Bcb->BcbLock, oldIrql); @@ -485,7 +485,7 @@ CcRosLookupCacheSegment ( KernelMode, FALSE, NULL); - return(current); + return current; } current_entry = current_entry->Flink; } @@ -493,7 +493,7 @@ CcRosLookupCacheSegment ( KeReleaseSpinLock(&Bcb->BcbLock, oldIrql); KeReleaseGuardedMutex(&ViewLock); - return(NULL); + return NULL; } NTSTATUS @@ -536,7 +536,7 @@ CcRosMarkDirtyCacheSegment ( KeReleaseSpinLock(&Bcb->BcbLock, oldIrql); KeReleaseGuardedMutex(&ViewLock); - KeReleaseMutex(&CacheSeg->Mutex, 0); + KeReleaseMutex(&CacheSeg->Mutex, FALSE); return(STATUS_SUCCESS); } @@ -589,7 +589,7 @@ CcRosUnmapCacheSegment ( KeReleaseSpinLock(&Bcb->BcbLock, oldIrql); KeReleaseGuardedMutex(&ViewLock); - KeReleaseMutex(&CacheSeg->Mutex, 0); + KeReleaseMutex(&CacheSeg->Mutex, FALSE); return(STATUS_SUCCESS); } @@ -659,8 +659,8 @@ CcRosCreateCacheSegment ( { current = CONTAINING_RECORD(current_entry, CACHE_SEGMENT, BcbSegmentListEntry); - if (current->FileOffset <= FileOffset && - (current->FileOffset + Bcb->CacheSegmentSize) > FileOffset) + if (IsPointInSegment(current->FileOffset, Bcb->CacheSegmentSize, + FileOffset)) { CcRosCacheSegmentIncRefCount(current); KeReleaseSpinLock(&Bcb->BcbLock, oldIrql); @@ -673,7 +673,7 @@ CcRosCreateCacheSegment ( current ); } #endif - KeReleaseMutex(&(*CacheSeg)->Mutex, 0); + KeReleaseMutex(&(*CacheSeg)->Mutex, FALSE); KeReleaseGuardedMutex(&ViewLock); ExFreeToNPagedLookasideList(&CacheSegLookasideList, *CacheSeg); *CacheSeg = current; @@ -996,38 +996,6 @@ CcRosInternalFreeCacheSegment ( return(STATUS_SUCCESS); } -NTSTATUS -NTAPI -CcRosFreeCacheSegment ( - PBCB Bcb, - PCACHE_SEGMENT CacheSeg) -{ - NTSTATUS Status; - KIRQL oldIrql; - - ASSERT(Bcb); - - DPRINT("CcRosFreeCacheSegment(Bcb 0x%p, CacheSeg 0x%p)\n", - Bcb, CacheSeg); - - KeAcquireGuardedMutex(&ViewLock); - KeAcquireSpinLock(&Bcb->BcbLock, &oldIrql); - RemoveEntryList(&CacheSeg->BcbSegmentListEntry); - RemoveEntryList(&CacheSeg->CacheSegmentListEntry); - RemoveEntryList(&CacheSeg->CacheSegmentLRUListEntry); - if (CacheSeg->Dirty) - { - RemoveEntryList(&CacheSeg->DirtySegmentListEntry); - DirtyPageCount -= Bcb->CacheSegmentSize / PAGE_SIZE; - - } - KeReleaseSpinLock(&Bcb->BcbLock, oldIrql); - KeReleaseGuardedMutex(&ViewLock); - - Status = CcRosInternalFreeCacheSegment(CacheSeg); - return(Status); -} - /* * @implemented */ @@ -1081,7 +1049,7 @@ CcFlushCache ( IoStatus->Status = Status; } } - KeReleaseMutex(¤t->Mutex, 0); + KeReleaseMutex(¤t->Mutex, FALSE); KeAcquireGuardedMutex(&ViewLock); KeAcquireSpinLock(&Bcb->BcbLock, &oldIrql); @@ -1148,7 +1116,6 @@ CcRosDeleteFileCache ( */ InitializeListHead(&FreeList); KeAcquireSpinLock(&Bcb->BcbLock, &oldIrql); - current_entry = Bcb->BcbSegmentListHead.Flink; while (!IsListEmpty(&Bcb->BcbSegmentListHead)) { current_entry = RemoveTailList(&Bcb->BcbSegmentListHead); diff --git a/reactos/ntoskrnl/include/internal/cc.h b/reactos/ntoskrnl/include/internal/cc.h index 17d2cacfbd7..60d86c636ea 100644 --- a/reactos/ntoskrnl/include/internal/cc.h +++ b/reactos/ntoskrnl/include/internal/cc.h @@ -203,13 +203,6 @@ VOID NTAPI CcInitView(VOID); -NTSTATUS -NTAPI -CcRosFreeCacheSegment( - PBCB, - PCACHE_SEGMENT -); - NTSTATUS NTAPI ReadCacheSegment(PCACHE_SEGMENT CacheSeg); @@ -315,3 +308,28 @@ CcRosReleaseFileCache( NTSTATUS NTAPI CcTryToInitializeFileCache(PFILE_OBJECT FileObject); + +FORCEINLINE +BOOLEAN +DoSegmentsIntersect( + _In_ ULONG Offset1, + _In_ ULONG Length1, + _In_ ULONG Offset2, + _In_ ULONG Length2) +{ + if (Offset1 + Length1 <= Offset2) + return FALSE; + if (Offset2 + Length2 <= Offset1) + return FALSE; + return TRUE; +} + +FORCEINLINE +BOOLEAN +IsPointInSegment( + _In_ ULONG Offset1, + _In_ ULONG Length1, + _In_ ULONG Point) +{ + return DoSegmentsIntersect(Offset1, Length1, Point, 1); +}