Windows: AFSLocateNameEntry tracking DirectoryCB IN/OUT
[openafs.git] / src / WINNT / afsrdr / kernel / lib / AFSGeneric.cpp
index 0d2c4cf..78600a6 100644 (file)
@@ -6053,6 +6053,7 @@ AFSRetrieveFileAttributes( IN AFSDirectoryCB *ParentDirectoryCB,
     AFSVolumeCB *pNewVolumeCB = NULL;
     LONG NewVolumeReferenceReason = AFS_VOLUME_REFERENCE_INVALID;
     AFSDirectoryCB *pDirectoryEntry = NULL, *pParentDirEntry = NULL;
+    AFSDirectoryCB *pNewParentDirEntry = NULL;
     WCHAR *pwchBuffer = NULL;
     UNICODE_STRING uniComponentName, uniRemainingPath, uniParsedName;
     ULONG ulNameDifference = 0;
@@ -6336,7 +6337,7 @@ AFSRetrieveFileAttributes( IN AFSDirectoryCB *ParentDirectoryCB,
                                        pParentDirEntry,
                                        &pNewVolumeCB,
                                        &NewVolumeReferenceReason,
-                                       &pParentDirEntry,
+                                       &pNewParentDirEntry,
                                        &pDirectoryEntry,
                                        NULL);
 
@@ -6366,44 +6367,28 @@ AFSRetrieveFileAttributes( IN AFSDirectoryCB *ParentDirectoryCB,
 
         NewVolumeReferenceReason = AFS_VOLUME_REFERENCE_INVALID;
 
-        if( !NT_SUCCESS( ntStatus) ||
-            ntStatus == STATUS_REPARSE)
-        {
-
-            if( ntStatus == STATUS_OBJECT_NAME_NOT_FOUND)
-            {
-
-                if( pDirectoryEntry != NULL)
-                {
-
-                    lCount = InterlockedDecrement( &pDirectoryEntry->DirOpenReferenceCount);
+        //
+        // AFSLocateNameEntry does not alter the reference count of
+        // pParentDirectoryCB and it returns pNewParentDirectoryCB with
+        // a reference held.
+        //
 
-                    AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
-                                  AFS_TRACE_LEVEL_VERBOSE,
-                                  "AFSRetrieveFileAttributes Decrement1 count on %wZ DE %p Ccb %p Cnt %d\n",
-                                  &pDirectoryEntry->NameInformation.FileName,
-                                  pDirectoryEntry,
-                                  NULL,
-                                  lCount);
+        lCount = InterlockedDecrement( &pParentDirEntry->DirOpenReferenceCount);
 
-                    ASSERT( lCount >= 0);
-                }
-                else
-                {
+        AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
+                      AFS_TRACE_LEVEL_VERBOSE,
+                      "AFSRetrieveFileAttributes DecrementX count on %wZ DE %p Cnt %d\n",
+                      &pParentDirEntry->NameInformation.FileName,
+                      pParentDirEntry,
+                      lCount);
 
-                    lCount = InterlockedDecrement( &pParentDirEntry->DirOpenReferenceCount);
+        pParentDirEntry = pNewParentDirEntry;
 
-                    AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
-                                  AFS_TRACE_LEVEL_VERBOSE,
-                                  "AFSRetrieveFileAttributes Decrement2 count on %wZ DE %p Ccb %p Cnt %d\n",
-                                  &pParentDirEntry->NameInformation.FileName,
-                                  pParentDirEntry,
-                                  NULL,
-                                  lCount);
+        pNewParentDirEntry = NULL;
 
-                    ASSERT( lCount >= 0);
-                }
-            }
+        if( !NT_SUCCESS( ntStatus) ||
+            ntStatus == STATUS_REPARSE)
+        {
 
             try_return( ntStatus);
         }
