Windows: Fcb sectionObjectResource
[openafs.git] / src / WINNT / afsrdr / kernel / lib / AFSWrite.cpp
index 12ff9e6..f0d3f37 100644 (file)
@@ -81,7 +81,7 @@ AFSWrite( IN PDEVICE_OBJECT LibDeviceObject,
 
         ntStatus = AFSCommonWrite( AFSRDRDeviceObject, Irp, NULL);
     }
-    __except( AFSExceptionFilter( GetExceptionCode(), GetExceptionInformation()) )
+    __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()) )
     {
 
         ntStatus = STATUS_INSUFFICIENT_RESOURCES;
@@ -108,15 +108,15 @@ AFSCommonWrite( IN PDEVICE_OBJECT DeviceObject,
     BOOLEAN            bPagingIo = FALSE;
     BOOLEAN            bNonCachedIo = FALSE;
     BOOLEAN            bReleaseMain = FALSE;
+    BOOLEAN            bReleaseSectionObject = FALSE;
     BOOLEAN            bReleasePaging = FALSE;
     BOOLEAN            bExtendingWrite = FALSE;
     BOOLEAN            bCompleteIrp = TRUE;
-    BOOLEAN            bForceFlush = FALSE;
     BOOLEAN            bLockOK;
-    BOOLEAN            bMapped = TRUE;
     HANDLE             hCallingUser = OnBehalfOf;
     ULONG              ulExtensionLength = 0;
     BOOLEAN            bRetry = FALSE;
+    ULONGLONG          ullProcessId = (ULONGLONG)PsGetCurrentProcessId();
 
     pIrpSp = IoGetCurrentIrpStackLocation( Irp);
 
@@ -333,62 +333,88 @@ AFSCommonWrite( IN PDEVICE_OBJECT DeviceObject,
         if( NULL != pFileObject->SectionObjectPointer->DataSectionObject && !bPagingIo && bNonCachedIo)
         {
             bNonCachedIo = FALSE;
-            bForceFlush = TRUE;
         }
 
-        if( (!bPagingIo && !bNonCachedIo))
-        {
+        if ( !bNonCachedIo) {
 
-            if( pFileObject->PrivateCacheMap == NULL)
+            if( !bPagingIo)
             {
 
-                __try
+                if( pFileObject->PrivateCacheMap == NULL)
                 {
 
-                    AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
+                    AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
                                   AFS_TRACE_LEVEL_VERBOSE,
-                                  "AFSCommonWrite Initialize caching on Fcb %08lX FileObject %08lX\n",
-                                  pFcb,
-                                  pFileObject);
+                                  "AFSCommonWrite Acquiring Fcb SectionObject lock %08lX EXCL %08lX\n",
+                                  &pNPFcb->SectionObjectResource,
+                                  PsGetCurrentThread());
 
-                    CcInitializeCacheMap( pFileObject,
-                                          (PCC_FILE_SIZES)&pFcb->Header.AllocationSize,
-                                          FALSE,
-                                          AFSLibCacheManagerCallbacks,
-                                          pFcb);
+                    AFSAcquireExcl( &pNPFcb->SectionObjectResource,
+                                    TRUE);
 
-                    CcSetReadAheadGranularity( pFileObject,
-                                               READ_AHEAD_GRANULARITY);
+                    bReleaseSectionObject = TRUE;
 
-                }
-                __except( EXCEPTION_EXECUTE_HANDLER)
-                {
+                    __try
+                    {
 
-                    ntStatus = GetExceptionCode();
+                        AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
+                                      AFS_TRACE_LEVEL_VERBOSE,
+                                      "AFSCommonWrite Initialize caching on Fcb %08lX FileObject %08lX\n",
+                                      pFcb,
+                                      pFileObject);
 
-                    AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
-                                  AFS_TRACE_LEVEL_ERROR,
-                                  "AFSCommonWrite (%08lX) Exception thrown while initializing cache map Status %08lX\n",
-                                  Irp,
-                                  ntStatus);
-                }
+                        CcInitializeCacheMap( pFileObject,
+                                              (PCC_FILE_SIZES)&pFcb->Header.AllocationSize,
+                                              FALSE,
+                                              AFSLibCacheManagerCallbacks,
+                                              pFcb);
 
-                if( !NT_SUCCESS( ntStatus))
-                {
+                        CcSetReadAheadGranularity( pFileObject,
+                                                   pDeviceExt->Specific.RDR.MaximumRPCLength);
 
-                    try_return( ntStatus);
+                        CcSetDirtyPageThreshold( pFileObject,
+                                                 AFS_DIRTY_CHUNK_THRESHOLD * pDeviceExt->Specific.RDR.MaximumRPCLength);
+                    }
+                    __except( EXCEPTION_EXECUTE_HANDLER)
+                    {
+
+                        ntStatus = GetExceptionCode();
+
+                        AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
+                                      AFS_TRACE_LEVEL_ERROR,
+                                      "AFSCommonWrite (%08lX) Exception thrown while initializing cache map Status %08lX\n",
+                                      Irp,
+                                      ntStatus);
+                    }
+
+                    AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
+                                  AFS_TRACE_LEVEL_VERBOSE,
+                                  "AFSCommonWrite Releasing Fcb SectionObject lock %08lX EXCL %08lX\n",
+                                  &pNPFcb->SectionObjectResource,
+                                  PsGetCurrentThread());
+
+                    AFSReleaseResource( &pNPFcb->SectionObjectResource);
+
+                    bReleaseSectionObject = FALSE;
+
+
+                    if( !NT_SUCCESS( ntStatus))
+                    {
+
+                        try_return( ntStatus);
+                    }
                 }
             }
