LINUX: afs_create infinite fetchStatus loop
[pkg-k5-afs_openafs.git] / src / WINNT / afsclass / c_agg.cpp
blob1184a63414c9eef011b2ac842581254d7ad18aad
1 /*
2 * Copyright 2000, International Business Machines Corporation and others.
3 * All Rights Reserved.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
8 */
10 #include <winsock2.h>
11 #include <ws2tcpip.h>
13 extern "C" {
14 #include <afsconfig.h>
15 #include <afs/param.h>
16 #include <roken.h>
19 #include <WINNT/afsclass.h>
20 #include "internal.h"
24 * DEFINITIONS ________________________________________________________________
30 * VARIABLES __________________________________________________________________
36 * PROTOTYPES _________________________________________________________________
42 * ROUTINES ___________________________________________________________________
47 AGGREGATE::AGGREGATE (LPSERVER lpServerParent, LPTSTR pszName, LPTSTR pszDevice)
49 m_lpiServer = lpServerParent->GetIdentifier();
50 m_lpiCell = lpServerParent->m_lpiCell;
51 m_lpiThis = NULL;
53 lstrcpy (m_szName, pszName);
54 lstrcpy (m_szDevice, pszDevice);
55 m_wGhost = 0;
56 m_idPartition = NO_PARTITION;
58 m_fFilesetsOutOfDate = TRUE;
59 m_lFilesets = New (HASHLIST);
60 m_lFilesets->SetCriticalSection (AfsClass_GetCriticalSection());
61 m_lkFilesetName = m_lFilesets->CreateKey ("Fileset Name", AGGREGATE::KeyFilesetName_Compare, AGGREGATE::KeyFilesetName_HashObject, AGGREGATE::KeyFilesetName_HashData);
62 m_lkFilesetID = m_lFilesets->CreateKey ("Fileset ID", AGGREGATE::KeyFilesetID_Compare, AGGREGATE::KeyFilesetID_HashObject, AGGREGATE::KeyFilesetID_HashData);
64 m_fStatusOutOfDate = TRUE;
65 m_fAllocationOutOfDate = TRUE;
66 memset (&m_as, 0x00, sizeof(AGGREGATESTATUS));
70 AGGREGATE::~AGGREGATE (void)
72 for (LPENUM pEnum = m_lFilesets->FindLast(); pEnum; pEnum = pEnum->FindPrevious())
74 LPFILESET lpFileset = (LPFILESET)(pEnum->GetObject());
75 m_lFilesets->Remove (lpFileset);
76 Delete (lpFileset);
78 Delete (m_lFilesets);
80 if (m_lpiThis)
81 m_lpiThis->m_cRef --;
85 void AGGREGATE::SendDeleteNotifications (void)
87 for (LPENUM pEnum = m_lFilesets->FindFirst(); pEnum; pEnum = pEnum->FindNext())
89 LPFILESET lpFileset = (LPFILESET)(pEnum->GetObject());
90 lpFileset->SendDeleteNotifications();
93 NOTIFYCALLBACK::SendNotificationToAll (evtDestroy, GetIdentifier());
97 void AGGREGATE::Close (void)
99 AfsClass_Leave();
103 LPIDENT AGGREGATE::GetIdentifier (void)
105 if (m_lpiThis == NULL)
107 if ((m_lpiThis = IDENT::FindIdent (this)) == NULL)
108 m_lpiThis = New2 (IDENT,(this));
109 m_lpiThis->m_cRef ++;
112 return m_lpiThis;
116 void AGGREGATE::Invalidate (void)
118 if (!m_fStatusOutOfDate || !m_fFilesetsOutOfDate)
120 if (m_wGhost & GHOST_HAS_SERVER_ENTRY)
122 m_fStatusOutOfDate = TRUE;
123 m_fFilesetsOutOfDate = TRUE;
126 NOTIFYCALLBACK::SendNotificationToAll (evtInvalidate, GetIdentifier());
131 void AGGREGATE::InvalidateAllocation (void)
133 m_fAllocationOutOfDate = TRUE;
137 BOOL AGGREGATE::RefreshFilesets (BOOL fNotify, ULONG *pStatus)
139 BOOL rc = TRUE;
140 DWORD status = 0;
142 if (m_fFilesetsOutOfDate)
144 m_fFilesetsOutOfDate = FALSE;
146 // We'll need m_as.dwID to do this.
148 if (!RefreshStatus (fNotify, pStatus))
149 return FALSE;
151 if (fNotify)
152 NOTIFYCALLBACK::SendNotificationToAll (evtRefreshFilesetsBegin, GetIdentifier());
154 // First thing is to forget about what filesets we think we have now.
156 for (LPENUM pEnum = m_lFilesets->FindLast(); pEnum; pEnum = pEnum->FindPrevious())
158 LPFILESET lpFileset = (LPFILESET)(pEnum->GetObject());
159 lpFileset->SendDeleteNotifications();
160 m_lFilesets->Remove (lpFileset);
161 Delete (lpFileset);
164 // Next, the harder part: look through the server to find a list
165 // of filesets.
167 LPSERVER lpServer;
168 if ((lpServer = OpenServer (&status)) == NULL)
169 rc = FALSE;
170 else
172 PVOID hCell;
173 PVOID hVOS;
174 if ((hVOS = lpServer->OpenVosObject (&hCell, &status)) == NULL)
175 rc = FALSE;
176 else
178 WORKERPACKET wpBegin;
179 wpBegin.wpVosVolumeGetBegin.hCell = hCell;
180 wpBegin.wpVosVolumeGetBegin.hServer = hVOS;
181 wpBegin.wpVosVolumeGetBegin.idPartition = m_idPartition;
183 if (!Worker_DoTask (wtaskVosVolumeGetBegin, &wpBegin, &status))
184 rc = FALSE;
185 else
187 for (;;)
189 WORKERPACKET wpNext;
190 wpNext.wpVosVolumeGetNext.hEnum = wpBegin.wpVosVolumeGetBegin.hEnum;
191 if (!Worker_DoTask (wtaskVosVolumeGetNext, &wpNext, &status))
193 if (status == ADMITERATORDONE)
194 status = 0;
195 else
196 rc = FALSE;
197 break;
200 LPTSTR pszName = AnsiToString (wpNext.wpVosVolumeGetNext.Data.name);
201 LPFILESET lpFileset = New2 (FILESET,(this, &wpNext.wpVosVolumeGetNext.Data.id, pszName));
202 FreeString (pszName, wpNext.wpVosVolumeGetNext.Data.name);
204 lpFileset->SetStatusFromVOS (&wpNext.wpVosVolumeGetNext.Data);
205 lpFileset->m_wGhost |= GHOST_HAS_SERVER_ENTRY;
206 m_lFilesets->Add (lpFileset);
207 NOTIFYCALLBACK::SendNotificationToAll (evtCreate, lpFileset->GetIdentifier());
210 WORKERPACKET wpDone;
211 wpDone.wpVosVolumeGetDone.hEnum = wpBegin.wpVosVolumeGetBegin.hEnum;
212 Worker_DoTask (wtaskVosVolumeGetDone, &wpDone);
215 lpServer->CloseVosObject();
218 lpServer->Close();
221 InvalidateAllocation();
223 if (fNotify)
224 NOTIFYCALLBACK::SendNotificationToAll (evtRefreshFilesetsEnd, GetIdentifier(), ((rc) ? 0 : status));
227 if (pStatus && !rc)
228 *pStatus = status;
229 return TRUE;
233 BOOL AGGREGATE::RefreshStatus (BOOL fNotify, ULONG *pStatus)
235 BOOL rc = TRUE;
236 DWORD status = 0;
238 if (m_fStatusOutOfDate)
240 m_fStatusOutOfDate = FALSE;
242 if (fNotify)
243 NOTIFYCALLBACK::SendNotificationToAll (evtRefreshStatusBegin, GetIdentifier());
245 LPSERVER lpServer;
246 if ((lpServer = OpenServer (&status)) == NULL)
247 rc = FALSE;
248 else
250 PVOID hCell;
251 PVOID hVOS;
252 if ((hVOS = lpServer->OpenVosObject (&hCell, &status)) == NULL)
253 rc = FALSE;
254 else
256 WORKERPACKET wp;
257 wp.wpVosPartitionGet.hCell = hCell;
258 wp.wpVosPartitionGet.hServer = hVOS;
259 wp.wpVosPartitionGet.idPartition = m_idPartition;
261 if (!Worker_DoTask (wtaskVosPartitionGet, &wp, &status))
262 rc = FALSE;
263 else
265 m_as.ckStorageTotal = wp.wpVosPartitionGet.Data.totalSpace;
266 m_as.ckStorageFree = wp.wpVosPartitionGet.Data.totalFreeSpace;
269 m_as.dwID = GetID();
270 lpServer->CloseVosObject();
273 lpServer->Close();
276 if (fNotify)
277 NOTIFYCALLBACK::SendNotificationToAll (evtRefreshStatusEnd, GetIdentifier(), ((rc) ? 0 : status));
280 if (rc && m_fAllocationOutOfDate)
282 if (fNotify)
283 NOTIFYCALLBACK::SendNotificationToAll (evtRefreshStatusBegin, GetIdentifier());
285 m_as.ckStorageAllocated = CalculateAllocation (fNotify);
286 m_fAllocationOutOfDate = FALSE;
288 if (fNotify)
289 NOTIFYCALLBACK::SendNotificationToAll (evtRefreshStatusEnd, GetIdentifier());
292 if (pStatus && !rc)
293 *pStatus = status;
294 return TRUE;
298 size_t AGGREGATE::CalculateAllocation (BOOL fNotify)
300 size_t ckAllocated = 0;
302 for (LPENUM pEnum = m_lFilesets->FindFirst(); pEnum; pEnum = pEnum->FindNext())
304 LPFILESET lpFileset = (LPFILESET)(pEnum->GetObject());
306 FILESETSTATUS fs;
307 if (lpFileset->GetStatus (&fs))
309 if (fs.Type == ftREADWRITE)
310 ckAllocated += fs.ckQuota;
314 return ckAllocated;
318 void AGGREGATE::GetName (LPTSTR pszName)
320 lstrcpy (pszName, m_szName);
324 void AGGREGATE::GetDevice (LPTSTR pszDevice)
326 lstrcpy (pszDevice, m_szDevice);
330 LPCELL AGGREGATE::OpenCell (ULONG *pStatus)
332 return m_lpiCell->OpenCell (pStatus);
336 LPSERVER AGGREGATE::OpenServer (ULONG *pStatus)
338 return m_lpiServer->OpenServer (pStatus);
343 * FILESETS ___________________________________________________________________
347 LPFILESET AGGREGATE::OpenFileset (LPTSTR pszName, ULONG *pStatus)
349 if (!RefreshFilesets (TRUE, pStatus))
350 return NULL;
352 LPFILESET lpFileset;
353 if ((lpFileset = (LPFILESET)(m_lkFilesetName->GetFirstObject (pszName))) != NULL)
354 AfsClass_Enter();
356 return lpFileset;
360 LPFILESET AGGREGATE::OpenFileset (VOLUMEID *pvidFileset, ULONG *pStatus)
362 if (!RefreshFilesets (TRUE, pStatus))
363 return NULL;
365 LPFILESET lpFileset;
366 if ((lpFileset = (LPFILESET)(m_lkFilesetID->GetFirstObject (pvidFileset))) != NULL)
367 AfsClass_Enter();
369 return lpFileset;
373 LPFILESET AGGREGATE::FilesetFindFirst (HENUM *phEnum, BOOL fNotify, ULONG *pStatus)
375 return FilesetFindFirst (phEnum, NULL, fNotify, pStatus);
379 LPFILESET AGGREGATE::FilesetFindFirst (HENUM *phEnum, LPIDENT lpiFind, BOOL fNotify, ULONG *pStatus)
381 LPFILESET lpFileset = NULL;
383 if (!RefreshFilesets (fNotify, pStatus))
384 return NULL;
386 if (lpiFind != NULL)
388 lpFileset = lpiFind->OpenFileset();
389 *phEnum = NULL;
391 else if ((*phEnum = m_lFilesets->FindFirst()) != NULL)
393 lpFileset = (LPFILESET)( (*phEnum)->GetObject() );
394 AfsClass_Enter();
397 if (!lpFileset && pStatus)
398 *pStatus = ERROR_FILE_NOT_FOUND;
399 return lpFileset;
403 LPFILESET AGGREGATE::FilesetFindNext (HENUM *phEnum)
405 LPFILESET lpFileset = NULL;
407 if (*phEnum)
409 if ((*phEnum = (*phEnum)->FindNext()) != NULL)
411 lpFileset = (LPFILESET)( (*phEnum)->GetObject() );
412 AfsClass_Enter();
416 return lpFileset;
420 void AGGREGATE::FilesetFindClose (HENUM *phEnum)
422 if (*phEnum)
424 Delete (*phEnum);
425 *phEnum = NULL;
430 BOOL AGGREGATE::GetStatus (LPAGGREGATESTATUS lpas, BOOL fNotify, ULONG *pStatus)
432 if (!RefreshStatus (fNotify, pStatus))
433 return FALSE;
435 memcpy (lpas, &m_as, sizeof(AGGREGATESTATUS));
436 return TRUE;
440 short AGGREGATE::GetGhostStatus (void)
442 return m_wGhost;
446 PVOID AGGREGATE::GetUserParam (void)
448 return GetIdentifier()->GetUserParam();
452 void AGGREGATE::SetUserParam (PVOID pUserNew)
454 GetIdentifier()->SetUserParam (pUserNew);
458 int AGGREGATE::GetID (void)
460 if (m_idPartition == NO_PARTITION)
462 WORKERPACKET wp;
463 wp.wpVosPartitionNameToId.pszPartition = m_szName;
465 ULONG status;
466 if (Worker_DoTask (wtaskVosPartitionNameToId, &wp, &status))
467 m_idPartition = wp.wpVosPartitionNameToId.idPartition;
470 return m_idPartition;
475 * HASH KEYS __________________________________________________________________
479 BOOL CALLBACK AGGREGATE::KeyFilesetName_Compare (LPHASHLISTKEY pKey, PVOID pObject, PVOID pData)
481 return !lstrcmp (((LPFILESET)pObject)->m_szName, (LPTSTR)pData);
484 HASHVALUE CALLBACK AGGREGATE::KeyFilesetName_HashObject (LPHASHLISTKEY pKey, PVOID pObject)
486 return AGGREGATE::KeyFilesetName_HashData (pKey, ((LPFILESET)pObject)->m_szName);
489 HASHVALUE CALLBACK AGGREGATE::KeyFilesetName_HashData (LPHASHLISTKEY pKey, PVOID pData)
491 return HashString ((LPTSTR)pData);
495 BOOL CALLBACK AGGREGATE::KeyFilesetID_Compare (LPHASHLISTKEY pKey, PVOID pObject, PVOID pData)
497 return !memcmp (&((LPFILESET)pObject)->m_idVolume, (LPVOLUMEID)pData, sizeof(VOLUMEID));
500 HASHVALUE CALLBACK AGGREGATE::KeyFilesetID_HashObject (LPHASHLISTKEY pKey, PVOID pObject)
502 return AGGREGATE::KeyFilesetID_HashData (pKey, &((LPFILESET)pObject)->m_idVolume);
505 HASHVALUE CALLBACK AGGREGATE::KeyFilesetID_HashData (LPHASHLISTKEY pKey, PVOID pData)
507 return (HASHVALUE)*((LPVOLUMEID)pData);