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