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