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