-        }
 
-        while (!bNonCachedIo && !CcCanIWrite( pFileObject,
-                                              ulByteCount,
-                                              FALSE,
-                                              bRetry))
-        {
-            static const LONGLONG llWriteDelay = (LONGLONG)-100000;
-            bRetry = TRUE;
-            KeDelayExecutionThread(KernelMode, FALSE, (PLARGE_INTEGER)&llWriteDelay);
+            while (!CcCanIWrite( pFileObject,
+                                 ulByteCount,
+                                 FALSE,
+                                 bRetry))
+            {
+                static const LONGLONG llWriteDelay = (LONGLONG)-100000;
+                bRetry = TRUE;
+                KeDelayExecutionThread(KernelMode, FALSE, (PLARGE_INTEGER)&llWriteDelay);
+            }
         }
 
         //
@@ -397,13 +423,13 @@ AFSCommonWrite( IN PDEVICE_OBJECT DeviceObject,
 
         if( !bPagingIo &&
             ( pFcb->Specific.File.ExtentRequestProcessId == 0 ||
-              ( PsGetCurrentProcessId() != AFSSysProcess &&
-                pFcb->Specific.File.ExtentRequestProcessId != (ULONGLONG)PsGetCurrentProcessId())))
+              ( ullProcessId != (ULONGLONG)AFSSysProcess &&
+                pFcb->Specific.File.ExtentRequestProcessId != ullProcessId)))
         {
 
-            pFcb->Specific.File.ExtentRequestProcessId = (ULONGLONG)PsGetCurrentProcessId();
+            pFcb->Specific.File.ExtentRequestProcessId = ullProcessId;
 
-            if( pFcb->Specific.File.ExtentRequestProcessId == (ULONGLONG)AFSSysProcess)
+            if( ullProcessId == (ULONGLONG)AFSSysProcess)
             {
                 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
                               AFS_TRACE_LEVEL_WARNING,
@@ -411,54 +437,12 @@ AFSCommonWrite( IN PDEVICE_OBJECT DeviceObject,
                               __FUNCTION__,
                               pFcb);
             }
-
-            pFcb->NPFcb->Specific.File.ExtentsRequestStatus = STATUS_SUCCESS;
-        }
-
-        //
-        // We should be ready to go.  So first of all ask for the extents
-        // Provoke a get of the extents - if we need to.
-        //
-
-        /*
-        if( !bPagingIo && !bNonCachedIo)
-        {
-
-            ntStatus = AFSRequestExtentsAsync( pFcb, &liStartingByte, ulByteCount);
-
-            if (!NT_SUCCESS(ntStatus))
-            {
-
-                AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
-                              AFS_TRACE_LEVEL_ERROR,
-                              "AFSCommonWrite (%08lX) Failed to request extents Status %08lX\n",
-                              Irp,
-                              ntStatus);
-
-                try_return( ntStatus );
-            }
-        }
-        */
-
-        //
-        // If they are not mapped and we are the Lazy Writer then just
-        // say "not now"
-        //
-        if (!bMapped && pFcb->Specific.File.LazyWriterThread == PsGetCurrentThread())
-        {
-
-            AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
-                          AFS_TRACE_LEVEL_VERBOSE,
-                          "AFSCommonWrite (%08lX) Failing lazy writer for unmapped request\n",
-                          Irp);
-
-            try_return ( ntStatus = STATUS_FILE_LOCK_CONFLICT);
         }
 
         //
         // Take locks
         //