@@ -6451,28 +6436,44 @@ AFSRetrieveFileAttributes( IN AFSDirectoryCB *ParentDirectoryCB,
 
         FileInfo->ChangeTime = pDirectoryEntry->ObjectInformation->ChangeTime;
 
-        //
-        // Remove the reference made above
-        //
+try_exit:
 
-        lCount = InterlockedDecrement( &pDirectoryEntry->DirOpenReferenceCount);
+        if( pDirEntry != NULL)
+        {
 
-        AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
-                      AFS_TRACE_LEVEL_VERBOSE,
-                      "AFSRetrieveFileAttributes Decrement3 count on %wZ DE %p Ccb %p Cnt %d\n",
-                      &pDirectoryEntry->NameInformation.FileName,
-                      pDirectoryEntry,
-                      NULL,
-                      lCount);
+            AFSExFreePoolWithTag( pDirEntry, AFS_GENERIC_MEMORY_2_TAG);
+        }
 
-        ASSERT( lCount >= 0);
+        if( pDirectoryEntry != NULL)
+        {
 
-try_exit:
+            lCount = InterlockedDecrement( &pDirectoryEntry->DirOpenReferenceCount);
 
-        if( pDirEntry != NULL)
+            AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
+                          AFS_TRACE_LEVEL_VERBOSE,
+                          "AFSRetrieveFileAttributes Decrement1 count on %wZ DE %p Ccb %p Cnt %d\n",
+                          &pDirectoryEntry->NameInformation.FileName,
+                          pDirectoryEntry,
+                          NULL,
+                          lCount);
+
+            ASSERT( lCount >= 0);
+        }
+
+        if ( pParentDirEntry != NULL)
         {
 
-            AFSExFreePoolWithTag( pDirEntry, AFS_GENERIC_MEMORY_2_TAG);
+            lCount = InterlockedDecrement( &pParentDirEntry->DirOpenReferenceCount);
+
+            AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
+                          AFS_TRACE_LEVEL_VERBOSE,
+                          "AFSRetrieveFileAttributes Decrement2 count on %wZ DE %p Ccb %p Cnt %d\n",
+                          &pParentDirEntry->NameInformation.FileName,
+                          pParentDirEntry,
+                          NULL,
+                          lCount);
+
+            ASSERT( lCount >= 0);
         }
 
         if( pVolumeCB != NULL)
