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