-        //   - if Paging then we need to nothing (the precalls will
+        //   - if Paging then we need to do nothing (the precalls will
         //     have acquired the paging resource), for clarity we will collect
         //     the paging resource
         //   - If extending Write then take the fileresource EX (EOF will change, Allocation will only move out)
@@ -470,15 +454,6 @@ AFSCommonWrite( IN PDEVICE_OBJECT DeviceObject,
         do
         {
 
-            if( !bPagingIo)
-            {
-
-                bExtendingWrite = (((liStartingByte.QuadPart + ulByteCount) >=
-                                    pFcb->Header.FileSize.QuadPart) ||
-                                   (liStartingByte.LowPart == FILE_WRITE_TO_END_OF_FILE &&
-                                    liStartingByte.HighPart == -1)) ;
-            }
-
             if( bPagingIo)
             {
 
@@ -500,103 +475,139 @@ AFSCommonWrite( IN PDEVICE_OBJECT DeviceObject,
                 //
                 bLockOK = TRUE;
             }
-            else if( bExtendingWrite)
+            else
             {
-                //
-                // Check for lock inversion
-                //
 
-                ASSERT( !ExIsResourceAcquiredLite( &pNPFcb->PagingResource ));
+                bExtendingWrite = (((liStartingByte.QuadPart + ulByteCount) >=
+                                     pFcb->Header.FileSize.QuadPart) ||
+                                    (liStartingByte.LowPart == FILE_WRITE_TO_END_OF_FILE &&
+                                      liStartingByte.HighPart == -1)) ;
+
+                if( bExtendingWrite)
+                {
+                    //
+                    // Check for lock inversion
+                    //
 
-                AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
-                              AFS_TRACE_LEVEL_VERBOSE,
-                              "AFSCommonWrite Acquiring Fcb lock %08lX EXCL %08lX\n",
-                              &pNPFcb->Resource,
-                              PsGetCurrentThread());
+                    ASSERT( !ExIsResourceAcquiredLite( &pNPFcb->PagingResource ));
 
-                AFSAcquireExcl( &pNPFcb->Resource,
-                                TRUE);
+                    AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
+                                  AFS_TRACE_LEVEL_VERBOSE,
+                                  "AFSCommonWrite Acquiring Fcb lock %08lX EXCL %08lX\n",
+                                  &pNPFcb->Resource,
+                                  PsGetCurrentThread());
 
-                if (liStartingByte.LowPart == FILE_WRITE_TO_END_OF_FILE &&
-                    liStartingByte.HighPart == -1)
-                {
-                    if (pFcb->Header.ValidDataLength.QuadPart > pFcb->Header.FileSize.QuadPart)
-                    {
-                        liStartingByte = pFcb->Header.ValidDataLength;
-                    }
-                    else
+                    AFSAcquireExcl( &pNPFcb->Resource,
+                                    TRUE);
+
+                    bReleaseMain = TRUE;
+
+                    AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
+                                  AFS_TRACE_LEVEL_VERBOSE,
+                                  "AFSCommonWrite Acquiring Fcb SectionObject lock %08lX EXCL %08lX\n",
+                                  &pNPFcb->SectionObjectResource,
+                                  PsGetCurrentThread());
+
+                    AFSAcquireExcl( &pNPFcb->SectionObjectResource,
+                                    TRUE);
+
+                    bReleaseSectionObject = TRUE;
+
+                    if (liStartingByte.LowPart == FILE_WRITE_TO_END_OF_FILE &&
+                         liStartingByte.HighPart == -1)
                     {
-                        liStartingByte = pFcb->Header.FileSize;
+                        if (pFcb->Header.ValidDataLength.QuadPart > pFcb->Header.FileSize.QuadPart)
+                        {
+                            liStartingByte = pFcb->Header.ValidDataLength;
+                        }
+                        else
+                        {
+                            liStartingByte = pFcb->Header.FileSize;
+                        }
                     }
+
+                    //
+                    // We have the correct lock - even if we don't end up truncating
+                    //
+                    bLockOK = TRUE;
                 }
-                bReleaseMain = TRUE;
+                else
+                {
+                    ASSERT( !ExIsResourceAcquiredLite( &pNPFcb->PagingResource ));
 
-                //
-                // We have the correct lock - even if we don't end up truncating
-                //
-                bLockOK = TRUE;
-            }
-            else
-            {
-                ASSERT( !ExIsResourceAcquiredLite( &pNPFcb->PagingResource ));
+                    AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
+                                  AFS_TRACE_LEVEL_VERBOSE,
+                                  "AFSCommonWrite Acquiring Fcb lock %08lX SHARED %08lX\n",
+                                  &pNPFcb->Resource,
+                                  PsGetCurrentThread());
 
-                AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
-                              AFS_TRACE_LEVEL_VERBOSE,
-                              "AFSCommonWrite Acquiring Fcb lock %08lX SHARED %08lX\n",
-                              &pNPFcb->Resource,
-                              PsGetCurrentThread());
+                    AFSAcquireShared( &pNPFcb->Resource,
+                                      TRUE);
 
-                AFSAcquireShared( &pNPFcb->Resource,
-                                  TRUE);
+                    bReleaseMain = TRUE;
 
-                bReleaseMain = TRUE;
+                    AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
+                                  AFS_TRACE_LEVEL_VERBOSE,
+                                  "AFSCommonWrite Acquiring Fcb SectionObject lock %08lX SHARED %08lX\n",
+                                  &pNPFcb->SectionObjectResource,
+                                  PsGetCurrentThread());
 
