b982b58971b131afbf3c10f6fd1b310a0344c836
[openafs.git] / src / viced / viced.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 /*  viced.c     - File Server main loop                                  */
11 /*                                                                       */
12 /*  Date: 5/1/85                                                         */
13 /*                                                                       */
14 /*  Function    - This routine has the initialization code for           */
15 /*                FileServer II                                          */
16 /*                                                                       */
17 /* ********************************************************************** */
18
19 #include <afsconfig.h>
20 #include <afs/param.h>
21
22 RCSID("$Header$");
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <errno.h>
27 #include <sys/types.h>
28 #include <afs/procmgmt.h>  /* signal(), kill(), wait(), etc. */
29 #include <sys/stat.h>
30 #ifdef AFS_NT40_ENV
31 #include <fcntl.h>
32 #include <io.h>
33 #include <windows.h>
34 #include <WINNT/afsevent.h>
35 #else
36 #include <sys/file.h>
37 #include <netinet/in.h>
38 #include <netdb.h>
39 #include <unistd.h>     /* sysconf() */
40
41 #ifdef HAVE_STRING_H
42 #include <string.h>
43 #else
44 #ifdef HAVE_STRINGS_H
45 #include <strings.h>
46 #endif
47 #endif
48
49 #ifndef ITIMER_REAL
50 #include <sys/time.h>
51 #endif /* ITIMER_REAL */
52 #include <sys/resource.h>
53 #endif /* AFS_NT40_ENV */
54 #include <afs/stds.h>
55 #undef SHARED
56 #include <rx/xdr.h>
57 #include <afs/nfs.h>
58 #ifdef AFS_PTHREAD_ENV
59 #include <assert.h>
60 #else /* AFS_PTHREAD_ENV */
61 #include <afs/assert.h>
62 #endif /* AFS_PTHREAD_ENV */
63 #include <lwp.h>
64 #include <lock.h>
65 #include <afs/ptclient.h>
66 #include <afs/afsint.h>
67 #include <afs/vldbint.h>
68 #include <afs/errors.h>
69 #include <afs/ihandle.h>
70 #include <afs/vnode.h>
71 #include <afs/volume.h>
72 #include <afs/auth.h>
73 #include <afs/cellconfig.h>
74 #include <afs/acl.h>
75 #include <afs/prs_fs.h>
76 #include <rx/rx.h>
77 #include <rx/rxkad.h>
78 #include <afs/keys.h>
79 #include <afs/afs_args.h>
80 #include <afs/vlserver.h>
81 #include <afs/afsutil.h>
82 #include <afs/fileutil.h>
83 #ifndef AFS_NT40_ENV
84 #include <afs/netutils.h>
85 #endif
86 #include "viced.h"
87 #include "host.h"
88 #ifdef AFS_PTHREAD_ENV
89 #include "softsig.h"
90 #endif
91 #if defined(AFS_SGI_ENV)
92 #include "sys/schedctl.h"
93 #include "sys/lock.h"
94 #endif
95 #include <rx/rx_globals.h>
96
97
98 extern int      BreakVolumeCallBacks(), InitCallBack();
99 extern int      BreakVolumeCallBacks(), InitCallBack(), BreakLaterCallBacks();
100 extern int BreakVolumeCallBacksLater();
101 extern  int     LogLevel, etext;
102 extern afs_int32        BlocksSpare, PctSpare;
103
104 void            ShutDown(void);
105 static void     ClearXStatValues(), NewParms(), PrintCounters();
106 static void     ResetCheckDescriptors(void), ResetCheckSignal(void);
107 static void     CheckSignal(void);
108 extern  int     GetKeysFromToken();
109 extern int RXAFS_ExecuteRequest();
110 extern int RXSTATS_ExecuteRequest();
111
112 int eventlog = 0, rxlog = 0;
113 FILE *debugFile;
114 FILE * console = NULL;
115
116 #ifdef AFS_PTHREAD_ENV
117 pthread_mutex_t fsync_glock_mutex;
118 pthread_cond_t fsync_cond;
119 #else 
120 char fsync_wait[1];
121 #endif /* AFS_PTHREAD_ENV */
122
123 #ifdef AFS_NT40_ENV
124 #define AFS_QUIETFS_ENV 1
125 #define NT_OPEN_MAX    1024 /* This is an arbitrary no. we came up with for 
126                              * now. We hope this will be replaced by a more
127                              * intelligent estimate later. */
128 #endif
129
130 int     SystemId;               /* ViceID of "Administrators"*/
131 int     SystemAnyUser;          /* Viceid of "System:AnyUser" */
132 prlist  SystemAnyUserCPS;       /* CPS for "system:AnyUser */
133 int     AnonymousID = 0;        /* ViceId of "Anonymous"*/
134 prlist  AnonCPS;                /* CPS for "Anonymous"*/
135
136 struct afsconf_dir *confDir;    /* Configuration dir object */
137
138 int     restartMode = RESTART_ORDINARY;
139
140 int Testing = 0; /* for ListViceInodes */
141
142 /*
143  * Home for the performance statistics.
144  */
145 struct afs_PerfStats afs_perfstats;
146
147 extern  int     LogLevel;
148 extern  int     Statistics;
149
150 int     busyonrst = 1;
151 int     timeout = 30;
152 int     SawSpare;
153 int     SawPctSpare;
154 int     debuglevel = 0;
155 int     printBanner = 0;
156 int     rxJumbograms = 1; /* default is to send and receive jumbograms. */
157 afs_int32       implicitAdminRights = PRSFS_LOOKUP;     /* The ADMINISTER right is 
158                                                    already implied */
159 afs_int32       readonlyServer = 0;
160
161 int     stack = 24;
162 int     stackSize = 24000;
163 int     fiveminutes = 300;              /* 5 minutes.  Change this for debugging only */
164 int     CurrentConnections = 0;
165 int     hostaclRefresh = 7200;          /* refresh host clients' acls every 2 hrs */
166 #if defined(AFS_SGI_ENV)
167 int     SawLock;
168 #endif
169 time_t  StartTime;
170
171 int     rxpackets = 150;        /* 100 */
172 int     nSmallVns = 400;        /* 200 */
173 int     large = 400;            /* 200 */
174 int     volcache = 400;         /* 400 */
175 int     numberofcbs = 60000;    /* 60000 */
176 int     lwps = 9;               /* 6 */
177 int     buffs = 90;             /* 70 */
178 int     novbc = 0;              /* Enable Volume Break calls */
179 int     busy_threshold = 600;
180 int     abort_threshold = 10;
181 int     udpBufSize = 0;         /* UDP buffer size for receive*/
182 int     sendBufSize = 16384;    /* send buffer size */
183
184 struct timeval  tp;
185
186 /*
187  * FileServer's name and IP address, both network byte order and
188  * host byte order.
189  */
190 #define ADDRSPERSITE 16     /* Same global is in rx/rx_user.c */
191
192 char FS_HostName[128] = "localhost";
193 afs_uint32 FS_HostAddr_NBO;
194 afs_uint32 FS_HostAddr_HBO;
195 afs_uint32 FS_HostAddrs[ADDRSPERSITE], FS_HostAddr_cnt = 0, FS_registered=0;
196 /* All addresses in FS_HostAddrs are in NBO */
197 afsUUID FS_HostUUID;
198
199 static void FlagMsg();
200
201 /*
202  * Home for the performance statistics.
203  */
204
205 /* DEBUG HACK */
206 static void CheckDescriptors()
207 {
208 #ifndef AFS_NT40_ENV
209     struct stat status;
210     register int tsize = getdtablesize();
211     register int i;
212     for (i = 0; i<tsize; i++) {
213         if (fstat(i, &status) != -1) {
214             printf("%d: dev %x, inode %u, length %u, type/mode %x\n",
215                    i, status.st_dev, status.st_ino,
216                    status.st_size, status.st_mode);
217         }
218     }
219     fflush(stdout);
220     ResetCheckDescriptors();
221 #endif
222 } /*CheckDescriptors*/
223
224
225 #ifdef AFS_PTHREAD_ENV
226 void CheckSignal_Signal(x)       {CheckSignal();}
227 void ShutDown_Signal(x)          {ShutDown();}
228 void CheckDescriptors_Signal(x)  {CheckDescriptors();}
229 #else /* AFS_PTHREAD_ENV */
230 void CheckSignal_Signal(x)       {IOMGR_SoftSig(CheckSignal, 0);}
231 void ShutDown_Signal(x)          {IOMGR_SoftSig(ShutDown, 0);}
232 void CheckDescriptors_Signal(x)  {IOMGR_SoftSig(CheckDescriptors, 0);}
233 #endif /* AFS_PTHREAD_ENV */
234
235 /* check whether caller is authorized to manage RX statistics */
236 int fs_rxstat_userok(struct rx_call *call)
237 {
238     return afsconf_SuperUser(confDir, call, NULL);
239 }
240
241 static void ResetCheckSignal(void)
242 {
243     int signo;
244
245 #if defined(AFS_HPUX_ENV)
246     signo = SIGPOLL;
247 #else
248 #if defined(AFS_NT40_ENV)
249     signo = SIGUSR2;
250 #else
251     signo = SIGXCPU;
252 #endif
253 #endif
254
255 #if defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV)
256     softsig_signal(signo, CheckSignal_Signal);
257 #else
258     signal(signo, CheckSignal_Signal);
259 #endif
260 }
261
262 static void ResetCheckDescriptors(void)
263 {
264 #ifndef AFS_NT40_ENV
265 #if defined(AFS_PTHREAD_ENV)
266     softsig_signal(SIGTERM, CheckDescriptors_Signal);
267 #else
268     signal(SIGTERM, CheckDescriptors_Signal);
269 #endif
270 #endif
271 }
272
273
274 /* proc called by rxkad module to get a key */
275 static int get_key(char *arock, register afs_int32 akvno, char *akey)
276 {
277     /* find the key */
278     static struct afsconf_key tkey;
279     register afs_int32 code;
280
281     if (!confDir) {
282         ViceLog(0, ("conf dir not open\n"));
283         return 1;
284     }
285     code = afsconf_GetKey(confDir, akvno, tkey.key);
286     if (code)
287         return code;
288     memcpy(akey, tkey.key, sizeof(tkey.key));
289     return 0;
290
291 } /*get_key*/
292
293 #ifndef AFS_NT40_ENV
294 int viced_syscall(afs_uint32 a3, afs_uint32 a4, void * a5)
295 {
296   afs_uint32 rcode;
297   void (*old)();
298         
299 #ifndef AFS_LINUX20_ENV
300   old = (void (*)())signal(SIGSYS, SIG_IGN);    
301 #endif
302   rcode = syscall (AFS_SYSCALL, 28 /* AFSCALL_CALL */, a3, a4, a5);
303 #ifndef AFS_LINUX20_ENV
304   signal(SIGSYS, old);  
305 #endif
306
307   return rcode;
308 }
309 #endif
310
311 #if !defined(AFS_NT40_ENV)
312 #include "AFS_component_version_number.c"
313 #endif /* !AFS_NT40_ENV */
314
315 #define MAXADMINNAME 64
316 char adminName[MAXADMINNAME];
317
318 static void
319 CheckAdminName()
320 {
321     int             fd = 0;
322     struct stat     status;
323     
324     if ((stat("/AdminName", &status)) ||        /* if file does not exist */
325         (status.st_size <= 0) ||                /* or it is too short */
326         (status.st_size >= (MAXADMINNAME)) ||   /* or it is too long */
327         !(fd = open("/AdminName", O_RDONLY, 0))) {      /* or the open fails */
328         strcpy(adminName, "System:Administrators");     /* use the default name */
329     }
330     else {
331         read(fd, adminName, status.st_size);    /* use name from the file */
332     }
333     if (fd)
334         close(fd);      /* close fd if it was opened */
335
336 } /*CheckAdminName*/
337
338
339 static void setThreadId(char *s)
340 {
341 #if defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV)
342     /* set our 'thread-id' so that the host hold table works */
343     MUTEX_ENTER(&rx_stats_mutex);   /* protects rxi_pthread_hinum */ 
344     ++rxi_pthread_hinum;
345     pthread_setspecific(rx_thread_id_key, (void *)rxi_pthread_hinum);
346     MUTEX_EXIT(&rx_stats_mutex);
347     ViceLog(0,("Set thread id %d for '%s'\n", pthread_getspecific(rx_thread_id_key), s));
348 #endif
349 }
350
351 /* This LWP does things roughly every 5 minutes */
352 static void FiveMinuteCheckLWP()
353 {
354     static int msg  = 0;
355     char tbuffer[32];
356
357     ViceLog(1, ("Starting five minute check process\n"));
358     setThreadId("FiveMinuteCheckLWP");
359     while (1) {
360 #ifdef AFS_PTHREAD_ENV
361         sleep(fiveminutes);
362 #else /* AFS_PTHREAD_ENV */
363         IOMGR_Sleep(fiveminutes);
364 #endif /* AFS_PTHREAD_ENV */
365
366         /* close the log so it can be removed */
367         ReOpenLog(AFSDIR_SERVER_FILELOG_FILEPATH); /* don't trunc, just append */
368         ViceLog(2, ("Cleaning up timed out callbacks\n"));
369         if(CleanupTimedOutCallBacks())
370             ViceLog(5,("Timed out callbacks deleted\n"));
371         ViceLog(2, ("Set disk usage statistics\n"));
372         VSetDiskUsage();
373         if (FS_registered == 1) Do_VLRegisterRPC();
374         /* Force wakeup in case we missed something; pthreads does timedwait */
375 #ifndef AFS_PTHREAD_ENV
376         LWP_NoYieldSignal(&fsync_wait);
377 #endif
378         if(printBanner && (++msg&1)) { /* Every 10 minutes */
379             time_t now = FT_ApproxTime();
380             if (console != NULL) {
381 #ifndef AFS_QUIETFS_ENV
382                 fprintf(console,"File server is running at %s\r",
383                         afs_ctime(&now, tbuffer, sizeof(tbuffer)));
384 #endif /* AFS_QUIETFS_ENV */
385                 ViceLog(2, ("File server is running at %s\n",
386                         afs_ctime(&now, tbuffer, sizeof(tbuffer))));
387             }
388         }
389     }
390 } /*FiveMinuteCheckLWP*/
391
392
393 /* This LWP does host checks every 5 minutes:  it should not be used for
394  * other 5 minute activities because it may be delayed by timeouts when
395  * it probes the workstations
396  */
397 static void HostCheckLWP()
398 {
399     ViceLog(1, ("Starting Host check process\n"));
400     setThreadId("HostCheckLWP");
401     while(1) {
402 #ifdef AFS_PTHREAD_ENV
403         sleep(fiveminutes);
404 #else /* AFS_PTHREAD_ENV */
405         IOMGR_Sleep(fiveminutes);
406 #endif /* AFS_PTHREAD_ENV */
407         ViceLog(2, ("Checking for dead venii & clients\n"));
408         h_CheckHosts();
409     }
410 } /*HostCheckLWP*/
411
412 /* This LWP does fsync checks every 5 minutes:  it should not be used for
413  * other 5 minute activities because it may be delayed by timeouts when
414  * it probes the workstations
415  */
416 static FsyncCheckLWP()
417 {
418     afs_int32 code;
419 #ifdef AFS_PTHREAD_ENV
420     struct timespec fsync_next;
421 #endif
422     ViceLog(1, ("Starting fsync check process\n"));
423
424     setThreadId("FsyncCheckLWP");
425
426 #ifdef AFS_PTHREAD_ENV
427     assert(pthread_cond_init(&fsync_cond, NULL) == 0);
428     assert(pthread_mutex_init(&fsync_glock_mutex, NULL) == 0);
429 #endif
430
431     FSYNC_LOCK
432     while(1) {
433 #ifdef AFS_PTHREAD_ENV
434         /* rounding is fine */
435         fsync_next.tv_nsec = 0;
436         fsync_next.tv_sec = time(0) + fiveminutes;
437         
438         code = pthread_cond_timedwait(&fsync_cond, &fsync_glock_mutex, 
439                                       &fsync_next);
440         if (code != 0 && code != ETIMEDOUT) 
441             ViceLog(0, ("pthread_cond_timedwait returned %d\n", code));
442 #else /* AFS_PTHREAD_ENV */
443         if (( code = LWP_WaitProcess(&fsync_wait)) != LWP_SUCCESS)
444             ViceLog(0, ("LWP_WaitProcess returned %d\n", code));
445 #endif /* AFS_PTHREAD_ENV */
446         ViceLog(2, ("Checking for fsync events\n"));
447         do {
448             code = BreakLaterCallBacks();
449         } while (code != 0);
450     }
451
452
453 /*------------------------------------------------------------------------
454  * PRIVATE ClearXStatValues
455  *
456  * Description:
457  *      Initialize all of the values collected via the xstat
458  *      interface.
459  *
460  * Arguments:
461  *      None.
462  *
463  * Returns:
464  *      Nothing.
465  *
466  * Environment:
467  *      Must be called during File Server initialization.
468  *
469  * Side Effects:
470  *      As advertised.
471  *------------------------------------------------------------------------*/
472
473 static void ClearXStatValues()
474 { /*ClearXStatValues*/
475
476     struct fs_stats_opTimingData *opTimeP;      /*Ptr to timing struct*/
477     struct fs_stats_xferData *opXferP;          /*Ptr to xfer struct*/
478     int i;                                      /*Loop counter*/
479
480     /*
481      * Zero all xstat-related structures.
482      */
483     memset((char *)(&afs_perfstats), 0, sizeof(struct afs_PerfStats));
484 #if FS_STATS_DETAILED
485     memset((char *)(&afs_FullPerfStats), 0, sizeof(struct fs_stats_FullPerfStats));
486
487     /*
488      * That's not enough.  We have to set reasonable minima for
489      * time and xfer values in the detailed stats.
490      */
491     opTimeP = &(afs_FullPerfStats.det.rpcOpTimes[0]);
492     for (i = 0; i < FS_STATS_NUM_RPC_OPS; i++, opTimeP++)
493         opTimeP->minTime.tv_sec = 999999;
494
495     opXferP = &(afs_FullPerfStats.det.xferOpTimes[0]);
496     for (i = 0; i < FS_STATS_NUM_XFER_OPS; i++, opXferP++) {
497         opXferP->minTime.tv_sec = 999999;
498         opXferP->minBytes = 999999999;
499     }
500
501     /*
502      * There's more.  We have to set our unique system identifier, as
503      * declared in param.h.  If such a thing is not defined, we bitch
504      * and declare ourselves to be an unknown system type.
505      */
506 #ifdef SYS_NAME_ID
507     afs_perfstats.sysname_ID = SYS_NAME_ID;
508 #else
509 #ifndef AFS_NT40_ENV
510     ViceLog(0, ("Sys name ID constant not defined in param.h!!\n"));
511     ViceLog(0, ("[Choosing ``undefined'' sys name ID.\n"));
512 #endif
513     afs_perfstats.sysname_ID = SYS_NAME_ID_UNDEFINED;
514 #endif /* SYS_NAME_ID */
515 #endif
516
517 } /*ClearXStatValues*/
518
519
520 static void PrintCounters()
521 {
522     int dirbuff, dircall, dirio;
523     struct timeval  tpl;
524     int workstations, activeworkstations, delworkstations;
525     int processSize = 0;
526     char tbuffer[32];
527
528     TM_GetTimeOfDay(&tpl, 0);
529     Statistics = 1;
530     ViceLog(0, ("Vice was last started at %s\n",
531                 afs_ctime(&StartTime, tbuffer, sizeof(tbuffer))));
532
533     VPrintCacheStats();
534     VPrintDiskStats();
535     DStat(&dirbuff, &dircall, &dirio);
536     ViceLog(0,("With %d directory buffers; %d reads resulted in %d read I/Os\n",
537             dirbuff, dircall, dirio));
538     rx_PrintStats(stderr);
539     h_PrintStats();
540     PrintCallBackStats();
541 #ifdef AFS_NT40_ENV
542     processSize = -1; /* TODO: */
543 #else
544     processSize = (int)((long) sbrk(0) >> 10);
545 #endif
546     ViceLog(0,("There are %d connections, process size %d\n",  CurrentConnections, processSize));
547     h_GetWorkStats(&workstations, &activeworkstations, &delworkstations,
548          tpl.tv_sec-15*60);
549     ViceLog(0,
550             ("There are %d workstations, %d are active (req in < 15 mins), %d marked \"down\"\n",
551             workstations, activeworkstations, delworkstations));
552     Statistics = 0;
553
554 } /*PrintCounters*/
555
556
557
558 static void CheckSignal()
559 {
560     if (FS_registered > 0)  {
561         /*
562          * We have proper ip addresses; tell the vlserver what we got; the following
563          * routine will do the proper reporting for us
564          */
565         Do_VLRegisterRPC();
566     }
567     h_DumpHosts();
568     h_PrintClients();
569     DumpCallBackState();
570     PrintCounters();
571     ResetCheckSignal();
572
573 } /*CheckSignal*/
574
575 void ShutDownAndCore(int dopanic)
576 {
577     time_t now = time(0);
578     char *tstr;
579     char tbuffer[32];
580
581     ViceLog(0, ("Shutting down file server at %s",
582                 afs_ctime(&now, tbuffer, sizeof(tbuffer))));
583     if (dopanic) 
584         ViceLog(0, ("ABNORMAL SHUTDOWN, see core file.\n"));
585 #ifndef AFS_QUIETFS_ENV
586     if (console != NULL) {
587         fprintf(console,"File server restart/shutdown received at %s\r",
588                 afs_ctime(&now, tbuffer, sizeof(tbuffer)));
589     }
590 #endif
591     DFlush();
592     PrintCounters();
593
594     /* do not allows new reqests to be served from now on, all new requests
595        are returned with an error code of RX_RESTARTING ( transient failure ) */
596     rx_SetRxTranquil();                 /* dhruba */
597     VShutdown();
598
599     if (debugFile) {
600         rx_PrintStats(debugFile);
601         fflush(debugFile);
602     }
603     if (console != NULL) {
604         now = time(0);
605         if (dopanic) {
606 #ifndef AFS_QUIETFS_ENV
607             fprintf(console, "File server has terminated abnormally at %s\r",
608                     afs_ctime(&now, tbuffer, sizeof(tbuffer)));
609 #endif
610             ViceLog(0, ("File server has terminated abnormally at %s\n",
611                 afs_ctime(&now, tbuffer, sizeof(tbuffer))));
612         } else {
613 #ifndef AFS_QUIETFS_ENV
614             fprintf(console, "File server has terminated normally at %s\r",
615                     afs_ctime(&now, tbuffer, sizeof(tbuffer)));
616 #endif
617             ViceLog(0, ("File server has terminated normally at %s\n",
618                 afs_ctime(&now, tbuffer, sizeof(tbuffer))));
619         }
620     }
621
622     exit(0);
623
624 } /*ShutDown*/
625
626 void ShutDown(void) /* backward compatibility */
627 {
628   ShutDownAndCore(DONTPANIC);
629 }
630
631
632 static void FlagMsg()
633 {
634     char buffer[1024];
635
636         /* default supports help flag */
637
638     strcpy(buffer, "Usage: fileserver ");
639     strcat(buffer, "[-d <debug level>] ");
640     strcat(buffer, "[-p <number of processes>] ");
641     strcat(buffer, "[-spare <number of spare blocks>] ");
642     strcat(buffer, "[-pctspare <percentage spare>] ");
643     strcat(buffer, "[-b <buffers>] ");
644     strcat(buffer, "[-l <large vnodes>] ");
645     strcat(buffer, "[-s <small vnodes>] ");
646     strcat(buffer, "[-vc <volume cachesize>] ");
647     strcat(buffer, "[-w <call back wait interval>] ");
648     strcat(buffer, "[-cb <number of call backs>] ");
649     strcat(buffer, "[-banner (print banner every 10 minutes)] ");
650     strcat(buffer, "[-novbc (whole volume cbs disabled)] ");
651     strcat(buffer, "[-implicit <admin mode bits: rlidwka>] ");
652     strcat(buffer, "[-readonly (read-only file server)] ");
653     strcat(buffer, "[-hr <number of hours between refreshing the host cps>] ");
654     strcat(buffer, "[-busyat <redirect clients when queue > n>] ");
655     strcat(buffer, "[-nobusy <no VBUSY before a volume is attached>] ");
656     strcat(buffer, "[-rxpck <number of rx extra packets>] ");
657     strcat(buffer, "[-rxdbg (enable rx debugging)] ");
658     strcat(buffer, "[-rxdbge (enable rxevent debugging)] ");
659 #ifdef  AFS_AIX32_ENV
660     strcat(buffer, "[-m <min percentage spare in partition>] ");
661 #endif
662 #if defined(AFS_SGI_ENV)
663     strcat(buffer, "[-lock (keep fileserver from swapping)] ");
664 #endif
665     strcat(buffer, "[-L (large server conf)] ");
666     strcat(buffer, "[-S (small server conf)] ");
667     strcat(buffer, "[-k <stack size>] ");
668     strcat(buffer, "[-realm <Kerberos realm name>] ");
669     strcat(buffer, "[-udpsize <size of socket buffer in bytes>] ");
670     strcat(buffer, "[-sendsize <size of send buffer in bytes>] ");
671 /*   strcat(buffer, "[-enable_peer_stats] "); */
672 /*   strcat(buffer, "[-enable_process_stats] "); */
673     strcat(buffer, "[-help]\n");
674 /*
675     ViceLog(0, ("%s", buffer));
676 */
677
678         printf("%s",buffer);
679         fflush(stdout);
680
681 } /*FlagMsg*/
682
683
684 static afs_int32 ParseRights(char *arights)
685 {
686     afs_int32 mode = 0;
687     int i, len;
688     char tc;
689
690     if (!arights || !strcmp(arights, "")) {
691         printf("Missing list of mode bits on -implicit option\n");
692         return -1;
693     }
694     if (!strcmp(arights, "none"))
695       mode = 0;
696     else if (!strcmp(arights, "read")) 
697       mode = PRSFS_READ | PRSFS_LOOKUP;
698     else if (!strcmp(arights, "write"))
699       mode = PRSFS_READ | PRSFS_LOOKUP | PRSFS_INSERT | 
700              PRSFS_DELETE | PRSFS_WRITE | PRSFS_LOCK;
701     else if (!strcmp(arights, "all"))
702       mode = PRSFS_READ | PRSFS_LOOKUP | PRSFS_INSERT | 
703              PRSFS_DELETE | PRSFS_WRITE | PRSFS_LOCK | PRSFS_ADMINISTER;
704     else {
705         len = strlen(arights);
706         for(i=0;i<len;i++) {
707             tc = *arights++;
708             if (tc == 'r') mode |= PRSFS_READ;
709             else if (tc == 'l') mode |= PRSFS_LOOKUP;
710             else if (tc == 'i') mode |= PRSFS_INSERT;
711             else if (tc == 'd') mode |= PRSFS_DELETE;
712             else if (tc == 'w') mode |= PRSFS_WRITE;
713             else if (tc == 'k') mode |= PRSFS_LOCK;
714             else if (tc == 'a') mode |= PRSFS_ADMINISTER;
715             else if (tc == 'A') mode |= PRSFS_USR0;
716             else if (tc == 'B') mode |= PRSFS_USR1;
717             else if (tc == 'C') mode |= PRSFS_USR2;
718             else if (tc == 'D') mode |= PRSFS_USR3;
719             else if (tc == 'E') mode |= PRSFS_USR4;
720             else if (tc == 'F') mode |= PRSFS_USR5;
721             else if (tc == 'G') mode |= PRSFS_USR6;
722             else if (tc == 'H') mode |= PRSFS_USR7;
723             else {
724                 printf("Illegal -implicit rights character '%c'.\n", tc);
725                 return -1;
726             }
727         }
728     }
729     return mode;
730 }
731
732 /*
733  * Limit MAX_FILESERVER_THREAD by the system limit on the number of
734  * pthreads (sysconf(_SC_THREAD_THREADS_MAX)), if applicable and
735  * available.
736  *
737  * AIX:         sysconf() limit is real
738  * HP-UX:       sysconf() limit is real
739  * IRIX:        sysconf() limit is apparently NOT real -- too small
740  * DUX:         sysconf() limit is apparently NOT real -- too big
741  * Linux:       sysconf() limit is apparently NOT real -- too big
742  * Solaris:     no sysconf() limit
743  */
744 static int
745 max_fileserver_thread(void)
746 {
747 #if defined(AFS_PTHREAD_ENV)
748 #if defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV)
749         long ans;
750
751         ans = sysconf(_SC_THREAD_THREADS_MAX);
752         if (0 < ans && ans < MAX_FILESERVER_THREAD)
753                 return (int) ans;
754 #endif
755 #endif /* defined(AFS_PTHREAD_ENV) */
756         return MAX_FILESERVER_THREAD;
757 }
758
759 static int ParseArgs(int argc, char *argv[])
760 {
761     int SawL=0, SawS=0, SawVC=0;
762     int Sawrxpck = 0, Sawsmall=0, Sawlarge=0, Sawcbs=0, Sawlwps=0, Sawbufs=0;
763     int Sawbusy=0;
764     int i;
765     int bufSize = 0;      /* temp variable to read in udp socket buf size*/
766
767     for (i = 1; i < argc; i++) {
768         if (!strcmp(argv[i], "-d")) {
769             debuglevel = atoi(argv[++i]);
770             LogLevel = debuglevel;
771         }
772         else
773             if (!strcmp(argv[i], "-banner")) {
774                 printBanner = 1;
775         } else
776             if (!strcmp(argv[i], "-implicit")) {
777                 implicitAdminRights = ParseRights(argv[++i]);
778                 if (implicitAdminRights < 0) return implicitAdminRights;
779         } else
780             if (!strcmp(argv[i], "-readonly")) {
781                 readonlyServer = 1;
782         } else
783             if (!strcmp(argv[i], "-L")) {
784                 SawL = 1;
785         } else
786             if (!strcmp(argv[i], "-S")) {
787                 SawS = 1;
788         }
789         else
790             if (!strcmp(argv[i], "-p")) {
791                 int lwps_max = max_fileserver_thread() - FILESERVER_HELPER_THREADS;
792                 Sawlwps = 1;
793                 lwps = atoi(argv[++i]);
794                 if (lwps > lwps_max)
795                     lwps = lwps_max;
796                 else if (lwps < 6)
797                     lwps = 6;
798             }
799         else
800             if (!strcmp(argv[i], "-b")) {
801                 Sawbufs = 1;
802                 buffs = atoi(argv[++i]);
803             }
804         else
805             if (!strcmp(argv[i], "-l")) {
806                 Sawlarge = 1;
807                 large = atoi(argv[++i]);
808             }
809         else
810             if (!strcmp(argv[i], "-vc")) {
811                 SawVC = 1;
812                 volcache = atoi(argv[++i]);
813             }
814         else
815             if (!strcmp(argv[i], "-novbc")) {
816                 novbc = 1;
817             }
818         else
819             if (!strcmp(argv[i], "-rxpck")) {
820                 Sawrxpck = 1;
821                 rxpackets = atoi(argv[++i]);
822             }
823         else
824             if (!strcmp(argv[i], "-s")) {
825                 Sawsmall = 1;
826                 nSmallVns = atoi(argv[++i]);
827             }
828         else    
829             if (!strcmp(argv[i], "-abortthreshold")) {
830                 abort_threshold = atoi(argv[++i]);
831             }
832         else    
833             if (!strcmp(argv[i], "-k"))
834                 stack = atoi(argv[++i]);
835 #if defined(AFS_SGI_ENV)
836         else
837             if (!strcmp(argv[i], "-lock")) {
838                 SawLock = 1;
839             }
840 #endif
841         else
842             if (!strcmp(argv[i], "-spare")) {
843                 BlocksSpare = atoi(argv[++i]);
844                 SawSpare = 1;
845             }
846         else
847             if (!strcmp(argv[i], "-pctspare")) {
848                 PctSpare = atoi(argv[++i]);
849                 BlocksSpare = 0;                /* has non-zero default */
850                 SawPctSpare = 1;
851             }
852         else
853             if (!strcmp(argv[i], "-w"))
854                 fiveminutes = atoi(argv[++i]);
855         else
856             if (!strcmp(argv[i], "-hr")) {
857                 int hr = atoi(argv[++i]);
858                 if ((hr < 1) || (hr > 36)) {
859                     printf("host acl refresh interval of %d hours is invalid; hours must be between 1 and 36\n\n",
860                            hr);
861                     return -1;
862                 }                   
863                 hostaclRefresh = hr*60*60;
864         } else
865             if (!strcmp(argv[i], "-rxdbg"))
866                 rxlog = 1;
867         else 
868             if (!strcmp(argv[i], "-rxdbge"))
869                 eventlog = 1;
870         else
871             if (!strcmp(argv[i], "-cb")) {
872                 Sawcbs = 1;
873                 numberofcbs = atoi(argv[++i]);
874                 if ((numberofcbs < 10000) || (numberofcbs > 2147483647)) {
875                     printf("number of cbs %d invalid; must be between 10000 and 2147483647\n",
876                            numberofcbs);
877                     return -1;
878                 }
879             }
880             else
881               if (!strcmp(argv[i], "-busyat")) {
882                 Sawbusy = 1;
883                 busy_threshold = atoi(argv[++i]);
884                 if (busy_threshold < 10) {
885                     printf("Busy threshold %d is too low, will compute default.\n",
886                            busy_threshold);
887                     Sawbusy = 0;
888                 }
889               }
890             else
891               if (!strcmp(argv[i], "-nobusy")) 
892                 busyonrst=0;
893 #ifdef  AFS_AIX32_ENV
894         else
895             if (!strcmp(argv[i], "-m")) {
896                 extern int aixlow_water;
897                  aixlow_water = atoi(argv[++i]);
898                 if ((aixlow_water < 0) || (aixlow_water > 30)) {
899                     printf("space reserved %d% invalid; must be between 0-30%\n", aixlow_water);
900                     return -1;
901                 }
902             }
903 #endif
904         else
905             if (!strcmp(argv[i], "-nojumbo")) {
906                 rxJumbograms = 0;
907             }
908         else
909             if (!strcmp(argv[i], "-realm")) {
910                 extern char local_realm[AFS_REALM_SZ];
911                 if (strlen(argv[++i]) >= AFS_REALM_SZ) {
912                     printf("-realm argument must contain fewer than %d characters.\n",AFS_REALM_SZ);
913                     return -1;
914                 }
915                 strncpy (local_realm, argv[i], AFS_REALM_SZ);
916             }
917         else
918             if ( !strcmp(argv[i], "-udpsize")) {
919                 if ( (i+1) >= argc ) {
920                     printf("You have to specify -udpsize <integer value>\n");
921                     return -1;
922                 }
923                 bufSize = atoi(argv[++i]);
924                 if ( bufSize < rx_GetMinUdpBufSize() )
925                     printf("Warning:udpsize %d is less than minimum %d; ignoring\n",
926                                 bufSize, rx_GetMinUdpBufSize() );
927                 else
928                     udpBufSize = bufSize;
929             }
930         else
931             if ( !strcmp(argv[i], "-sendsize")) {
932                 if ( (i+1) >= argc ) {
933                     printf("You have to specify -sendsize <integer value>\n");
934                     return -1;
935                 }
936                 bufSize = atoi(argv[++i]);
937                 if ( bufSize < 16384 )
938                     printf("Warning:sendsize %d is less than minimum %d; ignoring\n",
939                                 bufSize, 16384);
940                 else
941                     sendBufSize = bufSize;
942             }
943         else
944             if (!strcmp(argv[i], "-enable_peer_stats")) {
945                 rx_enablePeerRPCStats();
946             }
947         else
948             if (!strcmp(argv[i], "-enable_process_stats")) {
949                 rx_enableProcessRPCStats();
950             }
951 #ifndef AFS_NT40_ENV
952         else 
953             if (strcmp(argv[i], "-syslog")==0) {
954                 /* set syslog logging flag */
955                 serverLogSyslog = 1;
956             } 
957         else 
958             if (strncmp(argv[i], "-syslog=", 8)==0) {
959                 serverLogSyslog = 1;
960                 serverLogSyslogFacility = atoi(argv[i]+8);
961             }
962 #endif
963         else {
964             return(-1);
965         }
966     }
967     if (SawS && SawL) {
968         printf("Only one of -L, or -S must be specified\n");
969         return -1;
970     }
971     if (SawS) {
972         if (!Sawrxpck) rxpackets = 100;
973         if (!Sawsmall) nSmallVns = 200;
974         if (!Sawlarge) large = 200;
975         if (!Sawcbs) numberofcbs = 20000;
976         if (!Sawlwps) lwps = 6;
977         if (!Sawbufs) buffs = 70;
978         if (!SawVC) volcache = 200;
979     }
980     if (SawL) {
981         if (!Sawrxpck) rxpackets = 200;
982         if (!Sawsmall) nSmallVns = 600;
983         if (!Sawlarge) large = 600;
984         if (!Sawcbs) numberofcbs = 64000;
985         if (!Sawlwps) lwps = 12;
986         if (!Sawbufs) buffs = 120;
987         if (!SawVC) volcache = 600;
988     }
989     if (!Sawbusy) 
990         busy_threshold = 3*rxpackets/2;
991
992     return(0);
993
994 } /*ParseArgs*/
995
996
997 #define MAXPARMS 15
998
999 static void NewParms(int initializing)
1000 {
1001     static struct stat sbuf;
1002     register afs_offs_t i;
1003     register int fd;
1004     char *parms;
1005     char *argv[MAXPARMS];
1006     register int argc;
1007
1008     if (!(stat("/vice/file/parms",&sbuf))) {
1009         parms = (char *)malloc(sbuf.st_size);
1010         if(!parms) return;
1011         fd = open("parms", O_RDONLY, 0666);
1012         if(fd <= 0) {
1013             ViceLog(0, ("Open for parms failed with errno = %d\n", errno));
1014             return;
1015         }
1016
1017         i = read(fd, parms, sbuf.st_size);
1018         close(fd);
1019         if(i != sbuf.st_size) {
1020             if (i < 0 ) {
1021                 ViceLog(0, ("Read on parms failed with errno = %d\n", errno));
1022             } else {
1023                 ViceLog(0,
1024                         ("Read on parms failed; expected %d bytes but read %d\n",
1025                         sbuf.st_size, i));
1026             }
1027             free(parms);
1028             return;
1029         }
1030
1031         for (i = 0;i < MAXPARMS; argv[i++] = 0 );
1032         
1033         for (argc = i = 0; i < sbuf.st_size; i++) {
1034             if ((*(parms + i) != ' ') && (*(parms + i) != '\n')){
1035                 if(argv[argc] == 0) argv[argc] = (parms+i);
1036             }
1037             else {
1038                 *(parms + i) = '\0';
1039                 if(argv[argc] != 0) {
1040                     if(++argc == MAXPARMS) break;
1041                 }
1042                 while ((*(parms + i + 1) == ' ') || (*(parms + i + 1) == '\n'))
1043                     i++;
1044             }
1045         }
1046         if(ParseArgs(argc, argv) == 0) {
1047             ViceLog(0, ("Change parameters to:"));
1048         } else {
1049             ViceLog(0, ("Invalid parameter in:"));
1050         }
1051         for(i = 0; i < argc; i++) {
1052             ViceLog(0, (" %s", argv[i]));
1053         }
1054         ViceLog(0,("\n"));
1055         free(parms);
1056     }
1057     else
1058         if(!initializing)
1059             ViceLog(0, ("Received request to change parms but no parms file exists\n"));
1060
1061 } /*NewParms*/
1062
1063
1064 /* Miscellaneous routines */
1065 void Die (char *msg)
1066 {
1067     ViceLog (0,("%s\n", msg));
1068     assert(0);
1069
1070 } /*Die*/
1071
1072
1073 afs_int32 InitPR()
1074 {
1075     register code;
1076
1077     /*
1078      * If this fails, it's because something major is wrong, and is not
1079      * likely to be time dependent.
1080      */
1081     code = pr_Initialize(2, AFSDIR_SERVER_ETC_DIRPATH, 0);
1082     if (code != 0) {
1083         ViceLog(0, ("Couldn't initialize protection library; code=%d.\n", code));
1084         return code; 
1085     }
1086     SystemId = SYSADMINID;
1087     SystemAnyUser = ANYUSERID;
1088     SystemAnyUserCPS.prlist_len = 0;
1089     SystemAnyUserCPS.prlist_val = NULL;
1090     AnonCPS.prlist_len = 0;
1091     AnonCPS.prlist_val = NULL;
1092     while (1) {
1093         code = pr_GetCPS(SystemAnyUser, &SystemAnyUserCPS);
1094         if (code != 0) {
1095             ViceLog(0,
1096                     ("Couldn't get CPS for AnyUser, will try again in 30 seconds; code=%d.\n",
1097                     code));
1098             goto sleep;
1099         }
1100         code = pr_GetCPS(ANONYMOUSID,&AnonCPS);
1101         if (code != 0) {
1102             ViceLog(0,("Couldn't get Anonymous CPS, exiting; code=%d.\n", code));
1103             return -1;
1104         }
1105         AnonymousID = ANONYMOUSID;
1106         return 0;
1107 sleep:
1108 #ifdef AFS_PTHREAD_ENV
1109         sleep(30);
1110 #else /* AFS_PTHREAD_ENV */
1111         IOMGR_Sleep(30);
1112 #endif /* AFS_PTHREAD_ENV */
1113     }
1114 } /*InitPR*/
1115
1116 struct rx_connection *serverconns[MAXSERVERS];
1117 struct ubik_client *cstruct;
1118
1119 afs_int32 vl_Initialize(char *confDir)
1120 {   afs_int32 code, scIndex = 0, i;
1121     struct afsconf_dir *tdir;
1122     struct rx_securityClass *sc;
1123     struct afsconf_cell info;
1124
1125     tdir = afsconf_Open(confDir);
1126     if (!tdir) {
1127         ViceLog(0, ("Could not open configuration directory (%s).\n", confDir));
1128         exit(1);
1129     }
1130     code = afsconf_ClientAuth(tdir, &sc, &scIndex);
1131     if (code) {
1132         ViceLog(0, ("Could not get security object for localAuth\n"));
1133         exit(1);
1134     }
1135     code = afsconf_GetCellInfo(tdir,NULL, AFSCONF_VLDBSERVICE, &info);
1136     if (info.numServers > MAXSERVERS) {
1137         ViceLog(0, ("vl_Initialize: info.numServers=%d (> MAXSERVERS=%d)\n",info.numServers, MAXSERVERS));
1138         exit(1);
1139     }
1140     for (i = 0;i<info.numServers;i++) 
1141         serverconns[i] = rx_NewConnection(info.hostAddr[i].sin_addr.s_addr, info.hostAddr[i].sin_port,
1142                                           USER_SERVICE_ID, sc, scIndex);
1143     code = ubik_ClientInit(serverconns, &cstruct);
1144     if (code) {
1145         ViceLog(0, ("vl_Initialize: ubik client init failed.\n"));
1146         return code;
1147     }
1148     return 0;
1149 }
1150
1151 #define SYSIDMAGIC      0x88aabbcc
1152 #define SYSIDVERSION    1
1153
1154 afs_int32
1155 ReadSysIdFile() {
1156     afs_int32 fd, nentries, i;
1157     struct versionStamp vsn;
1158     struct stat     status;
1159     afsUUID uuid;
1160
1161     if ((stat(AFSDIR_SERVER_SYSID_FILEPATH, &status)) || (status.st_size <= 0)) {
1162         ViceLog(0, ("%s: doesn't exist\n", AFSDIR_SERVER_SYSID_FILEPATH));
1163         return ENOENT;
1164     }
1165     if (!(fd = open(AFSDIR_SERVER_SYSID_FILEPATH, O_RDONLY, 0))) {
1166         ViceLog(0, ("%s: can't open (%d)\n", AFSDIR_SERVER_SYSID_FILEPATH, errno));
1167         return EIO;
1168     }
1169     if ((i = read(fd, (char *)&vsn, sizeof(vsn))) != sizeof(vsn)) {
1170         ViceLog(0, ("%s: Read failed (%d)\n", AFSDIR_SERVER_SYSID_FILEPATH, errno));
1171         return EIO;
1172     }
1173     if (vsn.magic != SYSIDMAGIC) {
1174         ViceLog(0, ("%s: wrong magic %x (we support %x)\n", AFSDIR_SERVER_SYSID_FILEPATH, vsn.magic, SYSIDMAGIC));
1175         return EIO;
1176     }
1177     if (vsn.version != SYSIDVERSION) {
1178         ViceLog(0, ("%s: wrong version %d (we support %d)\n", AFSDIR_SERVER_SYSID_FILEPATH, vsn.version, SYSIDVERSION));
1179         return EIO;
1180     }
1181     if ((i = read(fd, (char *)&uuid, sizeof(struct afsUUID))) != sizeof(struct afsUUID)) {
1182         ViceLog(0, ("%s: read of uuid failed (%d)\n", AFSDIR_SERVER_SYSID_FILEPATH, errno));
1183         return EIO;
1184     }
1185     afs_ntohuuid(&uuid);
1186     FS_HostUUID = uuid;
1187     if ((i = read(fd, (char *)&nentries, sizeof(afs_int32))) != sizeof(afs_int32)) {
1188         ViceLog(0, ("%s: Read of entries failed (%d)\n", AFSDIR_SERVER_SYSID_FILEPATH, errno));
1189         return EIO;
1190     }
1191     if (nentries <= 0 || nentries > ADDRSPERSITE) {
1192         ViceLog(0, ("%s: invalid num of interfaces: %d\n", AFSDIR_SERVER_SYSID_FILEPATH, nentries));
1193         return EIO;
1194     }
1195     FS_HostAddr_cnt = nentries;
1196     for (i = 0; i < nentries; i++) {
1197         if (read(fd, (char *)&FS_HostAddrs[i], sizeof(afs_int32)) != sizeof(afs_int32)) {       
1198             ViceLog(0, ("%s: Read of addresses failed (%d)\n", AFSDIR_SERVER_SYSID_FILEPATH, errno));
1199             FS_HostAddr_cnt = 0;        /* reset it */
1200             return EIO;
1201         }
1202     }
1203     close(fd);
1204     return 0;
1205 }
1206
1207 afs_int32
1208 WriteSysIdFile() {
1209     afs_int32 fd, nentries, i;
1210     struct versionStamp vsn;
1211     struct stat     status;
1212     afsUUID uuid;
1213     
1214     if (!stat(AFSDIR_SERVER_SYSID_FILEPATH, &status)) {
1215         /*
1216          * File exists; keep the old one around
1217          */
1218         renamefile(AFSDIR_SERVER_SYSID_FILEPATH, AFSDIR_SERVER_OLDSYSID_FILEPATH);      
1219     }
1220     fd = open(AFSDIR_SERVER_SYSID_FILEPATH, O_WRONLY|O_TRUNC|O_CREAT, 0666);
1221     if (fd < 1) {
1222         ViceLog(0, ("%s: can't create (%d)\n", AFSDIR_SERVER_SYSID_FILEPATH, errno));
1223         return EIO;
1224     }
1225     vsn.magic = SYSIDMAGIC;
1226     vsn.version = 1;
1227     if ((i = write(fd, (char *)&vsn, sizeof(vsn))) != sizeof(vsn)) {
1228         ViceLog(0, ("%s: write failed (%d)\n", AFSDIR_SERVER_SYSID_FILEPATH, errno));
1229         return EIO;
1230     }
1231     uuid = FS_HostUUID;
1232     afs_htonuuid(&uuid);
1233     if ((i = write(fd, (char *)&uuid, sizeof(struct afsUUID))) != sizeof(struct afsUUID)) {
1234         ViceLog(0, ("%s: write of uuid failed (%d)\n", AFSDIR_SERVER_SYSID_FILEPATH, errno));
1235         return EIO;
1236     }
1237     if ((i = write(fd, (char *)&FS_HostAddr_cnt, sizeof(afs_int32))) != sizeof(afs_int32)) {
1238         ViceLog(0, ("%s: write of # of entries failed (%d)\n", AFSDIR_SERVER_SYSID_FILEPATH, errno));
1239         return EIO;
1240     }
1241     for (i = 0; i < FS_HostAddr_cnt; i++) {
1242         if (write(fd, (char *)&FS_HostAddrs[i], sizeof(afs_int32)) != sizeof(afs_int32)) {      
1243             ViceLog(0, ("%s: write of addresses failed (%d)\n", AFSDIR_SERVER_SYSID_FILEPATH, errno));
1244             return EIO;
1245         }
1246     }
1247     close(fd);
1248     return 0;
1249 }
1250
1251 /*
1252  * defect 10966 
1253  * This routine sets up the buffers for the VL_RegisterAddrs RPC. All addresses
1254  * in FS_HostAddrs[] are in NBO, while the RPC treats them as a "blob" of data
1255  * and so we need to convert each of them into HBO which is what the extra 
1256  * array called FS_HostAddrs_HBO is used here.
1257  */ 
1258 afs_int32
1259 Do_VLRegisterRPC() {
1260     register int code;
1261     bulkaddrs addrs;
1262     extern int VL_RegisterAddrs();
1263     afs_uint32 FS_HostAddrs_HBO[ADDRSPERSITE];
1264     int i=0;
1265  
1266     for (i=0; i < FS_HostAddr_cnt ; i++) 
1267      FS_HostAddrs_HBO[i]=ntohl(FS_HostAddrs[i]);
1268     addrs.bulkaddrs_len = FS_HostAddr_cnt;
1269     addrs.bulkaddrs_val = (afs_uint32 *)FS_HostAddrs_HBO;
1270     code = ubik_Call(VL_RegisterAddrs, cstruct, 0, &FS_HostUUID, 0, &addrs);
1271     if (code) {
1272        if (code == VL_MULTIPADDR) {
1273           ViceLog(0, ("VL_RegisterAddrs rpc failed; The ethernet address exist on a different server; repair it\n"));
1274           ViceLog(0, ("VL_RegisterAddrs rpc failed; See VLLog for details\n"));
1275           return code;
1276        } else if (code == RXGEN_OPCODE) {
1277           ViceLog(0, ("vlserver doesn't support VL_RegisterAddrs rpc; ignored\n"));
1278           FS_registered = 2;    /* So we don't have to retry in the gc daemon */
1279        } else {
1280           ViceLog(0, ("VL_RegisterAddrs rpc failed; will retry periodically (code=%d, err=%d)\n",
1281                       code, errno));
1282        }
1283     } else {
1284        FS_registered = 2;       /* So we don't have to retry in the gc daemon */
1285        WriteSysIdFile();
1286     }
1287
1288     return 0;
1289 }
1290
1291 afs_int32 InitVL() {
1292     int (*old)();
1293     afs_int32 code;
1294     afs_int32 cnt, i;
1295     extern int rxi_numNetAddrs;
1296     extern afs_uint32 rxi_NetAddrs[];
1297
1298     /*
1299      * If this fails, it's because something major is wrong, and is not
1300      * likely to be time dependent.
1301      */
1302     code = vl_Initialize(AFSDIR_SERVER_ETC_DIRPATH);
1303     if (code != 0) {
1304         ViceLog(0, ("Couldn't initialize protection library; code=%d.\n", code));
1305         return code;
1306     }
1307
1308     /* Read or create the sysid file and register the fileserver's
1309      * IP addresses with the vlserver.
1310      */
1311     code = ReadSysIdFile();
1312     if (code) {
1313        /* Need to create the file */
1314        ViceLog(0, ("Creating new SysID file\n")); 
1315        if ((code = afs_uuid_create(&FS_HostUUID))) {
1316           ViceLog(0, ("Failed to create new uuid: %d\n", code)); 
1317           exit(1);
1318        }
1319     }
1320     /* A good sysid file exists; inform the vlserver. If any conflicts,
1321      * we always use the latest interface available as the real truth.
1322      */
1323 #ifndef AFS_NT40_ENV
1324     if(AFSDIR_SERVER_NETRESTRICT_FILEPATH || AFSDIR_SERVER_NETINFO_FILEPATH) {
1325       /*
1326        * Find addresses we are supposed to register as per the netrestrict 
1327        * and netinfo files (/usr/afs/local/NetInfo and 
1328        * /usr/afs/local/NetRestict)
1329        */
1330       char reason[1024];
1331       afs_int32 code = parseNetFiles(FS_HostAddrs,NULL, NULL,
1332                                  ADDRSPERSITE, reason,
1333                                  AFSDIR_SERVER_NETINFO_FILEPATH,
1334                                  AFSDIR_SERVER_NETRESTRICT_FILEPATH);
1335       if (code < 0) {
1336         ViceLog(0,("Can't register any valid addresses: %s\n",reason));
1337         exit(1);
1338       }
1339       FS_HostAddr_cnt = (afs_uint32) code;
1340     }
1341     else 
1342 #endif
1343     {
1344       FS_HostAddr_cnt = rx_getAllAddr(FS_HostAddrs, ADDRSPERSITE);
1345     }
1346
1347     FS_registered = 1;
1348     code = Do_VLRegisterRPC();
1349     return code;
1350
1351
1352 int
1353 main(int argc, char * argv[])
1354 {
1355     int     i;
1356     afs_int32    code;
1357     FILE   *file;
1358     char tbuffer[32];
1359     struct rx_securityClass *sc[4];
1360     struct rx_service *tservice;
1361 #ifdef AFS_PTHREAD_ENV
1362     pthread_t parentPid, serverPid;
1363     pthread_attr_t tattr;
1364 #else /* AFS_PTHREAD_ENV */
1365     PROCESS parentPid, serverPid;
1366 #endif /* AFS_PTHREAD_ENV */
1367     struct hostent *he;
1368     int minVnodesRequired;      /* min size of vnode cache */
1369 #ifndef AFS_NT40_ENV
1370     struct rlimit rlim;         /* max number of open file descriptors */
1371 #endif
1372     int curLimit;
1373
1374 #ifdef  AFS_AIX32_ENV
1375     struct sigaction nsa;
1376     
1377     sigemptyset(&nsa.sa_mask);
1378     nsa.sa_handler = SIG_DFL;
1379     nsa.sa_flags = SA_FULLDUMP;
1380     sigaction(SIGABRT, &nsa, NULL);
1381     sigaction(SIGSEGV, &nsa, NULL);
1382 #endif
1383
1384     /* Initialize dirpaths */
1385     if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
1386 #ifdef AFS_NT40_ENV
1387         ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0],0);
1388 #endif
1389         fprintf(stderr,"%s: Unable to obtain AFS server directory.\n", argv[0]);
1390         exit(2);
1391     }
1392
1393 #ifndef AFS_QUIETFS_ENV
1394     console = fopen("/dev/console","w");
1395 #endif
1396
1397     if(ParseArgs(argc,argv)) {
1398         FlagMsg();
1399         exit(-1);
1400     }
1401
1402 #ifdef AFS_PTHREAD_ENV
1403     assert(pthread_mutex_init(&fileproc_glock_mutex, NULL) == 0);
1404 #endif /* AFS_PTHREAD_ENV */
1405
1406 #ifdef AFS_SGI_VNODE_GLUE
1407     if (afs_init_kernel_config(-1) <0) {
1408         printf("Can't determine NUMA configuration, not starting fileserver.\n");
1409         exit(1);
1410     }
1411 #endif
1412     confDir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
1413     if (!confDir) {
1414         fprintf(stderr, "Unable to open config directory %s\n",
1415                 AFSDIR_SERVER_ETC_DIRPATH);
1416         exit(-1);
1417     }
1418
1419     NewParms(1);
1420
1421     /* Open FileLog on stdout, stderr, fd 1 and fd2 (for perror), sigh. */
1422     OpenLog(AFSDIR_SERVER_FILELOG_FILEPATH);
1423     SetupLogSignals(); 
1424
1425     if (SawSpare && SawPctSpare) {
1426         ViceLog(0, ("Both -spare and -pctspare specified, exiting.\n"));
1427         exit(-1);
1428     }
1429
1430 #ifdef AFS_SGI_XFS_IOPS_ENV
1431     ViceLog(0, ("XFS/EFS File server starting\n"));
1432 #else
1433     ViceLog(0, ("File server starting\n"));
1434 #endif
1435
1436 #if defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV)
1437     /* initialize the pthread soft signal handler thread */
1438     softsig_init();
1439 #endif
1440
1441     /* install signal handlers for controlling the fileserver process */
1442     ResetCheckSignal();  /* set CheckSignal_Signal() sig handler */
1443     ResetCheckDescriptors();  /* set CheckDescriptors_Signal() sig handler */
1444
1445 #if defined(AFS_SGI_ENV)
1446     /* give this guy a non-degrading priority so help busy servers */
1447     schedctl(NDPRI, 0, NDPNORMMAX);
1448     if (SawLock)
1449         plock(PROCLOCK);
1450 #else
1451 #ifndef AFS_NT40_ENV
1452     nice(-5); /* TODO: */
1453 #endif
1454 #endif
1455     assert(DInit(buffs) == 0);
1456  
1457 #ifdef AFS_NT40_ENV
1458     if (afs_winsockInit()<0) {
1459         ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
1460         ViceLog(0, ("File server failed to intialize winsock.\n"));
1461         exit(1);
1462     }
1463 #endif
1464     CheckAdminName();
1465
1466     /* if we support more than 16 threads, then we better have the ability 
1467     ** to keep open a large number of files simultaneously 
1468     */
1469 #if     defined(AFS_AIX_ENV) && !defined(AFS_AIX42_ENV)
1470     curLimit = OPEN_MAX;        /* for pre AIX 4.2 systems */
1471 #elif defined(AFS_NT40_ENV)
1472     curLimit = NT_OPEN_MAX;     /* open file descriptor limit on NT */
1473 #else
1474
1475     curLimit = 0;               /* the number of open file descriptors */
1476     code     = getrlimit(RLIMIT_NOFILE, &rlim);
1477     if (code == 0) {
1478         curLimit      = rlim.rlim_cur;
1479         rlim.rlim_cur = rlim.rlim_max;
1480         code = setrlimit(RLIMIT_NOFILE, &rlim);
1481         if ( code == 0 ) 
1482             curLimit  = rlim.rlim_max;
1483     }
1484     if ( code != 0 )
1485         ViceLog(0, ("Failed to increase open file limit, using default\n"));
1486
1487 #endif /* defined(AFS_AIX_ENV) && !defined(AFS_AIX42_ENV) */
1488
1489     curLimit -= 32;             /* leave a slack of 32 file descriptors */
1490     if ( lwps > curLimit ) {
1491         if ( curLimit > 0)
1492             lwps = curLimit; 
1493         else if ( lwps > 16 )
1494             lwps = 16;            /* default to a maximum of 16 threads */
1495         ViceLog(0, ("The system supports a max of %d open files and we are starting %d threads\n", curLimit, lwps));
1496     }
1497
1498
1499 #ifndef AFS_PTHREAD_ENV
1500     assert(LWP_InitializeProcessSupport(LWP_MAX_PRIORITY - 2, &parentPid) == LWP_SUCCESS);
1501 #endif /* !AFS_PTHREAD_ENV */
1502
1503     /* Initialize volume support */
1504     if (!novbc) {
1505         V_BreakVolumeCallbacks = BreakVolumeCallBacksLater;
1506     }
1507
1508     /* initialize libacl routines */
1509     acl_Initialize(ACL_VERSION);
1510
1511     /* initialize RX support */
1512 #ifndef AFS_NT40_ENV
1513     rxi_syscallp = viced_syscall;
1514 #endif
1515     rx_extraPackets = rxpackets;
1516     rx_extraQuota = 4;  /* for outgoing prserver calls from R threads */
1517     rx_SetBusyThreshold(busy_threshold, VBUSY);
1518     rx_SetCallAbortThreshold(abort_threshold);
1519     rx_SetConnAbortThreshold(abort_threshold);
1520     stackSize = lwps * 4000;
1521     if (stackSize < 32000)
1522         stackSize = 32000;
1523     else if (stackSize > 44000)
1524         stackSize = 44000;
1525 #if    defined(AFS_HPUX_ENV) || defined(AFS_SUN_ENV) || defined(AFS_SGI51_ENV)
1526     rx_SetStackSize(1, stackSize);      
1527 #endif
1528     if ( udpBufSize )
1529         rx_SetUdpBufSize(udpBufSize);/* set the UDP buffer size for receive */
1530     if (rx_Init((int)htons(7000))<0) {
1531         ViceLog(0, ("Cannot initialize RX\n"));
1532         exit(1);
1533     }
1534     if (!rxJumbograms) {
1535         /* Don't send and don't allow 3.4 clients to send jumbograms. */
1536         rx_SetNoJumbo();
1537     }
1538     rx_GetIFInfo();
1539     rx_SetRxDeadTime(30);
1540     sc[0] = rxnull_NewServerSecurityObject();
1541     sc[1] = 0; /* rxvab_NewServerSecurityObject(key1, 0) */
1542     sc[2] = rxkad_NewServerSecurityObject (rxkad_clear, NULL,
1543                                            get_key, NULL);
1544     sc[3] = rxkad_NewServerSecurityObject (rxkad_crypt, NULL,
1545                                            get_key, NULL);
1546     tservice = rx_NewService
1547         (/* port */ 0, /* service id */ 1, /*service name */ "AFS",
1548          /* security classes */ sc, /* numb sec classes */ 4,
1549          RXAFS_ExecuteRequest);
1550     if (!tservice) {
1551         ViceLog(0, ("Failed to initialize RX, probably two servers running.\n"));
1552         exit(-1);
1553     }
1554     rx_SetDestroyConnProc(tservice, (void (*)())h_FreeConnection);
1555     rx_SetMinProcs(tservice, 3);
1556     rx_SetMaxProcs(tservice, lwps);
1557     rx_SetCheckReach(tservice, 1);
1558
1559     tservice = rx_NewService(0,  RX_STATS_SERVICE_ID, "rpcstats", sc, 4, RXSTATS_ExecuteRequest);
1560     if (!tservice) {
1561         ViceLog(0, ("Failed to initialize rpc stat service.\n"));
1562         exit(-1);
1563     }
1564     rx_SetMinProcs(tservice, 2);
1565     rx_SetMaxProcs(tservice, 4);
1566
1567     /*
1568      * Enable RX hot threads, which allows the listener thread to trade
1569      * places with an idle thread and moves the context switch from listener
1570      * to worker out of the critical path.
1571      */
1572     rx_EnableHotThread();
1573
1574     /* Some rx debugging */
1575     if (rxlog || eventlog) {
1576         debugFile = fopen("rx_dbg", "w");
1577         if (rxlog) rx_debugFile = debugFile;
1578         if (eventlog) rxevent_debugFile = debugFile;
1579     }
1580
1581     init_sys_error_to_et(); /* Set up error table translation */
1582     h_InitHostPackage();  /* set up local cellname and realmname */
1583     InitCallBack(numberofcbs);
1584     ClearXStatValues();
1585
1586     code = InitVL();
1587     if (code) {
1588         ViceLog(0,("Fatal error in library initialization, exiting!!\n"));
1589         exit(1);
1590     }
1591
1592     code = InitPR();
1593     if (code) {
1594         ViceLog(0,("Fatal error in protection initialization, exiting!!\n"));
1595         exit(1);
1596     }
1597
1598     /* allow super users to manage RX statistics */
1599     rx_SetRxStatUserOk(fs_rxstat_userok);
1600
1601     rx_StartServer(0);  /* now start handling requests */
1602
1603     /* we ensure that there is enough space in the vnode buffer to satisfy
1604     ** requests from all concurrent threads. 
1605     ** the maximum number of vnodes used by a single thread at any one time
1606     ** is three ( "link" uses three vnodes simultaneously, one vLarge and
1607     ** two vSmall for linking files and two vLarge and one vSmall for linking
1608     ** files  ) : dhruba 
1609     */
1610     minVnodesRequired = 2 * lwps + 1;     
1611     if ( minVnodesRequired > nSmallVns ) {
1612         nSmallVns = minVnodesRequired;
1613         ViceLog(0, ("Overriding -s command line parameter with %d\n",
1614                     nSmallVns));
1615     }
1616     if ( minVnodesRequired > large ) {
1617         large = minVnodesRequired;
1618         ViceLog(0, ("Overriding -l command line parameter with %d\n", large));
1619     }
1620
1621     /* We now do this after getting the listener up and running, so that client
1622        connections don't timeout (maybe) if a file server is restarted, since it
1623        will be available "real soon now".  Worry about whether we can satisfy the 
1624        calls in the volume package itself.
1625      */
1626     if (VInitVolumePackage(fileServer,large,nSmallVns,0, volcache)) {
1627         ViceLog(0, ("Shutting down: errors encountered initializing volume package\n"));
1628         VShutdown();
1629         exit(1);
1630     }
1631
1632     /*
1633      * We are done calling fopen/fdopen. It is safe to use a large
1634      * of the file descriptor cache.
1635      */
1636     ih_UseLargeCache();
1637
1638 #ifdef AFS_PTHREAD_ENV
1639     ViceLog(5, ("Starting pthreads\n"));
1640     assert(pthread_attr_init(&tattr) == 0);
1641     assert(pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED) == 0);
1642
1643     assert(pthread_create(&serverPid, &tattr, (void *)FiveMinuteCheckLWP, &fiveminutes) == 0);
1644     assert(pthread_create(&serverPid, &tattr, (void *)HostCheckLWP, &fiveminutes) == 0);
1645     assert(pthread_create(&serverPid, &tattr, (void *)FsyncCheckLWP, &fiveminutes) == 0);
1646 #else /* AFS_PTHREAD_ENV */
1647     ViceLog(5, ("Starting LWP\n"));
1648     assert(LWP_CreateProcess(FiveMinuteCheckLWP, stack*1024, LWP_MAX_PRIORITY - 2,
1649                              (void *) &fiveminutes, "FiveMinuteChecks", &serverPid) == LWP_SUCCESS);
1650             
1651     assert(LWP_CreateProcess(HostCheckLWP, stack*1024, LWP_MAX_PRIORITY - 2,
1652             (void *) &fiveminutes, "HostCheck", &serverPid) == LWP_SUCCESS);
1653     assert(LWP_CreateProcess(FsyncCheckLWP, stack*1024, LWP_MAX_PRIORITY - 2,
1654             (void *) &fiveminutes, "FsyncCheck", &serverPid) == LWP_SUCCESS);
1655 #endif /* AFS_PTHREAD_ENV */
1656
1657     TM_GetTimeOfDay(&tp, 0);
1658
1659 #ifndef AFS_QUIETFS_ENV
1660     if (console != NULL) {
1661         fprintf(console, "File server has started at %s\r",
1662                 afs_ctime(&tp.tv_sec, tbuffer, sizeof(tbuffer)));
1663     }
1664 #endif
1665
1666     /*
1667      * Figure out the FileServer's name and primary address.
1668      */
1669     ViceLog(0, ("Getting FileServer name...\n"));
1670     code = gethostname(FS_HostName, 64);
1671     if (code) {
1672         ViceLog(0, ("gethostname() failed\n"));
1673     }
1674     ViceLog(0, ("FileServer host name is '%s'\n", FS_HostName));
1675
1676     ViceLog(0, ("Getting FileServer address...\n"));
1677     he = gethostbyname(FS_HostName);
1678     if (!he) {
1679         ViceLog(0, ("Can't find address for FileServer '%s'\n", FS_HostName));
1680     }
1681     else {
1682         char hoststr[16];
1683         memcpy(&FS_HostAddr_NBO, he->h_addr, 4);
1684         afs_inet_ntoa_r(FS_HostAddr_NBO, hoststr);
1685         FS_HostAddr_HBO = ntohl(FS_HostAddr_NBO);
1686         ViceLog(0,("FileServer %s has address %s (0x%x or 0x%x in host byte order)\n",
1687                    FS_HostName, hoststr, FS_HostAddr_NBO, FS_HostAddr_HBO));
1688     }
1689
1690     /* Install handler to catch the shutdown signal;
1691      * bosserver assumes SIGQUIT shutdown
1692      */
1693 #if defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV)
1694     softsig_signal(SIGQUIT, ShutDown_Signal);
1695 #else
1696     signal(SIGQUIT, ShutDown_Signal);
1697 #endif
1698
1699     ViceLog(0,("File Server started %s",
1700                afs_ctime(&tp.tv_sec, tbuffer, sizeof(tbuffer))));
1701 #if FS_STATS_DETAILED
1702     afs_FullPerfStats.det.epoch.tv_sec = StartTime = tp.tv_sec;
1703 #endif
1704 #ifdef AFS_PTHREAD_ENV
1705     while(1) {
1706         sleep(1000); /* long time */
1707     }
1708 #else /* AFS_PTHREAD_ENV */
1709     assert(LWP_WaitProcess(&parentPid) == LWP_SUCCESS);
1710 #endif /* AFS_PTHREAD_ENV */
1711 }
1712