vos-listaddrs-avoid-holes-in-index-20020805
[openafs.git] / src / volser / volmain.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
13 RCSID("$Header$");
14
15 #include <sys/types.h>
16 #ifdef AFS_NT40_ENV
17 #include <time.h>
18 #include <fcntl.h>
19 #include <windows.h>
20 #include <WINNT/afsevent.h>
21 #else
22 #include <sys/time.h>
23 #include <sys/file.h>
24 #include <netinet/in.h>
25 #endif
26 #include <rx/xdr.h>
27 #include <afs/afsint.h>
28 #include <stdio.h>
29 #include <signal.h>
30 #include <afs/assert.h>
31 #include <afs/prs_fs.h>
32 #include <afs/nfs.h>
33 #include <lwp.h>
34 #include <lock.h>
35 #include <afs/afssyscalls.h>
36 #include <afs/ihandle.h>
37 #ifdef AFS_NT40_ENV
38 #include <afs/ntops.h>
39 #endif
40 #include <afs/vnode.h>
41 #include <afs/volume.h>
42 #include <afs/partition.h>
43 #include <rx/rx.h>
44 #include <rx/rx_globals.h>
45 #include <afs/auth.h>
46 #include <rx/rxkad.h>
47 #include <afs/cellconfig.h>
48 #include <afs/keys.h>
49 #include <ubik.h>
50
51 #include "volser.h"
52 #include <errno.h>
53 #include <afs/audit.h>
54 #include <afs/afsutil.h>
55
56 #define VolserVersion "2.0"
57 #define N_SECURITY_OBJECTS 3
58
59 extern int (*vol_PollProc)();
60 extern struct volser_trans *TransList();
61 extern int IOMGR_Poll();
62 char *GlobalNameHack = (char *)0;
63 int hackIsIn = 0;
64 afs_int32 GlobalVolCloneId, GlobalVolParentId;
65 int GlobalVolType;
66 int VolumeChanged;  /* XXXX */
67 static char busyFlags[MAXHELPERS];
68 struct volser_trans *QI_GlobalWriteTrans = 0;
69 extern int QI_write();
70 extern int QI_flush();
71 extern int (*VolWriteProc)();
72 extern int (*VolFlushProc)();
73 extern void AFSVolExecuteRequest();
74 extern void RXSTATS_ExecuteRequest();
75 extern struct rx_securityClass *rxnull_NewServerSecurityObject();
76 extern struct rx_service *rx_NewService();
77 extern Log();
78 struct afsconf_dir *tdir;
79 static afs_int32 runningCalls=0;
80 int DoLogging = 0;
81 #define MAXLWP 16
82 int lwps = 9;
83 int     udpBufSize = 0;   /* UDP buffer size for receive*/
84
85 int Testing = 0; /* for ListViceInodes */
86
87 #define VS_EXIT(code)  {                                          \
88                           osi_audit(VS_ExitEvent, code, AUD_END); \
89                           exit(code);                             \
90                        }
91
92
93 static MyBeforeProc () {
94     runningCalls++;
95     return 0;
96 }
97
98 static MyAfterProc () {
99     runningCalls--;
100     return 0;
101 }
102
103 /* Called every GCWAKEUP seconds to try to unlock all our partitions,
104  * if we're idle and there are no active transactions 
105  */
106 static TryUnlock() {
107     /* if there are no running calls, and there are no active transactions, then
108        it should be safe to release any partition locks we've accumulated */
109     if (runningCalls == 0 && TransList() == (struct volser_trans *) 0) {
110         VPFullUnlock();         /* in volprocs.c */
111     }
112 }
113
114 /* background daemon for timing out transactions */
115 static BKGLoop() {
116     struct timeval tv;
117     int loop=0;
118
119     while (1) {
120         tv.tv_sec = GCWAKEUP;
121         tv.tv_usec = 0;
122         (void) IOMGR_Select(0, 0, 0, 0, &tv);
123         GCTrans();
124         TryUnlock();
125         loop++;
126         if ( loop == 10 )       
127         {                       /* reopen log every 5 minutes */
128                 loop = 0; 
129                 ReOpenLog(AFSDIR_SERVER_VOLSERLOG_FILEPATH);
130         }
131     }
132 }
133
134 /* Background daemon for sleeping so the volserver does not become I/O bound */
135 afs_int32 TTsleep, TTrun;
136 static BKGSleep()
137 {
138   struct volser_trans *tt;
139
140   if (TTsleep) {
141      while (1) {
142         IOMGR_Sleep(TTrun);
143         for (tt=TransList(); tt; tt=tt->next) {
144            if ( (strcmp(tt->lastProcName,"DeleteVolume") == 0) ||
145                 (strcmp(tt->lastProcName,"Clone")        == 0) ||
146                 (strcmp(tt->lastProcName,"ReClone")      == 0) ||
147                 (strcmp(tt->lastProcName,"Forward")      == 0) ||
148                 (strcmp(tt->lastProcName,"Restore")      == 0) ||
149                 (strcmp(tt->lastProcName,"ForwardMulti") == 0) )
150               break;
151         }
152         if (tt) {
153            sleep(TTsleep);
154         }
155      }
156   }
157 }
158
159 #ifndef AFS_NT40_ENV
160 int volser_syscall(a3, a4, a5)
161 afs_uint32 a3, a4;
162 void * a5;
163 {
164   afs_uint32 rcode;
165   void (*old)();
166         
167 #ifndef AFS_LINUX20_ENV
168   old = signal(SIGSYS, SIG_IGN);        
169 #endif
170   rcode = syscall (AFS_SYSCALL /* AFS_SYSCALL */, 28 /* AFSCALL_CALL */, a3, a4, a5);
171 #ifndef AFS_LINUX20_ENV
172   signal(SIGSYS, old);  
173 #endif
174
175   return rcode;
176 }
177 #endif
178
179
180 /* check whether caller is authorized to manage RX statistics */
181 int vol_rxstat_userok(call)
182     struct rx_call *call;
183 {
184     return afsconf_SuperUser(tdir, call, (char *)0);
185 }
186
187 #include "AFS_component_version_number.c"
188 main(argc, argv)
189 int argc;
190 char **argv; {
191     char *pid;
192     register afs_int32 code;
193     struct rx_securityClass *(securityObjects[3]);
194     struct rx_service *service;
195     struct ktc_encryptionKey tkey;
196     int rxpackets = 100;
197     char commandLine[150];
198     int i;
199     int rxJumbograms = 1; /* default is to send and receive jumbograms. */
200     int bufSize = 0;      /* temp variable to read in udp socket buf size*/
201
202 #ifdef  AFS_AIX32_ENV
203     /*
204      * The following signal action for AIX is necessary so that in case of a 
205      * crash (i.e. core is generated) we can include the user's data section 
206      * in the core dump. Unfortunately, by default, only a partial core is
207      * generated which, in many cases, isn't too useful.
208      */
209     struct sigaction nsa;
210     
211     sigemptyset(&nsa.sa_mask);
212     nsa.sa_handler = SIG_DFL;
213     nsa.sa_flags = SA_FULLDUMP;
214     sigaction(SIGABRT, &nsa, NULL);
215     sigaction(SIGSEGV, &nsa, NULL);
216 #endif
217     osi_audit(VS_StartEvent, 0, AUD_END);
218
219     /* Initialize dirpaths */
220     if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
221 #ifdef AFS_NT40_ENV
222         ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0],0);
223 #endif
224         fprintf(stderr,"%s: Unable to obtain AFS server directory.\n", argv[0]);
225         exit(2);
226     }
227     
228     for (commandLine[0] = '\0', i=0; i<argc; i++) {
229         if (i > 0) strcat(commandLine, " ");
230         strcat(commandLine, argv[i]);
231     }
232
233     TTsleep = TTrun = 0;
234
235     /* parse cmd line */
236     for(code=1;code<argc;code++) {
237         if (strcmp(argv[code], "-log")==0) {
238             /* set extra logging flag */
239             DoLogging = 1;
240         }
241         else if (strcmp(argv[code], "-help")==0) {
242             goto usage;
243         }
244         else if (strcmp(argv[code], "-p") == 0) {
245             lwps = atoi(argv[++code]);
246             if (lwps > MAXLWP) {
247                 printf("Warning: '-p %d' is too big; using %d instead\n",
248                        lwps, MAXLWP);
249                 lwps = MAXLWP;
250             }
251         }
252         else if (strcmp(argv[code], "-nojumbo")==0) {
253             rxJumbograms = 0;
254         }
255         else if (strcmp(argv[code], "-sleep")==0) {
256             sscanf(argv[++code], "%d/%d", &TTsleep, &TTrun);
257             if ((TTsleep < 0) || (TTrun <= 0)) {
258                 printf("Warning: '-sleep %d/%d' is incorrect; ignoring\n",
259                        TTsleep, TTrun);
260                 TTsleep = TTrun = 0;
261             }
262         }
263         else if ( strcmp(argv[code], "-udpsize")==0) {
264             if ( (code+1) >= argc ) {
265                 printf("You have to specify -udpsize <integer value>\n");
266                 exit(1);
267             }
268             sscanf(argv[++code], "%d", &bufSize);
269             if ( bufSize < rx_GetMinUdpBufSize() ) 
270                  printf("Warning:udpsize %d is less than minimum %d; ignoring\n",
271                         bufSize, rx_GetMinUdpBufSize() );
272             else
273                 udpBufSize = bufSize;
274         }       
275         else if (strcmp(argv[code], "-enable_peer_stats")==0) {
276             rx_enablePeerRPCStats();
277         }
278         else if (strcmp(argv[code], "-enable_process_stats")==0) {
279             rx_enableProcessRPCStats();
280         }
281 #ifndef AFS_NT40_ENV
282         else if (strcmp(argv[code], "-syslog")==0) {
283             /* set syslog logging flag */
284             serverLogSyslog = 1;
285         } 
286         else if (strncmp(argv[code], "-syslog=", 8)==0) {
287             serverLogSyslog = 1;
288             serverLogSyslogFacility = atoi(argv[code]+8);
289         }
290 #endif
291         else {
292             printf("volserver: unrecognized flag '%s'\n", argv[code]);
293 usage:
294 #ifndef AFS_NT40_ENV
295             printf("Usage: volserver [-log] [-p <number of processes>] "
296                    "[-udpsize <size of socket buffer in bytes>] "
297                    "[-syslog[=FACILITY]] "
298                    "[-enable_peer_stats] [-enable_process_stats] "
299                    "[-help]\n");
300 #else
301             printf("Usage: volserver [-log] [-p <number of processes>] "
302                    "[-udpsize <size of socket buffer in bytes>] "
303                    "[-enable_peer_stats] [-enable_process_stats] "
304                    "[-help]\n");
305 #endif
306             VS_EXIT(1);
307         }
308     }
309 #ifdef AFS_SGI_VNODE_GLUE
310     if (afs_init_kernel_config(-1) <0) {
311         printf("Can't determine NUMA configuration, not starting volserver.\n");
312         exit(1);
313     }
314 #endif
315     InitErrTabs();
316
317 #ifdef AFS_NT40_ENV
318     if (afs_winsockInit()<0) {
319         ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
320         printf("Volume server unable to start winsock, exiting.\n");
321         exit(1);
322     }
323 #endif
324     VInitVolumePackage(volumeUtility, 0, 0, CONNECT_FS, 0);
325     DInit(40);
326     vol_PollProc = IOMGR_Poll;  /* tell vol pkg to poll io system periodically */
327 #ifndef AFS_NT40_ENV
328     rxi_syscallp = volser_syscall;
329 #endif
330     rx_nPackets = rxpackets; /* set the max number of packets */
331     if ( udpBufSize)
332         rx_SetUdpBufSize(udpBufSize);/* set the UDP buffer size for receive */
333     code = rx_Init((int)htons(AFSCONF_VOLUMEPORT));
334     if (code) {
335         fprintf(stderr,"rx init failed on socket AFSCONF_VOLUMEPORT %u\n",AFSCONF_VOLUMEPORT);
336         VS_EXIT(1);
337     }
338     if (!rxJumbograms) {
339         /* Don't allow 3.4 vos clients to send jumbograms and we don't send. */
340         rx_SetNoJumbo();
341     }
342     rx_GetIFInfo();
343     rx_SetRxDeadTime(420);
344     memset(busyFlags, 0, sizeof(busyFlags));
345
346     /* Open FileLog and map stdout, stderr into it */
347     OpenLog(AFSDIR_SERVER_VOLSERLOG_FILEPATH);
348     SetupLogSignals();
349
350     /* create the lwp to garbage-collect old transactions and sleep periodically */
351     LWP_CreateProcess(BKGLoop, 16*1024, 3, 0, "vol bkg daemon", &pid);
352     LWP_CreateProcess(BKGSleep,16*1024, 3, 0, "vol slp daemon", &pid);
353
354     /* Create a single security object, in this case the null security object, for unauthenticated connections, which will be used to control security on connections made to this server */
355
356     tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
357     if (!tdir) {
358         Abort("volser: could not open conf files in %s\n", AFSDIR_SERVER_ETC_DIRPATH);
359         VS_EXIT(1);
360     }
361     afsconf_GetKey(tdir, 999, &tkey);
362     securityObjects[0] = (struct rx_securityClass *) rxnull_NewServerSecurityObject();
363     securityObjects[1] = (struct rx_securityClass *) 0; /* don't bother with rxvab */
364     securityObjects[2] = (struct rx_securityClass *) rxkad_NewServerSecurityObject(0, tdir, afsconf_GetKey, (char *) 0);
365     if (securityObjects[0] == (struct rx_securityClass *) 0) Abort("rxnull_NewServerSecurityObject");
366     service = rx_NewService(0, VOLSERVICE_ID, "VOLSER", securityObjects, 3, AFSVolExecuteRequest);
367     if (service == (struct rx_service *) 0) Abort("rx_NewService");
368     rx_SetBeforeProc(service, (char (*)()) MyBeforeProc);
369     rx_SetAfterProc(service, (char (*)()) MyAfterProc);
370     rx_SetIdleDeadTime(service, 0);     /* never timeout */
371     if (lwps < 4)
372         lwps = 4;
373     rx_SetMaxProcs(service, lwps);
374     rx_SetStackSize(service, 32768);
375
376     service = rx_NewService(0, RX_STATS_SERVICE_ID, "rpcstats", securityObjects, 3, RXSTATS_ExecuteRequest);
377     if (service == (struct rx_service *) 0) Abort("rx_NewService");
378     rx_SetMinProcs(service, 2);
379     rx_SetMaxProcs(service, 4);
380
381     Log("Starting AFS Volserver %s (%s)\n", VolserVersion, commandLine);
382     if (TTsleep) {
383        Log("Will sleep %d second%s every %d second%s\n",
384            TTsleep, (TTsleep>1)?"s":"", 
385            TTrun+TTsleep, (TTrun+TTsleep>1)?"s":"");
386     }
387
388     /* allow super users to manage RX statistics */
389     rx_SetRxStatUserOk(vol_rxstat_userok);
390
391     rx_StartServer(1); /* Donate this process to the server process pool */
392
393     osi_audit(VS_FinishEvent, (-1), AUD_END);
394     Abort("StartServer returned?");
395 }
396