-                //
-                // Have things moved?  Are we extending? If so, the the lock isn't OK
-                //
-                bLockOK = (liStartingByte.QuadPart + ulByteCount) < pFcb->Header.FileSize.QuadPart;
+                    AFSAcquireShared( &pNPFcb->SectionObjectResource,
+                                      TRUE);
 
-                if (!bLockOK)
-                {
-                    AFSReleaseResource( &pNPFcb->Resource);
-                    bReleaseMain = FALSE;
+                    bReleaseSectionObject = TRUE;
+
+                    //
+                    // Have things moved?  Are we extending? If so, the the lock isn't OK
+                    //
+                    bLockOK = (liStartingByte.QuadPart + ulByteCount) < pFcb->Header.FileSize.QuadPart;
+
+                    if (!bLockOK)
+                    {
+                        AFSReleaseResource( &pNPFcb->Resource);
+
+                        bReleaseMain = FALSE;
+                    }
                 }
             }
         }
         while (!bLockOK);
 
-        //
-        // Check the BR locks on the file.
-        //
-
-        if( !bPagingIo &&
-            !FsRtlCheckLockForWriteAccess( &pFcb->Specific.File.FileLock,
-                                           Irp))
+        if( !bPagingIo)
         {
 
-            AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
-                          AFS_TRACE_LEVEL_ERROR,
-                          "AFSCommonWrite (%08lX) Request failed due to lock conflict\n",
-                          Irp);
+            //
+            // Check the BR locks on the file.
+            //
 
-            try_return( ntStatus = STATUS_FILE_LOCK_CONFLICT);
-        }
+            if ( !FsRtlCheckLockForWriteAccess( &pFcb->Specific.File.FileLock,
+                                                Irp))
+            {
 
-        if( bExtendingWrite)
-        {
+                AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
+                              AFS_TRACE_LEVEL_ERROR,
+                              "AFSCommonWrite (%08lX) Request failed due to lock conflict\n",
+                              Irp);
 
-            ntStatus = AFSExtendingWrite( pFcb, pFileObject, (liStartingByte.QuadPart + ulByteCount));
+                try_return( ntStatus = STATUS_FILE_LOCK_CONFLICT);
+            }
 
-            if( !NT_SUCCESS(ntStatus))
+            if( bExtendingWrite)
             {
 
-                AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
-                              AFS_TRACE_LEVEL_ERROR,
-                              "AFSCommonWrite (%08lX) Failed extending write request Status %08lX\n",
-                              Irp,
-                              ntStatus);
+                ntStatus = AFSExtendingWrite( pFcb, pFileObject, (liStartingByte.QuadPart + ulByteCount));
+
+                if( !NT_SUCCESS(ntStatus))
+                {
+
+                    AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
+                                  AFS_TRACE_LEVEL_ERROR,
+                                  "AFSCommonWrite (%08lX) Failed extending write request Status %08lX\n",
+                                  Irp,
+                                  ntStatus);
 
-                try_return( ntStatus );
+                    try_return( ntStatus );
+                }
             }
         }
 