@@ -6927,6 +6928,7 @@ AFSEvaluateRootEntry( IN AFSDirectoryCB *DirectoryCB,
     AFSVolumeCB *pNewVolumeCB = NULL;
     LONG NewVolumeReferenceReason = AFS_VOLUME_REFERENCE_INVALID;
     AFSDirectoryCB *pDirectoryEntry = NULL, *pParentDirEntry = NULL;
+    AFSDirectoryCB *pNewParentDirEntry = NULL;
     WCHAR *pwchBuffer = NULL;
     UNICODE_STRING uniComponentName, uniRemainingPath, uniParsedName;
     ULONG ulNameDifference = 0;
@@ -7117,7 +7119,7 @@ AFSEvaluateRootEntry( IN AFSDirectoryCB *DirectoryCB,
                                        pParentDirEntry,
                                        &pNewVolumeCB,
                                        &VolumeReferenceReason,
-                                       &pParentDirEntry,
+                                       &pNewParentDirEntry,
                                        &pDirectoryEntry,
                                        NULL);
 
@@ -7147,44 +7149,28 @@ AFSEvaluateRootEntry( IN AFSDirectoryCB *DirectoryCB,
 
         NewVolumeReferenceReason = AFS_VOLUME_REFERENCE_INVALID;
 
-        if( !NT_SUCCESS( ntStatus) ||
-            ntStatus == STATUS_REPARSE)
-        {
-
-            if( ntStatus == STATUS_OBJECT_NAME_NOT_FOUND)
-            {
-
-                if( pDirectoryEntry != NULL)
-                {
-
-                    lCount = InterlockedDecrement( &pDirectoryEntry->DirOpenReferenceCount);
+        //
+        // AFSLocateNameEntry does not alter the reference count of
+        // pParentDirectoryCB and it returns pNewParentDirectoryCB with
+        // a reference held.
+        //
 
-                    AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
-                                  AFS_TRACE_LEVEL_VERBOSE,
-                                  "AFSEvaluateRootEntry Decrement1 count on %wZ DE %p Ccb %p Cnt %d\n",
-                                  &pDirectoryEntry->NameInformation.FileName,
-                                  pDirectoryEntry,
-                                  NULL,
-                                  lCount);
+        lCount = InterlockedDecrement( &pParentDirEntry->DirOpenReferenceCount);
 
-                    ASSERT( lCount >= 0);
-                }
-                else
-                {
+        AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
+                      AFS_TRACE_LEVEL_VERBOSE,
+                      "AFSEvaluateRootEntry DecrementX count on %wZ DE %p Cnt %d\n",
+                      &pParentDirEntry->NameInformation.FileName,
+                      pParentDirEntry,
+                      lCount);
 
-                    lCount = InterlockedDecrement( &pParentDirEntry->DirOpenReferenceCount);
+        pParentDirEntry = pNewParentDirEntry;
 
-                    AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
-                                  AFS_TRACE_LEVEL_VERBOSE,
-                                  "AFSEvaluateRootEntry Decrement2 count on %wZ DE %p Ccb %p Cnt %d\n",
-                                  &pParentDirEntry->NameInformation.FileName,
-                                  pParentDirEntry,
-                                  NULL,
-                                  lCount);
+        pNewParentDirEntry = NULL;
 
-                    ASSERT( lCount >= 0);
-                }
-            }
+        if( !NT_SUCCESS( ntStatus) ||
+            ntStatus == STATUS_REPARSE)
+        {
 
             pVolumeCB = NULL;
 
@@ -7198,8 +7184,42 @@ AFSEvaluateRootEntry( IN AFSDirectoryCB *DirectoryCB,
 
         *TargetDirEntry = pDirectoryEntry;
 
+        pDirectoryEntry = NULL;
+
 try_exit:
 
+        if( pDirectoryEntry != NULL)
+        {
+
+            lCount = InterlockedDecrement( &pDirectoryEntry->DirOpenReferenceCount);
+
+            AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
+                          AFS_TRACE_LEVEL_VERBOSE,
+                          "AFSEvaluateRootEntry Decrement1 count on %wZ DE %p Ccb %p Cnt %d\n",
+                          &pDirectoryEntry->NameInformation.FileName,
+                          pDirectoryEntry,
+                          NULL,
+                          lCount);
+
+            ASSERT( lCount >= 0);
+        }
+
+        if ( pParentDirEntry != NULL)
+        {
+
+            lCount = InterlockedDecrement( &pParentDirEntry->DirOpenReferenceCount);
+
+            AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
+                          AFS_TRACE_LEVEL_VERBOSE,
+                          "AFSEvaluateRootEntry Decrement2 count on %wZ DE %p Ccb %p Cnt %d\n",
+                          &pParentDirEntry->NameInformation.FileName,
+                          pParentDirEntry,
+                          NULL,
+                          lCount);
+
+            ASSERT( lCount >= 0);
+        }
+
         if( pDirEntry != NULL)
         {
 
@@ -8391,6 +8411,7 @@ AFSGetObjectStatus( IN AFSGetStatusInfoCB *GetStatusInfo,
     UNICODE_STRING uniFullPathName, uniRemainingPath, uniComponentName, uniParsedName;
     AFSNameArrayHdr *pNameArray = NULL;
     AFSDirectoryCB *pDirectoryEntry = NULL, *pParentDirEntry = NULL;
+    AFSDirectoryCB *pNewParentDirEntry = NULL;
     LONG lCount;
 
     __Enter
@@ -8586,7 +8607,7 @@ AFSGetObjectStatus( IN AFSGetStatusInfoCB *GetStatusInfo,
                                            pParentDirEntry,
                                            &pNewVolumeCB,
                                            &NewVolumeReferenceReason,
-                                           &pParentDirEntry,
+                                           &pNewParentDirEntry,
                                            &pDirectoryEntry,
                                            NULL);
 
@@ -8616,71 +8637,34 @@ AFSGetObjectStatus( IN AFSGetStatusInfoCB *GetStatusInfo,
 
             NewVolumeReferenceReason = AFS_VOLUME_REFERENCE_INVALID;
 
-            if( !NT_SUCCESS( ntStatus) ||
-                ntStatus == STATUS_REPARSE)
-            {
-
-                //
-                // The volume lock was released on failure or reparse above
-                // Except for STATUS_OBJECT_NAME_NOT_FOUND
-                //
-
-                if( ntStatus == STATUS_OBJECT_NAME_NOT_FOUND)
-                {
-
-                    if( pDirectoryEntry != NULL)
-                    {
-
-                        lCount = InterlockedDecrement( &pDirectoryEntry->DirOpenReferenceCount);
+            //
+            // AFSLocateNameEntry does not alter the reference count of
+            // pParentDirectoryCB and it returns pNewParentDirectoryCB with
+            // a reference held.
+            //
 
-                        AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
-                                      AFS_TRACE_LEVEL_VERBOSE,
-                                      "AFSGetObjectStatus Decrement1 count on %wZ DE %p Ccb %p Cnt %d\n",
-                                      &pDirectoryEntry->NameInformation.FileName,
-                                      pDirectoryEntry,
-                                      NULL,
-                                      lCount);
+            lCount = InterlockedDecrement( &pParentDirEntry->DirOpenReferenceCount);
 
-                        ASSERT( lCount >= 0);
-                    }
-                    else
-                    {
+            AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
+                          AFS_TRACE_LEVEL_VERBOSE,
+                          "AFSGetObjectStatus DecrementX count on %wZ DE %p Cnt %d\n",
+                          &pParentDirEntry->NameInformation.FileName,
+                          pParentDirEntry,
+                          lCount);
 
-                        lCount = InterlockedDecrement( &pParentDirEntry->DirOpenReferenceCount);
+            pParentDirEntry = pNewParentDirEntry;
 
-                        AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
-                                      AFS_TRACE_LEVEL_VERBOSE,
-                                      "AFSGetObjectStatus Decrement2 count on %wZ DE %p Ccb %p Cnt %d\n",
-                                      &pParentDirEntry->NameInformation.FileName,
-                                      pParentDirEntry,
-                                      NULL,
-                                      lCount);
+            pNewParentDirEntry = NULL;
 
-                        ASSERT( lCount >= 0);
-                    }
-                }
+            if( !NT_SUCCESS( ntStatus) ||
+                ntStatus == STATUS_REPARSE)
+            {
 
                 pVolumeCB = NULL;
 
                 try_return( ntStatus);
             }
 
-            //
-            // Remove the reference obtained from AFSLocateNameEntry
-            //
-
-            lCount = InterlockedDecrement( &pDirectoryEntry->DirOpenReferenceCount);
-
-            AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
-                          AFS_TRACE_LEVEL_VERBOSE,
-                          "AFSGetObjectStatus Decrement3 count on %wZ DE %p Ccb %p Cnt %d\n",
-                          &pDirectoryEntry->NameInformation.FileName,
-                          pDirectoryEntry,
-                          NULL,
-                          lCount);
-
-            ASSERT( lCount >= 0);
-
             pObjectInfo = pDirectoryEntry->ObjectInformation;
 
             lCount = AFSObjectInfoIncrement( pObjectInfo,
@@ -8735,6 +8719,38 @@ AFSGetObjectStatus( IN AFSGetStatusInfoCB *GetStatusInfo,
 
 try_exit:
 
+        if( pDirectoryEntry != NULL)
+        {
+
+            lCount = InterlockedDecrement( &pDirectoryEntry->DirOpenReferenceCount);
+
+            AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
+                          AFS_TRACE_LEVEL_VERBOSE,
+                          "AFSGetObjectStatus Decrement1 count on %wZ DE %p Ccb %p Cnt %d\n",
+                          &pDirectoryEntry->NameInformation.FileName,
+                          pDirectoryEntry,
+                          NULL,
+                          lCount);
+
+            ASSERT( lCount >= 0);
+        }
+
+        if ( pParentDirEntry != NULL)
+        {
+
+            lCount = InterlockedDecrement( &pParentDirEntry->DirOpenReferenceCount);
+
+            AFSDbgLogMsg( AFS_SUBSYSTEM_DIRENTRY_REF_COUNTING,
+                          AFS_TRACE_LEVEL_VERBOSE,
+                          "AFSGetObjectStatus Decrement2 count on %wZ DE %p Ccb %p Cnt %d\n",
+                          &pParentDirEntry->NameInformation.FileName,
+                          pParentDirEntry,
+                          NULL,
+                          lCount);
+
+            ASSERT( lCount >= 0);
+        }
+
         if( pObjectInfo != NULL)
         {