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