@@ -609,6 +620,10 @@ AFSCommonWrite( IN PDEVICE_OBJECT DeviceObject,
             !bNonCachedIo)
         {
 
+            //
+            // Main and SectionObject resources held Shared
+            //
+
             AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
                           AFS_TRACE_LEVEL_VERBOSE,
                           "AFSCommonWrite (%08lX) Processing CACHED request Offset %I64X Len %08lX\n",
@@ -616,12 +631,41 @@ AFSCommonWrite( IN PDEVICE_OBJECT DeviceObject,
                           liStartingByte.QuadPart,
                           ulByteCount);
 
-            ntStatus = AFSCachedWrite( DeviceObject, Irp, liStartingByte, ulByteCount, bForceFlush);
+            ntStatus = AFSCachedWrite( DeviceObject, Irp, liStartingByte, ulByteCount, TRUE);
 
         }
         else
         {
 
+            //
+            // if bPagingIo, Paging Resource held Shared
+            // else Main and SectionObject resources held Shared
+            //
+
+            if( bReleaseSectionObject)
+            {
+
+                AFSReleaseResource( &pNPFcb->SectionObjectResource);
+
+                bReleaseSectionObject = FALSE;
+            }
+
+            if( bReleasePaging)
+            {
+
+                AFSReleaseResource( &pNPFcb->PagingResource);
+
+                bReleasePaging = FALSE;
+            }
+
+            if( bReleaseMain)
+            {
+
+                AFSReleaseResource( &pNPFcb->Resource);
+
+                bReleaseMain = FALSE;
+            }
+
             AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
                           AFS_TRACE_LEVEL_VERBOSE,
                           "AFSCommonWrite (%08lX) Processing NON-CACHED request Offset %I64X Len %08lX\n",
@@ -642,10 +686,10 @@ try_exit:
 
         ObDereferenceObject(pFileObject);
 
-        if( bReleaseMain)
+        if( bReleaseSectionObject)
         {
 
-            AFSReleaseResource( &pNPFcb->Resource);
+            AFSReleaseResource( &pNPFcb->SectionObjectResource);
         }
 
         if( bReleasePaging)
@@ -654,6 +698,12 @@ try_exit:
             AFSReleaseResource( &pNPFcb->PagingResource);
         }
 
