Windows: reorg RDR_CleanupFile to prevent lock leak
[openafs.git] / src / WINNT / afsrdr / user / RDRFunction.c
index 8aca9b3..a75acba 100644 (file)
@@ -404,7 +404,7 @@ RDR_PopulateCurrentEntry( IN  AFSDirEnumEntry * pCurrentEntry,
              * status information.  If not, perform a bulk status lookup of multiple
              * entries in order to reduce the number of RPCs issued to the file server.
              */
-            if ((scp->flags & CM_SCACHEFLAG_EACCESS))
+            if (cm_EAccesFindEntry(userp, &scp->fid))
                 bMustFake = TRUE;
             else if (!cm_HaveCallback(scp)) {
                 lock_ReleaseWrite(&scp->rw);
@@ -1759,7 +1759,7 @@ RDR_CleanupFileEntry( IN cm_user_t *userp,
     Fid.unique = FileId.Unique;
     Fid.hash   = FileId.Hash;
 
-    code = cm_GetSCache(&Fid, &dscp->fid, &scp, userp, &req);
+    code = cm_GetSCache(&Fid, dscp ? &dscp->fid : NULL, &scp, userp, &req);
     if (code) {
         osi_Log1(afsd_logp, "RDR_CleanupFileEntry cm_GetSCache object FID failure code=0x%x",
                  code);
@@ -1841,106 +1841,85 @@ RDR_CleanupFileEntry( IN cm_user_t *userp,
         scp->flags &= ~CM_SCACHEFLAG_RDR_IN_USE;
     }
 
-    if (bLastHandle || bFlushFile) {
-        if (!bScpLocked) {
-            lock_ObtainWrite(&scp->rw);
-            bScpLocked = TRUE;
-        }
-        code = cm_SyncOp(scp, NULL, userp, &req, PRSFS_WRITE,
-                          CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
-        if (code == 0) {
-            if (bScpLocked) {
-                lock_ReleaseWrite(&scp->rw);
-                bScpLocked = FALSE;
+    /* If not a readonly object, flush dirty data and update metadata */
+    if (!(scp->flags & CM_SCACHEFLAG_RO)) {
+        if ((bLastHandle || bFlushFile) &&
+             buf_DirtyBuffersExist(&scp->fid)) {
+            if (!bScpLocked) {
+                lock_ObtainWrite(&scp->rw);
+                bScpLocked = TRUE;
             }
+            code = cm_SyncOp(scp, NULL, userp, &req, PRSFS_WRITE,
+                             CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
+            if (code == 0) {
+                if (bScpLocked) {
+                    lock_ReleaseWrite(&scp->rw);
+                    bScpLocked = FALSE;
+                }
 
-            code = cm_FSync(scp, userp, &req, bScpLocked);
+                code = cm_FSync(scp, userp, &req, bScpLocked);
+            }
+            if (bLastHandle && code)
+                goto unlock;
         }
-        if (bLastHandle && code)
-            goto on_error;
-    }
 
-    if (bUnlockFile || bDeleteFile) {
-        if (!bScpLocked) {
-            lock_ObtainWrite(&scp->rw);
-            bScpLocked = TRUE;
-        }
-        code = cm_SyncOp(scp, NULL, userp, &req, 0,
-                          CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_LOCK);
-        if (code) {
-            osi_Log2(afsd_logp, "RDR_CleanupFileEntry cm_SyncOp (2) failure scp=0x%p code=0x%x",
-                     scp, code);
-            goto on_error;
-        }
+        if (CleanupCB->ChangeTime.QuadPart) {
 
-        key = cm_GenerateKey(CM_SESSION_IFS, CleanupCB->ProcessId, 0);
+            if (scp->fileType == CM_SCACHETYPE_FILE) {
+                /* Do not set length and other attributes at the same time */
+                if (scp->length.QuadPart != CleanupCB->AllocationSize.QuadPart) {
+                    osi_Log2(afsd_logp, "RDR_CleanupFileEntry Length Change 0x%x -> 0x%x",
+                             (afs_uint32)scp->length.QuadPart, (afs_uint32)CleanupCB->AllocationSize.QuadPart);
+                    setAttr.mask |= CM_ATTRMASK_LENGTH;
+                    setAttr.length.LowPart = CleanupCB->AllocationSize.LowPart;
+                    setAttr.length.HighPart = CleanupCB->AllocationSize.HighPart;
 
-        /* the scp is now locked and current */
-        code = cm_UnlockByKey(scp, key,
-                              bDeleteFile ? CM_UNLOCK_FLAG_BY_FID : 0,
-                              userp, &req);
-
-        cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_LOCK);
-
-        if (code)
-            goto on_error;
-    }
-
-    if (CleanupCB->ChangeTime.QuadPart) {
-
-        if (scp->fileType == CM_SCACHETYPE_FILE) {
-            /* Do not set length and other attributes at the same time */
-            if (scp->length.QuadPart != CleanupCB->AllocationSize.QuadPart) {
-                osi_Log2(afsd_logp, "RDR_CleanupFileEntry Length Change 0x%x -> 0x%x",
-                          (afs_uint32)scp->length.QuadPart, (afs_uint32)CleanupCB->AllocationSize.QuadPart);
-                setAttr.mask |= CM_ATTRMASK_LENGTH;
-                setAttr.length.LowPart = CleanupCB->AllocationSize.LowPart;
-                setAttr.length.HighPart = CleanupCB->AllocationSize.HighPart;
-
-                if (bScpLocked) {
-                    lock_ReleaseWrite(&scp->rw);
-                    bScpLocked = FALSE;
+                    if (bScpLocked) {
+                        lock_ReleaseWrite(&scp->rw);
+                        bScpLocked = FALSE;
+                    }
+                    code = cm_SetAttr(scp, &setAttr, userp, &req);
+                    if (code)
+                        goto unlock;
+                    setAttr.mask = 0;
                 }
-                code = cm_SetAttr(scp, &setAttr, userp, &req);
-                if (code)
-                    goto on_error;
-                setAttr.mask = 0;
             }
-        }
 
-        if (!bScpLocked) {
-            lock_ObtainWrite(&scp->rw);
-            bScpLocked = TRUE;
-        }
+            if (!bScpLocked) {
+                lock_ObtainWrite(&scp->rw);
+                bScpLocked = TRUE;
+            }
 
-        if ((scp->unixModeBits & 0200) && (CleanupCB->FileAttributes & FILE_ATTRIBUTE_READONLY)) {
-            setAttr.mask |= CM_ATTRMASK_UNIXMODEBITS;
-            setAttr.unixModeBits = scp->unixModeBits & ~0222;
-        } else if (!(scp->unixModeBits & 0200) && !(CleanupCB->FileAttributes & FILE_ATTRIBUTE_READONLY)) {
-            setAttr.mask |= CM_ATTRMASK_UNIXMODEBITS;
-            setAttr.unixModeBits = scp->unixModeBits | 0222;
+            if ((scp->unixModeBits & 0200) && (CleanupCB->FileAttributes & FILE_ATTRIBUTE_READONLY)) {
+                setAttr.mask |= CM_ATTRMASK_UNIXMODEBITS;
+                setAttr.unixModeBits = scp->unixModeBits & ~0222;
+            } else if (!(scp->unixModeBits & 0200) && !(CleanupCB->FileAttributes & FILE_ATTRIBUTE_READONLY)) {
+                setAttr.mask |= CM_ATTRMASK_UNIXMODEBITS;
+                setAttr.unixModeBits = scp->unixModeBits | 0222;
+            }
         }
-    }
 
-    if (CleanupCB->LastWriteTime.QuadPart) {
-        ft.dwLowDateTime = CleanupCB->LastWriteTime.LowPart;
-        ft.dwHighDateTime = CleanupCB->LastWriteTime.HighPart;
+        if (CleanupCB->LastWriteTime.QuadPart) {
+            ft.dwLowDateTime = CleanupCB->LastWriteTime.LowPart;
+            ft.dwHighDateTime = CleanupCB->LastWriteTime.HighPart;
 
-        cm_UnixTimeFromLargeSearchTime(&clientModTime, &ft);
-        if (scp->clientModTime != clientModTime) {
-            setAttr.mask |= CM_ATTRMASK_CLIENTMODTIME;
-            setAttr.clientModTime = clientModTime;
+            cm_UnixTimeFromLargeSearchTime(&clientModTime, &ft);
+            if (scp->clientModTime != clientModTime) {
+                setAttr.mask |= CM_ATTRMASK_CLIENTMODTIME;
+                setAttr.clientModTime = clientModTime;
+            }
         }
-    }
 
-    /* call setattr */
-    if (setAttr.mask) {
-        lock_ReleaseWrite(&scp->rw);
-        bScpLocked = FALSE;
-        code = cm_SetAttr(scp, &setAttr, userp, &req);
-    } else
-        code = 0;
+        /* call setattr */
+        if (setAttr.mask) {
+            lock_ReleaseWrite(&scp->rw);
+            bScpLocked = FALSE;
+            code = cm_SetAttr(scp, &setAttr, userp, &req);
+        } else
+            code = 0;
+    }
 
+  unlock:
     /* Now drop the lock enforcing the share access */
     if ( CleanupCB->FileAccess != AFS_FILE_ACCESS_NOLOCK) {
         unsigned int sLockType;
@@ -1979,13 +1958,39 @@ RDR_CleanupFileEntry( IN cm_user_t *userp,
         }
     }
 
+    if (bUnlockFile || bDeleteFile) {
+        if (!bScpLocked) {
+            lock_ObtainWrite(&scp->rw);
+            bScpLocked = TRUE;
+        }
+        code = cm_SyncOp(scp, NULL, userp, &req, 0,
+                          CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_LOCK);
+        if (code) {
+            osi_Log2(afsd_logp, "RDR_CleanupFileEntry cm_SyncOp (2) failure scp=0x%p code=0x%x",
+                     scp, code);
+            goto on_error;
+        }
+
+        key = cm_GenerateKey(CM_SESSION_IFS, CleanupCB->ProcessId, 0);
+
+        /* the scp is now locked and current */
+        code = cm_UnlockByKey(scp, key,
+                              bDeleteFile ? CM_UNLOCK_FLAG_BY_FID : 0,
+                              userp, &req);
+
+        cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_LOCK);
+
+        if (code)
+            goto on_error;
+    }
+
   on_error:
     if (bDscpLocked)
         lock_ReleaseWrite(&dscp->rw);
     if (bScpLocked)
         lock_ReleaseWrite(&scp->rw);
 
-    if (dscp && bDeleteFile) {
+    if (code == 0 && dscp && bDeleteFile) {
         WCHAR FileName[260];
 
         StringCchCopyNW(FileName, 260, FileNameCounted, FileNameLength / sizeof(WCHAR));
@@ -1996,16 +2001,16 @@ RDR_CleanupFileEntry( IN cm_user_t *userp,
             code = cm_Unlink(dscp, NULL, FileName, userp, &req);
     }
 
-    if ( ResultBufferLength >=  sizeof( AFSFileCleanupResultCB))
-    {
-        (*ResultCB)->ResultBufferLength = sizeof( AFSFileCleanupResultCB);
-        pResultCB = (AFSFileCleanupResultCB *)&(*ResultCB)->ResultData;
-        pResultCB->ParentDataVersion.QuadPart = dscp ? dscp->dataVersion : 0;
-    } else {
-        (*ResultCB)->ResultBufferLength = 0;
-    }
-
     if (code == 0) {
+        if ( ResultBufferLength >=  sizeof( AFSFileCleanupResultCB))
+        {
+            (*ResultCB)->ResultBufferLength = sizeof( AFSFileCleanupResultCB);
+            pResultCB = (AFSFileCleanupResultCB *)&(*ResultCB)->ResultData;
+            pResultCB->ParentDataVersion.QuadPart = dscp ? dscp->dataVersion : 0;
+        } else {
+            (*ResultCB)->ResultBufferLength = 0;
+        }
+
         (*ResultCB)->ResultStatus = 0;
         osi_Log0(afsd_logp, "RDR_CleanupFileEntry SUCCESS");
     } else {
@@ -2014,6 +2019,7 @@ RDR_CleanupFileEntry( IN cm_user_t *userp,
         osi_Log2(afsd_logp, "RDR_CleanupFileEntry FAILURE code=0x%x status=0x%x",
                   code, status);
     }
+
     if (scp)
         cm_ReleaseSCache(scp);
     if (dscp)
@@ -4995,14 +5001,8 @@ RDR_GetVolumeInfo( IN cm_user_t     *userp,
     (*ResultCB)->ResultBufferLength = sizeof(AFSVolumeInfoCB);
     pResultCB = (AFSVolumeInfoCB *)(*ResultCB)->ResultData;
 
-    /* Allocate the extents from the buffer package */
     if (FileId.Cell != 0) {
-        Fid.cell = FileId.Cell;
-        Fid.volume = FileId.Volume;
-        Fid.vnode = FileId.Vnode;
-        Fid.unique = FileId.Unique;
-        Fid.hash = FileId.Hash;
-
+        cm_SetFid(&Fid, FileId.Cell, FileId.Volume, 1, 1);
         code = cm_GetSCache(&Fid, NULL, &scp, userp, &req);
         if (code) {
             smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
@@ -5019,7 +5019,6 @@ RDR_GetVolumeInfo( IN cm_user_t     *userp,
     }
     lock_ObtainWrite(&scp->rw);
 
-    /* start by looking up the file's end */
     code = cm_SyncOp(scp, NULL, userp, &req, 0,
                       CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
     if (code) {
@@ -5032,7 +5031,6 @@ RDR_GetVolumeInfo( IN cm_user_t     *userp,
         return;
     }
 
-    /* Fake for now */
     pResultCB->SectorsPerAllocationUnit = 1;
     pResultCB->BytesPerSector = 1024;
 
@@ -5084,11 +5082,29 @@ RDR_GetVolumeInfo( IN cm_user_t     *userp,
        } while (cm_Analyze(connp, userp, &req, &scp->fid, 0, NULL, NULL, NULL, code));
        code = cm_MapRPCError(code, &req);
         if (code == 0) {
-            pResultCB->TotalAllocationUnits.QuadPart = volStat.PartMaxBlocks;
-            pResultCB->AvailableAllocationUnits.QuadPart = volStat.PartBlocksAvail;
-
-            pResultCB->VolumeLabelLength = cm_Utf8ToUtf16( Name, -1, pResultCB->VolumeLabel,
-                                                           (sizeof(pResultCB->VolumeLabel) / sizeof(WCHAR)) + 1);
+            if (volStat.MaxQuota)
+            {
+                pResultCB->TotalAllocationUnits.QuadPart = volStat.MaxQuota;
+                if (volType == ROVOL || volType == BACKVOL) {
+                    pResultCB->AvailableAllocationUnits.QuadPart = 0;
+                }
+                else
+                {
+                    pResultCB->AvailableAllocationUnits.QuadPart =
+                        min(volStat.MaxQuota - volStat.BlocksInUse, volStat.PartBlocksAvail);
+                }
+            }
+            else
+            {
+                pResultCB->TotalAllocationUnits.QuadPart = volStat.PartMaxBlocks;
+                if (volType == ROVOL || volType == BACKVOL) {
+                    pResultCB->AvailableAllocationUnits.QuadPart = 0;
+                }
+                else
+                {
+                    pResultCB->AvailableAllocationUnits.QuadPart = volStat.PartBlocksAvail;
+                }
+            }
         } else {
             pResultCB->TotalAllocationUnits.QuadPart = 0x7FFFFFFF;
             pResultCB->AvailableAllocationUnits.QuadPart = (volType == ROVOL || volType == BACKVOL) ? 0 : 0x3F000000;
@@ -5119,6 +5135,167 @@ RDR_GetVolumeInfo( IN cm_user_t     *userp,
 }
 
 void
+RDR_GetVolumeSizeInfo( IN cm_user_t     *userp,
+                   IN AFSFileID     FileId,
+                   IN BOOL bWow64,
+                   IN DWORD ResultBufferLength,
+                   IN OUT AFSCommResult **ResultCB)
+{
+    AFSVolumeSizeInfoCB *pResultCB = NULL;
+    DWORD       Length;
+    cm_scache_t *scp = NULL;
+    cm_volume_t *volp = NULL;
+    afs_uint32   volType;
+    cm_cell_t   *cellp = NULL;
+    cm_fid_t    Fid;
+    afs_uint32  code;
+    cm_req_t    req;
+    DWORD       status;
+
+    char volName[32]="(unknown)";
+    char offLineMsg[256]="server temporarily inaccessible";
+    char motd[256]="server temporarily inaccessible";
+    cm_conn_t *connp;
+    AFSFetchVolumeStatus volStat;
+    char *Name;
+    char *OfflineMsg;
+    char *MOTD;
+    struct rx_connection * rxconnp;
+
+    RDR_InitReq(&req);
+    if ( bWow64 )
+        req.flags |= CM_REQ_WOW64;
+
+    osi_Log4(afsd_logp, "RDR_GetVolumeSizeInfo File FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
+             FileId.Cell, FileId.Volume,
+             FileId.Vnode, FileId.Unique);
+
+    Length = sizeof( AFSCommResult) + sizeof(AFSVolumeSizeInfoCB);
+    if (sizeof(AFSVolumeSizeInfoCB) > ResultBufferLength) {
+        *ResultCB = (AFSCommResult *)malloc(sizeof(AFSCommResult) );
+        if (!(*ResultCB))
+            return;
+        memset( *ResultCB, 0, sizeof(AFSCommResult));
+        (*ResultCB)->ResultStatus = STATUS_BUFFER_OVERFLOW;
+        return;
+    }
+
+    *ResultCB = (AFSCommResult *)malloc( Length );
+    if (!(*ResultCB))
+       return;
+    memset( *ResultCB, '\0', Length );
+    (*ResultCB)->ResultBufferLength = sizeof(AFSVolumeSizeInfoCB);
+    pResultCB = (AFSVolumeSizeInfoCB *)(*ResultCB)->ResultData;
+
+    if (FileId.Cell != 0) {
+        cm_SetFid(&Fid, FileId.Cell, FileId.Volume, 1, 1);
+        code = cm_GetSCache(&Fid, NULL, &scp, userp, &req);
+        if (code) {
+            smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
+            (*ResultCB)->ResultStatus = status;
+            (*ResultCB)->ResultBufferLength = 0;
+            osi_Log2(afsd_logp, "RDR_GetVolumeSizeInfo cm_GetSCache FID failure code=0x%x status=0x%x",
+                      code, status);
+            return;
+        }
+    } else {
+        (*ResultCB)->ResultStatus = STATUS_OBJECT_NAME_INVALID;
+        osi_Log0(afsd_logp, "RDR_GetVolumeSizeInfo Object Name Invalid - Cell = 0");
+        return;
+    }
+    lock_ObtainWrite(&scp->rw);
+
+    code = cm_SyncOp(scp, NULL, userp, &req, 0,
+                      CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
+    if (code) {
+        lock_ReleaseWrite(&scp->rw);
+        smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
+        (*ResultCB)->ResultStatus = status;
+        (*ResultCB)->ResultBufferLength = 0;
+        osi_Log3(afsd_logp, "RDR_GetVolumeSizeInfo cm_SyncOp failure scp=0x%p code=0x%x status=0x%x",
+                 scp, code, status);
+        return;
+    }
+
+    pResultCB->SectorsPerAllocationUnit = 1;
+    pResultCB->BytesPerSector = 1024;
+
+    if (scp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
+         scp->fid.volume==AFS_FAKE_ROOT_VOL_ID)
+    {
+        pResultCB->TotalAllocationUnits.QuadPart = 100;
+        pResultCB->AvailableAllocationUnits.QuadPart = 0;
+    } else {
+        volp = cm_GetVolumeByFID(&scp->fid);
+        if (!volp) {
+            code = CM_ERROR_NOSUCHVOLUME;
+            goto _done;
+        }
+
+        volType = cm_VolumeType(volp, scp->fid.volume);
+        Name = volName;
+       OfflineMsg = offLineMsg;
+       MOTD = motd;
+       lock_ReleaseWrite(&scp->rw);
+       do {
+           code = cm_ConnFromFID(&scp->fid, userp, &req, &connp);
+           if (code) continue;
+
+           rxconnp = cm_GetRxConn(connp);
+           code = RXAFS_GetVolumeStatus(rxconnp, scp->fid.volume,
+                                        &volStat, &Name, &OfflineMsg, &MOTD);
+           rx_PutConnection(rxconnp);
+
+       } while (cm_Analyze(connp, userp, &req, &scp->fid, 0, NULL, NULL, NULL, code));
+       code = cm_MapRPCError(code, &req);
+        if (code == 0) {
+            if (volStat.MaxQuota)
+            {
+                pResultCB->TotalAllocationUnits.QuadPart = volStat.MaxQuota;
+                if (volType == ROVOL || volType == BACKVOL) {
+                    pResultCB->AvailableAllocationUnits.QuadPart = 0;
+                }
+                else
+                {
+                    pResultCB->AvailableAllocationUnits.QuadPart =
+                        min(volStat.MaxQuota - volStat.BlocksInUse, volStat.PartBlocksAvail);
+                }
+            }
+            else
+            {
+                pResultCB->TotalAllocationUnits.QuadPart = volStat.PartMaxBlocks;
+                if (volType == ROVOL || volType == BACKVOL) {
+                    pResultCB->AvailableAllocationUnits.QuadPart = 0;
+                }
+                else
+                {
+                    pResultCB->AvailableAllocationUnits.QuadPart = volStat.PartBlocksAvail;
+                }
+            }
+        } else {
+
+            pResultCB->TotalAllocationUnits.QuadPart = 0x7FFFFFFF;
+            pResultCB->AvailableAllocationUnits.QuadPart = (volType == ROVOL || volType == BACKVOL) ? 0 : 0x3F000000;
+            code = 0;
+        }
+        lock_ObtainWrite(&scp->rw);
+    }
+
+    cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
+
+  _done:
+    lock_ReleaseWrite(&scp->rw);
+    if (volp)
+       cm_PutVolume(volp);
+    cm_ReleaseSCache(scp);
+
+    smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
+    (*ResultCB)->ResultStatus = status;
+    osi_Log0(afsd_logp, "RDR_GetVolumeSizeInfo SUCCESS");
+    return;
+}
+
+void
 RDR_HoldFid( IN cm_user_t     *userp,
              IN AFSHoldFidRequestCB * pHoldFidCB,
              IN BOOL bFast,