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