+        if( bReleaseMain)
+        {
+
+            AFSReleaseResource( &pNPFcb->Resource);
+        }
+
         if( bCompleteIrp)
         {
 
@@ -761,7 +811,7 @@ AFSIOCtlWrite( IN PDEVICE_OBJECT DeviceObject,
 
         ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_PIOCTL_WRITE,
                                       AFS_REQUEST_FLAG_SYNCHRONOUS,
-                                      &pFcb->AuthGroup,
+                                      &pCcb->AuthGroup,
                                       NULL,
                                       &stParentFID,
                                       (void *)&stIORequestCB,
@@ -837,6 +887,7 @@ AFSNonCachedWrite( IN PDEVICE_OBJECT DeviceObject,
     LARGE_INTEGER      liCurrentTime, liLastRequestTime;
     AFSDeviceExt      *pControlDevExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
     PFILE_OBJECT       pCacheFileObject = NULL;
+    BOOLEAN            bDerefExtents = FALSE;
 
     __Enter
     {
@@ -889,6 +940,7 @@ AFSNonCachedWrite( IN PDEVICE_OBJECT DeviceObject,
                       ByteCount);
 
         ntStatus = AFSRequestExtentsAsync( pFcb,
+                                           pCcb,
                                            &StartingByte,
                                            ByteCount);
 
@@ -940,7 +992,7 @@ AFSNonCachedWrite( IN PDEVICE_OBJECT DeviceObject,
             bLocked= FALSE;
 
             //
-            // We will re-request the extents after 10 seconds of waiting for them
+            // We will re-request the extents after waiting for them
             //
 
             KeQueryTickCount( &liCurrentTime);
@@ -959,6 +1011,7 @@ AFSNonCachedWrite( IN PDEVICE_OBJECT DeviceObject,
                               ByteCount);
 
                 ntStatus = AFSRequestExtentsAsync( pFcb,
+                                                   pCcb,
                                                    &StartingByte,
                                                    ByteCount);
 
@@ -992,7 +1045,7 @@ AFSNonCachedWrite( IN PDEVICE_OBJECT DeviceObject,
             // Wait for it
             //
 
-            ntStatus =  AFSWaitForExtentMapping ( pFcb );
+            ntStatus =  AFSWaitForExtentMapping ( pFcb, pCcb);
 
             if (!NT_SUCCESS(ntStatus))
             {
@@ -1163,6 +1216,8 @@ AFSNonCachedWrite( IN PDEVICE_OBJECT DeviceObject,
         AFSReferenceActiveExtents( pStartExtent,
                                    extentsCount);
 
+        bDerefExtents = TRUE;
+
         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
                       AFS_TRACE_LEVEL_VERBOSE,
                       "AFSNonCachedWrite Releasing(2) Fcb extents lock %08lX SHARED %08lX\n",
@@ -1306,31 +1361,42 @@ AFSNonCachedWrite( IN PDEVICE_OBJECT DeviceObject,
             try_return( ntStatus);
         }
 
-        //
-        // Since this is dirty we can mark the extents dirty now.
-        // AFSMarkDirty will dereference the extents.  Do not call
-        // AFSDereferenceActiveExtents() in this code path.
-        //
-
-        AFSMarkDirty( pFcb,
-                      pStartExtent,
-                      extentsCount,
-                      &StartingByte);
+try_exit:
 
-        if (!bPagingIo)
+        if( NT_SUCCESS( ntStatus) &&
+            pStartExtent != NULL &&
+            Irp->IoStatus.Information > 0)
         {
+
+           if ( !bLocked)
+           {
+
+               AFSAcquireShared( &pFcb->NPFcb->Specific.File.ExtentsResource,
+                                 TRUE);
+               bLocked = TRUE;
+           }
+
             //
-            // This was an uncached user write - tell the server to do
-            // the flush when the worker thread next wakes up
+            // Since this is dirty we can mark the extents dirty now.
+            // AFSMarkDirty will dereference the extents.  Do not call
+            // AFSDereferenceActiveExtents() in this code path.
             //
-            pFcb->Specific.File.LastServerFlush.QuadPart = 0;
-        }
 
-        //
-        // All done
-        //
+            AFSMarkDirty( pFcb,
+                          pStartExtent,
+                          extentsCount,
+                          &StartingByte,
+                          bDerefExtents);
 
-try_exit:
+            if (!bPagingIo)
+            {
+                //
+                // This was an uncached user write - tell the server to do
+                // the flush when the worker thread next wakes up
+                //
+                pFcb->Specific.File.LastServerFlush.QuadPart = 0;
+            }
+        }
 
         if( pCacheFileObject != NULL)
         {
@@ -1365,13 +1431,13 @@ try_exit:
 
         if( pGatherIo)
         {
-            AFSExFreePool(pGatherIo);
+            AFSExFreePoolWithTag(pGatherIo, AFS_GATHER_TAG);
         }
 
         if( NULL != pIoRuns &&
             stIoRuns != pIoRuns)
         {
-            AFSExFreePool(pIoRuns);
+            AFSExFreePoolWithTag(pIoRuns, AFS_IO_RUN_TAG);
         }
 
         if( bCompleteIrp)
@@ -1672,13 +1738,7 @@ AFSExtendingWrite( IN AFSFcb *Fcb,
 
     ntStatus = AFSUpdateFileInformation( &Fcb->ObjectInformation->ParentObjectInformation->FileId,
                                          Fcb->ObjectInformation,
-                                         &Fcb->AuthGroup);
-
-    AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
-                  AFS_TRACE_LEVEL_VERBOSE,
-                  "AFSExtendingWrite Acquiring Fcb lock %08lX EXCL %08lX\n",
-                  &Fcb->NPFcb->Resource,
-                  PsGetCurrentThread());
+                                         &pCcb->AuthGroup);
 
     if (NT_SUCCESS(ntStatus))
     {
@@ -1804,7 +1864,7 @@ AFSShareWrite( IN PDEVICE_OBJECT DeviceObject,
 
         ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_PIPE_WRITE,
                                       AFS_REQUEST_FLAG_SYNCHRONOUS,
-                                      &pFcb->AuthGroup,
+                                      &pCcb->AuthGroup,
                                       &pCcb->DirectoryCB->NameInformation.FileName,
                                       NULL,
                                       pIoRequest,
@@ -1844,7 +1904,7 @@ try_exit:
         if( pIoRequest != NULL)
         {
 
-            AFSExFreePool( pIoRequest);
+            AFSExFreePoolWithTag( pIoRequest, AFS_GENERIC_MEMORY_14_TAG);
         }
     }