Windows: Direct IO Support for Service
[openafs.git] / src / WINNT / afsd / cm_daemon.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
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  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12 #include <roken.h>
13
14 #include <afs/stds.h>
15
16 #include <windows.h>
17 #include <winsock2.h>
18 #include <iphlpapi.h>
19 #include <stdlib.h>
20 #include <malloc.h>
21 #include <string.h>
22
23 #include "afsd.h"
24 #include "smb.h"
25
26 #include <rx/rx.h>
27 #include <rx/rx_prototypes.h>
28 #include <WINNT/afsreg.h>
29
30 #include "afsicf.h"
31
32 /* in seconds */
33 long cm_daemonCheckDownInterval  = 180;
34 long cm_daemonCheckUpInterval    = 240;
35 long cm_daemonCheckVolInterval   = 3600;
36 long cm_daemonCheckCBInterval    = 60;
37 long cm_daemonCheckVolCBInterval = 0;
38 long cm_daemonCheckLockInterval  = 60;
39 long cm_daemonTokenCheckInterval = 180;
40 long cm_daemonCheckOfflineVolInterval = 600;
41 long cm_daemonPerformanceTuningInterval = 0;
42 long cm_daemonRankServerInterval = 600;
43 long cm_daemonRDRShakeExtentsInterval = 0;
44 long cm_daemonAfsdHookReloadInterval = 0;
45 long cm_daemonEAccesCheckInterval = 1800;
46
47 typedef struct daemon_state {
48     osi_rwlock_t lock;
49     afs_uint32   queueCount;
50     cm_bkgRequest_t *head;
51     cm_bkgRequest_t *tail;
52     afs_uint64   completeCount;
53     afs_uint64   retryCount;
54     afs_uint64   errorCount;
55 } daemon_state_t;
56
57 daemon_state_t *cm_daemons = NULL;
58 int cm_nDaemons = 0;
59
60 extern int powerStateSuspended;
61 int daemon_ShutdownFlag = 0;
62 static time_t lastIPAddrChange = 0;
63
64 static EVENT_HANDLE cm_Daemon_ShutdownEvent = NULL;
65 static EVENT_HANDLE cm_LockDaemon_ShutdownEvent = NULL;
66 static EVENT_HANDLE cm_IPAddrDaemon_ShutdownEvent = NULL;
67 static EVENT_HANDLE cm_BkgDaemon_ShutdownEvent[CM_MAX_DAEMONS] =
68        {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
69
70 void * cm_IpAddrDaemon(void * vparm)
71 {
72     extern void smb_CheckVCs(void);
73     char * name = "cm_IPAddrDaemon_ShutdownEvent";
74
75     cm_IPAddrDaemon_ShutdownEvent = thrd_CreateEvent(NULL, FALSE, FALSE, name);
76     if ( GetLastError() == ERROR_ALREADY_EXISTS )
77         afsi_log("Event Object Already Exists: %s", name);
78
79     rx_StartClientThread();
80
81     while (daemon_ShutdownFlag == 0) {
82         DWORD Result;
83
84         thrd_SetEvent(cm_IPAddrDaemon_ShutdownEvent);
85         Result = NotifyAddrChange(NULL,NULL);
86         if (Result == NO_ERROR && daemon_ShutdownFlag == 0) {
87             lastIPAddrChange = osi_Time();
88             if (smb_Enabled)
89                 smb_SetLanAdapterChangeDetected();
90             cm_SetLanAdapterChangeDetected();
91             thrd_ResetEvent(cm_IPAddrDaemon_ShutdownEvent);
92
93             cm_ServerClearRPCStats();
94         }
95     }
96
97     thrd_SetEvent(cm_IPAddrDaemon_ShutdownEvent);
98     pthread_exit(NULL);
99     return NULL;
100 }
101
102 afs_int32 cm_RequestWillBlock(cm_bkgRequest_t *rp)
103 {
104     afs_int32 willBlock = 0;
105
106     if (rp->procp == cm_BkgStore) {
107         /*
108          * If the datastoring flag is set, it means that another
109          * thread is already performing an exclusive store operation
110          * on this file.  The exclusive state will be cleared once
111          * the file server locks the vnode.  Therefore, at most two
112          * threads can be actively involved in storing data at a time
113          * on a file.
114          */
115         lock_ObtainRead(&rp->scp->rw);
116         willBlock = (rp->scp->flags & CM_SCACHEFLAG_DATASTORING);
117         lock_ReleaseRead(&rp->scp->rw);
118     }
119     else if (rp->procp == RDR_BkgFetch || rp->procp == cm_BkgPrefetch) {
120         /*
121          * Attempt to determine if there is a conflict on the requested
122          * range of the file.  If the first in the range does not exist
123          * in the cache assume there is no conflict.  If the buffer does
124          * exist, check to see if an I/O operation is in progress
125          * by using the writing and reading flags as an indicator.
126          */
127         cm_buf_t *bufp = NULL;
128         rock_BkgFetch_t *rockp = (rock_BkgFetch_t *)rp->rockp;
129
130         bufp = buf_Find(&rp->scp->fid, &rockp->base);
131         if (bufp) {
132             willBlock = (bufp->flags & (CM_BUF_WRITING|CM_BUF_READING));
133             buf_Release(bufp);
134         }
135     }
136
137     return willBlock;
138 }
139
140 void * cm_BkgDaemon(void * vparm)
141 {
142     cm_bkgRequest_t *rp;
143     afs_int32 code;
144     char name[32] = "";
145     long daemonID = (long)(LONG_PTR)vparm;
146
147     snprintf(name, sizeof(name), "cm_BkgDaemon_ShutdownEvent%u", daemonID);
148
149     cm_BkgDaemon_ShutdownEvent[daemonID] = thrd_CreateEvent(NULL, FALSE, FALSE, name);
150     if ( GetLastError() == ERROR_ALREADY_EXISTS )
151         afsi_log("Event Object Already Exists: %s", name);
152
153     rx_StartClientThread();
154
155     lock_ObtainWrite(&cm_daemons[daemonID].lock);
156     while (daemon_ShutdownFlag == 0) {
157         int willBlock = 0;
158
159         if (powerStateSuspended) {
160             Sleep(1000);
161             continue;
162         }
163         if (!cm_daemons[daemonID].tail) {
164             osi_SleepW((LONG_PTR)&cm_daemons[daemonID].head, &cm_daemons[daemonID].lock);
165             lock_ObtainWrite(&cm_daemons[daemonID].lock);
166             continue;
167         }
168
169         /* we found a request */
170         for (rp = cm_daemons[daemonID].tail; rp; rp = (cm_bkgRequest_t *) osi_QPrev(&rp->q))
171         {
172             if (rp->scp->flags & CM_SCACHEFLAG_DELETED)
173                 break;
174
175             /*
176              * If the request has active I/O such that this worker would
177              * be forced to block, leave the request in the queue and move
178              * on to one that might be available for servicing.
179              */
180             if (cm_RequestWillBlock(rp)) {
181                 willBlock++;
182                 continue;
183             }
184
185             if (cm_ServerAvailable(&rp->scp->fid, rp->userp))
186                 break;
187         }
188
189         if (rp == NULL) {
190             /*
191              * Couldn't find a request that we could process at the
192              * current time.  If there were requests that would cause
193              * the worker to block, sleep for 25ms so it can promptly
194              * respond when it is available.  Otherwise, sleep for 1s.
195              *
196              * This polling cycle needs to be replaced with a proper
197              * producer/consumer dynamic worker pool.
198              */
199             osi_Log2(afsd_logp,"cm_BkgDaemon[%u] sleeping %dms all tasks would block",
200                      daemonID, willBlock ? 100 : 1000);
201
202             lock_ReleaseWrite(&cm_daemons[daemonID].lock);
203             Sleep(willBlock ? 100 : 1000);
204             lock_ObtainWrite(&cm_daemons[daemonID].lock);
205             continue;
206         }
207
208         osi_QRemoveHT((osi_queue_t **) &cm_daemons[daemonID].head, (osi_queue_t **) &cm_daemons[daemonID].tail, &rp->q);
209         osi_assertx(cm_daemons[daemonID].queueCount-- > 0, "cm_bkgQueueCount 0");
210         lock_ReleaseWrite(&cm_daemons[daemonID].lock);
211
212         osi_Log2(afsd_logp,"cm_BkgDaemon[%u] processing request 0x%p", daemonID, rp);
213
214         if (rp->scp->flags & CM_SCACHEFLAG_DELETED) {
215             osi_Log2(afsd_logp,"cm_BkgDaemon[%u] DELETED scp 0x%x", daemonID, rp->scp);
216             code = CM_ERROR_BADFD;
217             if (rp->procp == cm_BkgDirectWrite) {
218                 cm_BkgDirectWriteDone(rp->scp, rp->rockp, code);
219             }
220         } else {
221 #ifdef DEBUG_REFCOUNT
222             osi_Log3(afsd_logp,"cm_BkgDaemon[%u] (before) scp 0x%x ref %d", daemonID, rp->scp, rp->scp->refCount);
223 #endif
224             code = (*rp->procp)(rp->scp, rp->rockp, rp->userp, &rp->req);
225 #ifdef DEBUG_REFCOUNT
226             osi_Log3(afsd_logp,"cm_BkgDaemon[%u] (after) scp 0x%x ref %d", daemonID, rp->scp, rp->scp->refCount);
227 #endif
228         }
229
230         /*
231          * Keep the following list synchronized with the
232          * error code list in cm_BkgStore.
233          * cm_SyncOpDone(CM_SCACHESYNC_ASYNCSTORE) will be called there unless
234          * one of these errors has occurred.
235          */
236         switch ( code ) {
237         case CM_ERROR_TIMEDOUT: /* or server restarting */
238         case CM_ERROR_RETRY:
239         case CM_ERROR_WOULDBLOCK:
240         case CM_ERROR_ALLBUSY:
241         case CM_ERROR_ALLDOWN:
242         case CM_ERROR_ALLOFFLINE:
243         case CM_ERROR_PARTIALWRITE:
244             if (rp->procp == cm_BkgStore ||
245                 rp->procp == cm_BkgDirectWrite ||
246                 rp->procp == RDR_BkgFetch) {
247                 osi_Log3(afsd_logp,
248                          "cm_BkgDaemon[%u] re-queueing failed request 0x%p code 0x%x",
249                          daemonID, rp, code);
250                 lock_ObtainWrite(&cm_daemons[daemonID].lock);
251                 cm_daemons[daemonID].queueCount++;
252                 cm_daemons[daemonID].retryCount++;
253                 osi_QAddT((osi_queue_t **) &cm_daemons[daemonID].head, (osi_queue_t **)&cm_daemons[daemonID].tail, &rp->q);
254                 break;
255             } /* otherwise fall through */
256         case 0:  /* success */
257         default: /* other error */
258             if (code == 0) {
259                 osi_Log2(afsd_logp,"cm_BkgDaemon[%u] SUCCESS: request 0x%p", daemonID, rp);
260                 cm_daemons[daemonID].completeCount++;
261             } else {
262                 osi_Log3(afsd_logp,"cm_BkgDaemon[%u] FAILED: request dropped 0x%p code 0x%x",
263                          daemonID, rp, code);
264                 cm_daemons[daemonID].errorCount++;
265             }
266             cm_ReleaseUser(rp->userp);
267             cm_ReleaseSCache(rp->scp);
268             free(rp->rockp);
269             free(rp);
270             lock_ObtainWrite(&cm_daemons[daemonID].lock);
271         }
272     }
273     lock_ReleaseWrite(&cm_daemons[daemonID].lock);
274     thrd_SetEvent(cm_BkgDaemon_ShutdownEvent[daemonID]);
275     pthread_exit(NULL);
276     return NULL;
277 }
278
279 int cm_QueueBKGRequest(cm_scache_t *scp, cm_bkgProc_t *procp, void *rockp,
280                         cm_user_t *userp, cm_req_t *reqp)
281 {
282     cm_bkgRequest_t *rp, *rpq;
283     afs_uint32 daemonID;
284     int duplicate = 0;
285
286     rp = malloc(sizeof(*rp));
287     memset(rp, 0, sizeof(*rp));
288
289     cm_HoldSCache(scp);
290     rp->scp = scp;
291     cm_HoldUser(userp);
292     rp->userp = userp;
293     rp->procp = procp;
294     rp->rockp = rockp;
295     rp->req = *reqp;
296
297     /* Use separate queues for fetch and store operations */
298     daemonID = scp->fid.hash % (cm_nDaemons/2) * 2;
299     if (procp == cm_BkgStore ||
300         procp == cm_BkgDirectWrite)
301         daemonID++;
302
303     /* Check to see if this is a duplicate request */
304     lock_ObtainWrite(&cm_daemons[daemonID].lock);
305     if ( procp == cm_BkgStore || procp == RDR_BkgFetch || procp == cm_BkgPrefetch ) {
306         for (rpq = cm_daemons[daemonID].head; rpq; rpq = (cm_bkgRequest_t *) osi_QNext(&rpq->q))
307         {
308             if ( rpq->procp == procp &&
309                  rpq->scp == scp &&
310                  rpq->userp == userp)
311             {
312                 if (rp->procp == cm_BkgStore) {
313                     rock_BkgStore_t *rock1p = (rock_BkgStore_t *)rp->rockp;
314                     rock_BkgStore_t *rock2p = (rock_BkgStore_t *)rpq->rockp;
315
316                     duplicate = (memcmp(rock1p, rock2p, sizeof(*rock1p)) == 0);
317                 }
318                 else if (rp->procp == RDR_BkgFetch || rp->procp == cm_BkgPrefetch) {
319                     rock_BkgFetch_t *rock1p = (rock_BkgFetch_t *)rp->rockp;
320                     rock_BkgFetch_t *rock2p = (rock_BkgFetch_t *)rpq->rockp;
321
322                     duplicate = (memcmp(rock1p, rock2p, sizeof(*rock1p)) == 0);
323                 }
324
325                 if (duplicate) {
326                     /* found a duplicate; update request with latest info */
327                     break;
328                 }
329             }
330         }
331     }
332
333     if (!duplicate) {
334         cm_daemons[daemonID].queueCount++;
335         osi_QAddH((osi_queue_t **) &cm_daemons[daemonID].head, (osi_queue_t **)&cm_daemons[daemonID].tail, &rp->q);
336     }
337     lock_ReleaseWrite(&cm_daemons[daemonID].lock);
338
339     if (duplicate) {
340         cm_ReleaseSCache(scp);
341         cm_ReleaseUser(userp);
342         free(rp->rockp);
343         free(rp);
344         return -1;
345     } else {
346         osi_Wakeup((LONG_PTR) &cm_daemons[daemonID].head);
347         return 0;
348     }
349 }
350
351 static int
352 IsWindowsFirewallPresent(void)
353 {
354     SC_HANDLE scm;
355     SC_HANDLE svc;
356     BOOLEAN flag;
357     BOOLEAN result = FALSE;
358     LPQUERY_SERVICE_CONFIG pConfig = NULL;
359     DWORD BufSize;
360     LONG status;
361
362     /* Open services manager */
363     scm = OpenSCManager(NULL, NULL, GENERIC_READ);
364     if (!scm) return FALSE;
365
366     /* Open Windows Firewall service */
367     svc = OpenService(scm, "MpsSvc", SERVICE_QUERY_CONFIG);
368     if (!svc) {
369         afsi_log("MpsSvc Service could not be opened for query: 0x%x", GetLastError());
370         svc = OpenService(scm, "SharedAccess", SERVICE_QUERY_CONFIG);
371         if (!svc)
372             afsi_log("SharedAccess Service could not be opened for query: 0x%x", GetLastError());
373     }
374     if (!svc)
375         goto close_scm;
376
377     /* Query Windows Firewall service config, first just to get buffer size */
378     /* Expected to fail, so don't test return value */
379     (void) QueryServiceConfig(svc, NULL, 0, &BufSize);
380     status = GetLastError();
381     if (status != ERROR_INSUFFICIENT_BUFFER)
382         goto close_svc;
383
384     /* Allocate buffer */
385     pConfig = (LPQUERY_SERVICE_CONFIG)GlobalAlloc(GMEM_FIXED,BufSize);
386     if (!pConfig)
387         goto close_svc;
388
389     /* Query Windows Firewall service config, this time for real */
390     flag = QueryServiceConfig(svc, pConfig, BufSize, &BufSize);
391     if (!flag) {
392         afsi_log("QueryServiceConfig failed: 0x%x", GetLastError());
393         goto free_pConfig;
394     }
395
396     /* Is it autostart? */
397     afsi_log("AutoStart 0x%x", pConfig->dwStartType);
398     if (pConfig->dwStartType < SERVICE_DEMAND_START)
399         result = TRUE;
400
401   free_pConfig:
402     GlobalFree(pConfig);
403   close_svc:
404     CloseServiceHandle(svc);
405   close_scm:
406     CloseServiceHandle(scm);
407
408     return result;
409 }
410
411 void
412 cm_DaemonCheckInit(void)
413 {
414     HKEY parmKey;
415     DWORD dummyLen;
416     DWORD dummy;
417     DWORD code;
418
419     code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY,
420                          0, KEY_QUERY_VALUE, &parmKey);
421     if (code)
422         return;
423
424     dummyLen = sizeof(DWORD);
425     code = RegQueryValueEx(parmKey, "daemonCheckDownInterval", NULL, NULL,
426                             (BYTE *) &dummy, &dummyLen);
427     if (code == ERROR_SUCCESS && dummy)
428         cm_daemonCheckDownInterval = dummy;
429     afsi_log("daemonCheckDownInterval is %d", cm_daemonCheckDownInterval);
430
431     dummyLen = sizeof(DWORD);
432     code = RegQueryValueEx(parmKey, "daemonCheckUpInterval", NULL, NULL,
433                             (BYTE *) &dummy, &dummyLen);
434     if (code == ERROR_SUCCESS && dummy)
435         cm_daemonCheckUpInterval = dummy;
436     afsi_log("daemonCheckUpInterval is %d", cm_daemonCheckUpInterval);
437
438     dummyLen = sizeof(DWORD);
439     code = RegQueryValueEx(parmKey, "daemonCheckVolInterval", NULL, NULL,
440                             (BYTE *) &dummy, &dummyLen);
441     if (code == ERROR_SUCCESS && dummy)
442         cm_daemonCheckVolInterval = dummy;
443     afsi_log("daemonCheckVolInterval is %d", cm_daemonCheckVolInterval);
444
445     dummyLen = sizeof(DWORD);
446     code = RegQueryValueEx(parmKey, "daemonCheckCBInterval", NULL, NULL,
447                             (BYTE *) &dummy, &dummyLen);
448     if (code == ERROR_SUCCESS && dummy)
449         cm_daemonCheckCBInterval = dummy;
450     afsi_log("daemonCheckCBInterval is %d", cm_daemonCheckCBInterval);
451
452     dummyLen = sizeof(DWORD);
453     code = RegQueryValueEx(parmKey, "daemonCheckVolCBInterval", NULL, NULL,
454                             (BYTE *) &dummy, &dummyLen);
455     if (code == ERROR_SUCCESS && dummy)
456         cm_daemonCheckVolCBInterval = dummy;
457     afsi_log("daemonCheckVolCBInterval is %d", cm_daemonCheckVolCBInterval);
458
459     dummyLen = sizeof(DWORD);
460     code = RegQueryValueEx(parmKey, "daemonCheckLockInterval", NULL, NULL,
461                             (BYTE *) &dummy, &dummyLen);
462     if (code == ERROR_SUCCESS && dummy)
463         cm_daemonCheckLockInterval = dummy;
464     afsi_log("daemonCheckLockInterval is %d", cm_daemonCheckLockInterval);
465
466     dummyLen = sizeof(DWORD);
467     code = RegQueryValueEx(parmKey, "daemonCheckTokenInterval", NULL, NULL,
468                             (BYTE *) &dummy, &dummyLen);
469     if (code == ERROR_SUCCESS && dummy)
470         cm_daemonTokenCheckInterval = dummy;
471     afsi_log("daemonCheckTokenInterval is %d", cm_daemonTokenCheckInterval);
472
473     dummyLen = sizeof(DWORD);
474     code = RegQueryValueEx(parmKey, "daemonCheckOfflineVolInterval", NULL, NULL,
475                             (BYTE *) &dummy, &dummyLen);
476     if (code == ERROR_SUCCESS && dummy)
477         cm_daemonCheckOfflineVolInterval = dummy;
478     afsi_log("daemonCheckOfflineVolInterval is %d", cm_daemonCheckOfflineVolInterval);
479
480     dummyLen = sizeof(DWORD);
481     code = RegQueryValueEx(parmKey, "daemonRDRShakeExtentsInterval", NULL, NULL,
482                             (BYTE *) &dummy, &dummyLen);
483     if (code == ERROR_SUCCESS && dummy)
484         cm_daemonRDRShakeExtentsInterval = dummy;
485     afsi_log("daemonRDRShakeExtentsInterval is %d", cm_daemonRDRShakeExtentsInterval);
486
487     dummyLen = sizeof(DWORD);
488     code = RegQueryValueEx(parmKey, "daemonPerformanceTuningInterval", NULL, NULL,
489                             (BYTE *) &dummy, &dummyLen);
490     if (code == ERROR_SUCCESS)
491         cm_daemonPerformanceTuningInterval = dummy;
492     afsi_log("daemonPerformanceTuningInterval is %d", cm_daemonPerformanceTuningInterval);
493
494     dummyLen = sizeof(DWORD);
495     code = RegQueryValueEx(parmKey, "daemonRankServerInterval", NULL, NULL,
496                             (BYTE *) &dummy, &dummyLen);
497     if (code == ERROR_SUCCESS && dummy)
498         cm_daemonRankServerInterval = dummy;
499     afsi_log("daemonRankServerInterval is %d", cm_daemonRankServerInterval);
500
501     dummyLen = sizeof(DWORD);
502     code = RegQueryValueEx(parmKey, "daemonAfsdHookReloadInterval", NULL, NULL,
503                             (BYTE *) &dummy, &dummyLen);
504     if (code == ERROR_SUCCESS && dummy)
505         cm_daemonAfsdHookReloadInterval = dummy;
506     afsi_log("daemonAfsdHookReloadInterval is %d", cm_daemonAfsdHookReloadInterval);
507
508     RegCloseKey(parmKey);
509
510     if (cm_daemonPerformanceTuningInterval)
511         cm_PerformanceTuningInit();
512 }
513
514 /* periodic lock check daemon */
515 void * cm_LockDaemon(void * vparm)
516 {
517     time_t now;
518     time_t lastLockCheck;
519     char * name = "cm_LockDaemon_ShutdownEvent";
520
521     cm_LockDaemon_ShutdownEvent = thrd_CreateEvent(NULL, FALSE, FALSE, name);
522     if ( GetLastError() == ERROR_ALREADY_EXISTS )
523         afsi_log("Event Object Already Exists: %s", name);
524
525     now = osi_Time();
526     lastLockCheck = now - cm_daemonCheckLockInterval/2 + (rand() % cm_daemonCheckLockInterval);
527
528     while (daemon_ShutdownFlag == 0) {
529         if (powerStateSuspended) {
530             Sleep(1000);
531             continue;
532         }
533
534         now = osi_Time();
535
536         if (now > lastLockCheck + cm_daemonCheckLockInterval &&
537             daemon_ShutdownFlag == 0 &&
538             powerStateSuspended == 0) {
539             lastLockCheck = now;
540             cm_CheckLocks();
541             if (daemon_ShutdownFlag == 1)
542                 break;
543         }
544
545         thrd_Sleep(1000);               /* sleep 1 second */
546     }
547     thrd_SetEvent(cm_LockDaemon_ShutdownEvent);
548     pthread_exit(NULL);
549     return NULL;
550 }
551
552 /* periodic check daemon */
553 void * cm_Daemon(void *vparm)
554 {
555     time_t now;
556     time_t lastVolCheck;
557     time_t lastCBExpirationCheck;
558     time_t lastVolCBRenewalCheck;
559     time_t lastDownServerCheck;
560     time_t lastUpServerCheck;
561     time_t lastTokenCacheCheck;
562     time_t lastBusyVolCheck;
563     time_t lastPerformanceCheck;
564     time_t lastServerRankCheck;
565     time_t lastRDRShakeExtents;
566     time_t lastAfsdHookReload;
567     time_t lastEAccesCheck;
568     char thostName[200];
569     unsigned long code;
570     struct hostent *thp;
571     HMODULE hHookDll = NULL;
572     AfsdDaemonHook daemonHook = NULL;
573     char * name = "cm_Daemon_ShutdownEvent";
574     int configureFirewall = IsWindowsFirewallPresent();
575     int bAddrChangeCheck = 0;
576
577     cm_Daemon_ShutdownEvent = thrd_CreateEvent(NULL, FALSE, FALSE, name);
578     if ( GetLastError() == ERROR_ALREADY_EXISTS )
579         afsi_log("Event Object Already Exists: %s", name);
580
581     if (!configureFirewall) {
582         afsi_log("No Windows Firewall detected");
583     }
584
585     if (cm_freelanceEnabled && cm_freelanceImportCellServDB)
586         cm_FreelanceImportCellServDB();
587
588     /* ping all file servers, up or down, with unauthenticated connection,
589      * to find out whether we have all our callbacks from the server still.
590      * Also, ping down VLDBs.
591      */
592     /*
593      * Seed the random number generator with our own address, so that
594      * clients starting at the same time don't all do vol checks at the
595      * same time.
596      */
597     gethostname(thostName, sizeof(thostName));
598     thp = gethostbyname(thostName);
599     if (thp == NULL)    /* In djgpp, gethostname returns the netbios
600                            name of the machine.  gethostbyname will fail
601                            looking this up if it differs from DNS name. */
602         code = 0;
603     else
604         memcpy(&code, thp->h_addr_list[0], 4);
605
606     srand(ntohl(code));
607
608     cm_DaemonCheckInit();
609
610     now = osi_Time();
611     lastVolCheck = now - cm_daemonCheckVolInterval/2 + (rand() % cm_daemonCheckVolInterval);
612     lastCBExpirationCheck = now - cm_daemonCheckCBInterval/2 + (rand() % cm_daemonCheckCBInterval);
613     if (cm_daemonCheckVolCBInterval)
614         lastVolCBRenewalCheck = now - cm_daemonCheckVolCBInterval/2 + (rand() % cm_daemonCheckVolCBInterval);
615     lastDownServerCheck = now - cm_daemonCheckDownInterval/2 + (rand() % cm_daemonCheckDownInterval);
616     lastUpServerCheck = now - cm_daemonCheckUpInterval/2 + (rand() % cm_daemonCheckUpInterval);
617     lastTokenCacheCheck = now - cm_daemonTokenCheckInterval/2 + (rand() % cm_daemonTokenCheckInterval);
618     if (cm_daemonCheckOfflineVolInterval)
619         lastBusyVolCheck = now - cm_daemonCheckOfflineVolInterval/2 * (rand() % cm_daemonCheckOfflineVolInterval);
620     if (cm_daemonPerformanceTuningInterval)
621         lastPerformanceCheck = now - cm_daemonPerformanceTuningInterval/2 * (rand() % cm_daemonPerformanceTuningInterval);
622     lastServerRankCheck = now - cm_daemonRankServerInterval/2 * (rand() % cm_daemonRankServerInterval);
623     if (cm_daemonRDRShakeExtentsInterval)
624         lastRDRShakeExtents = now - cm_daemonRDRShakeExtentsInterval/2 * (rand() % cm_daemonRDRShakeExtentsInterval);
625     if (cm_daemonAfsdHookReloadInterval)
626         lastAfsdHookReload = now;
627     lastEAccesCheck = now;
628
629     hHookDll = cm_LoadAfsdHookLib();
630     if (hHookDll)
631         daemonHook = ( AfsdDaemonHook ) GetProcAddress(hHookDll, AFSD_DAEMON_HOOK);
632
633     while (daemon_ShutdownFlag == 0) {
634         if (powerStateSuspended) {
635             Sleep(1000);
636             continue;
637         }
638         /* check to see if the listener threads halted due to network
639          * disconnect or other issues.  If so, attempt to restart them.
640          */
641         smb_RestartListeners(0);
642
643         if (daemon_ShutdownFlag == 1)
644             break;
645
646         if (configureFirewall) {
647             /* Open Microsoft Firewall to allow in port 7001 */
648             switch (icf_CheckAndAddAFSPorts(AFS_PORTSET_CLIENT)) {
649             case 0:
650                 afsi_log("Windows Firewall Configuration succeeded");
651                 configureFirewall = 0;
652                 break;
653             case 1:
654                 afsi_log("Invalid Windows Firewall Port Set");
655                 break;
656             case 2:
657                 afsi_log("Unable to open Windows Firewall Profile");
658                 break;
659             case 3:
660                 afsi_log("Unable to create/modify Windows Firewall Port entries");
661                 break;
662             default:
663                 afsi_log("Unknown Windows Firewall Configuration error");
664             }
665         }
666
667         /* find out what time it is */
668         now = osi_Time();
669
670         /* Determine whether an address change took place that we need to respond to */
671         if (bAddrChangeCheck)
672             bAddrChangeCheck = 0;
673
674         if (lastIPAddrChange != 0 && lastIPAddrChange + 2500 < now) {
675             bAddrChangeCheck = 1;
676             lastIPAddrChange = 0;
677         }
678
679         /* check down servers */
680         if ((bAddrChangeCheck || now > lastDownServerCheck + cm_daemonCheckDownInterval) &&
681             daemon_ShutdownFlag == 0 &&
682             powerStateSuspended == 0) {
683             lastDownServerCheck = now;
684             osi_Log0(afsd_logp, "cm_Daemon CheckDownServers");
685             cm_CheckServers(CM_FLAG_CHECKDOWNSERVERS, NULL);
686             if (daemon_ShutdownFlag == 1)
687                 break;
688             now = osi_Time();
689         }
690
691         if (bAddrChangeCheck &&
692             daemon_ShutdownFlag == 0 &&
693             powerStateSuspended == 0) {
694             cm_ForceNewConnectionsAllServers();
695         }
696
697         /* check up servers */
698         if ((bAddrChangeCheck || now > lastUpServerCheck + cm_daemonCheckUpInterval) &&
699             daemon_ShutdownFlag == 0 &&
700             powerStateSuspended == 0) {
701             lastUpServerCheck = now;
702             osi_Log0(afsd_logp, "cm_Daemon CheckUpServers");
703             cm_CheckServers(CM_FLAG_CHECKUPSERVERS, NULL);
704             if (daemon_ShutdownFlag == 1)
705                 break;
706             now = osi_Time();
707         }
708
709         if (bAddrChangeCheck &&
710             daemon_ShutdownFlag == 0 &&
711             powerStateSuspended == 0) {
712             smb_CheckVCs();
713             cm_VolStatus_Network_Addr_Change();
714         }
715
716         /*
717          * Once every five minutes inspect the volume list and enforce
718          * the volume location expiration time.
719          */
720         if (now > lastVolCheck + 300 &&
721             daemon_ShutdownFlag == 0 &&
722             powerStateSuspended == 0) {
723             lastVolCheck = now;
724             cm_RefreshVolumes(cm_daemonCheckVolInterval);
725             if (daemon_ShutdownFlag == 1)
726                 break;
727             now = osi_Time();
728         }
729
730         /* Rank all up servers */
731         if ((now > lastServerRankCheck + cm_daemonRankServerInterval) &&
732             daemon_ShutdownFlag == 0 &&
733             powerStateSuspended == 0) {
734             lastServerRankCheck = now;
735             osi_Log0(afsd_logp, "cm_Daemon RankServer");
736             cm_RankUpServers();
737             if(daemon_ShutdownFlag == 1)
738                 break;
739             now = osi_Time();
740         }
741
742         if (cm_daemonCheckVolCBInterval &&
743             now > lastVolCBRenewalCheck + cm_daemonCheckVolCBInterval &&
744             daemon_ShutdownFlag == 0 &&
745             powerStateSuspended == 0) {
746             lastVolCBRenewalCheck = now;
747             cm_VolumeRenewROCallbacks();
748             if (daemon_ShutdownFlag == 1)
749                 break;
750             now = osi_Time();
751         }
752
753         if ((bAddrChangeCheck || (cm_daemonCheckOfflineVolInterval &&
754                                   now > lastBusyVolCheck + cm_daemonCheckOfflineVolInterval)) &&
755             daemon_ShutdownFlag == 0 &&
756             powerStateSuspended == 0) {
757             lastBusyVolCheck = now;
758             cm_CheckOfflineVolumes();
759             if (daemon_ShutdownFlag == 1)
760                 break;
761             now = osi_Time();
762         }
763
764         if (now > lastCBExpirationCheck + cm_daemonCheckCBInterval &&
765             daemon_ShutdownFlag == 0 &&
766             powerStateSuspended == 0) {
767             lastCBExpirationCheck = now;
768             cm_CheckCBExpiration();
769             if (daemon_ShutdownFlag == 1)
770                 break;
771             now = osi_Time();
772         }
773
774         if (now > lastTokenCacheCheck + cm_daemonTokenCheckInterval &&
775             daemon_ShutdownFlag == 0 &&
776             powerStateSuspended == 0) {
777             lastTokenCacheCheck = now;
778             cm_CheckTokenCache(now);
779             if (daemon_ShutdownFlag == 1)
780                 break;
781             now = osi_Time();
782         }
783
784         if (now > lastEAccesCheck + cm_daemonEAccesCheckInterval &&
785              daemon_ShutdownFlag == 0 &&
786              powerStateSuspended == 0) {
787             lastEAccesCheck = now;
788             cm_EAccesClearOutdatedEntries();
789             if (daemon_ShutdownFlag == 1)
790                 break;
791             now = osi_Time();
792         }
793
794         if (cm_daemonRDRShakeExtentsInterval &&
795             now > lastRDRShakeExtents + cm_daemonRDRShakeExtentsInterval &&
796             daemon_ShutdownFlag == 0 &&
797             powerStateSuspended == 0) {
798             cm_req_t req;
799             cm_InitReq(&req);
800             lastRDRShakeExtents = now;
801             if (cm_data.buf_redirCount > cm_data.buf_freeCount)
802                 buf_RDRShakeSomeExtentsFree(&req, FALSE, 10 /* seconds */);
803             if (daemon_ShutdownFlag == 1)
804                 break;
805             now = osi_Time();
806         }
807
808         /* allow an exit to be called prior to stopping the service */
809         if (cm_daemonAfsdHookReloadInterval &&
810             lastAfsdHookReload != 0 && lastAfsdHookReload < now) {
811             if (hHookDll) {
812                 FreeLibrary(hHookDll);
813                 hHookDll = NULL;
814                 daemonHook = NULL;
815             }
816
817             hHookDll = cm_LoadAfsdHookLib();
818             if (hHookDll)
819                 daemonHook = ( AfsdDaemonHook ) GetProcAddress(hHookDll, AFSD_DAEMON_HOOK);
820         }
821
822         if (daemonHook)
823         {
824             BOOL hookRc = daemonHook();
825
826             if (hookRc == FALSE)
827             {
828                 SetEvent(WaitToTerminate);
829             }
830
831             if (daemon_ShutdownFlag == 1) {
832                 break;
833             }
834             now = osi_Time();
835         }
836
837         if (cm_daemonPerformanceTuningInterval &&
838             now > lastPerformanceCheck + cm_daemonPerformanceTuningInterval &&
839             daemon_ShutdownFlag == 0 &&
840             powerStateSuspended == 0) {
841             lastPerformanceCheck = now;
842             cm_PerformanceTuningCheck();
843             if (daemon_ShutdownFlag == 1)
844                 break;
845             now = osi_Time();
846         }
847
848         /*
849          * sleep .5 seconds.  if the thread blocks for a long time
850          * we risk not being able to close the cache before Windows
851          * kills our process during system shutdown.
852          */
853         thrd_Sleep(500);
854     }
855
856     if (hHookDll) {
857         FreeLibrary(hHookDll);
858     }
859
860     thrd_SetEvent(cm_Daemon_ShutdownEvent);
861     pthread_exit(NULL);
862     return NULL;
863 }
864
865 void cm_DaemonShutdown(void)
866 {
867     int i;
868     DWORD code;
869
870     daemon_ShutdownFlag = 1;
871
872     /* wait for shutdown */
873     for ( i=0; i<cm_nDaemons; i++) {
874         osi_Wakeup((LONG_PTR) &cm_daemons[i].head);
875         if (cm_BkgDaemon_ShutdownEvent[i])
876             code = thrd_WaitForSingleObject_Event(cm_BkgDaemon_ShutdownEvent[i], INFINITE);
877     }
878
879     if (cm_Daemon_ShutdownEvent)
880         code = thrd_WaitForSingleObject_Event(cm_Daemon_ShutdownEvent, INFINITE);
881
882     if (cm_LockDaemon_ShutdownEvent)
883         code = thrd_WaitForSingleObject_Event(cm_LockDaemon_ShutdownEvent, INFINITE);
884
885 #if 0
886     /*
887      * Do not waste precious time waiting for the ipaddr daemon to shutdown.
888      * When it does it means we have lost our network connection and we need
889      * it during cache shutdown in order to notify the file servers that this
890      * client is giving up all callbacks.
891      */
892     if (cm_IPAddrDaemon_ShutdownEvent)
893         code = thrd_WaitForSingleObject_Event(cm_IPAddrDaemon_ShutdownEvent, INFINITE);
894 #endif
895 }
896
897 void cm_InitDaemon(int nDaemons)
898 {
899     static osi_once_t once;
900     pthread_t phandle;
901     pthread_attr_t tattr;
902     int pstatus;
903     int i;
904
905     pthread_attr_init(&tattr);
906     pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
907
908     if (nDaemons > CM_MAX_DAEMONS)
909         cm_nDaemons = CM_MAX_DAEMONS;
910     else if (nDaemons < CM_MIN_DAEMONS)
911         cm_nDaemons = CM_MIN_DAEMONS;
912     else
913         cm_nDaemons = (nDaemons / 2) * 2; /* must be divisible by two */
914
915     if (osi_Once(&once)) {
916         /* creating IP Address Change monitor daemon */
917         pstatus = pthread_create(&phandle, &tattr, cm_IpAddrDaemon, 0);
918         osi_assertx(pstatus == 0, "cm_IpAddrDaemon thread creation failure");
919
920         /* creating pinging daemon */
921         pstatus = pthread_create(&phandle, &tattr, cm_Daemon, 0);
922         osi_assertx(pstatus == 0, "cm_Daemon thread creation failure");
923
924         pstatus = pthread_create(&phandle, &tattr, cm_LockDaemon, 0);
925         osi_assertx(pstatus == 0, "cm_LockDaemon thread creation failure");
926
927         cm_daemons = malloc(nDaemons * sizeof(daemon_state_t));
928
929         for(i=0; i < cm_nDaemons; i++) {
930             lock_InitializeRWLock(&cm_daemons[i].lock, "cm_daemonLock",
931                                   LOCK_HIERARCHY_DAEMON_GLOBAL);
932             cm_daemons[i].head = cm_daemons[i].tail = NULL;
933             cm_daemons[i].queueCount=0;
934             cm_daemons[i].completeCount=0;
935             cm_daemons[i].retryCount=0;
936             cm_daemons[i].errorCount=0;
937             pstatus = pthread_create(&phandle, &tattr, cm_BkgDaemon, (LPVOID)(LONG_PTR)i);
938             osi_assertx(pstatus == 0, "cm_BkgDaemon thread creation failure");
939         }
940         osi_EndOnce(&once);
941     }
942
943     pthread_attr_destroy(&tattr);
944 }