2 * Copyright 2000, International Business Machines Corporation and others.
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
10 #include <afsconfig.h>
11 #include "afs/param.h"
14 #include "afs/sysincludes.h" /* Standard vendor system headers */
15 #include "afsincludes.h" /* Afs-based standard headers */
16 #include "afs/afs_stats.h"
17 #include "rx/rx_globals.h"
18 #include "rx/rxstat.h"
19 #if !defined(UKERNEL) && !defined(AFS_LINUX20_ENV)
22 #include "h/hashing.h"
24 #if !defined(AFS_HPUX110_ENV) && !defined(AFS_DARWIN_ENV)
25 #include "netinet/in_var.h"
27 #endif /* !defined(UKERNEL) */
28 #ifdef AFS_LINUX22_ENV
29 #include "h/smp_lock.h"
35 afs_int32 afs_termState = 0;
36 afs_int32 afs_gcpags = AFS_GCPAGS;
37 int afs_shuttingdown = 0;
38 int afs_cold_shutdown = 0;
39 int afs_resourceinit_flag = 0;
40 afs_int32 afs_nfs_server_addr;
41 struct interfaceAddr afs_cb_interface;
42 struct afs_osi_WaitHandle AFS_WaitHandler;
43 static struct rx_securityClass *srv_secobj;
44 static struct rx_securityClass *clt_secobj;
45 static struct rx_service *stats_svc;
46 static struct rx_service *pagcb_svc;
47 static struct rx_connection *rmtsys_conn;
48 char *afs_sysname = 0;
49 char *afs_sysnamelist[MAXNUMSYSNAMES];
50 int afs_sysnamecount = 0;
51 int afs_sysnamegen = 0;
52 afs_int32 afs_showflags = GAGUSER | GAGCONSOLE; /* show all messages */
58 afs_int32 now, last10MinCheck, last60MinCheck;
66 if (last10MinCheck + 600 < now) {
70 if (last60MinCheck + 3600 < now) {
75 now = 20000 - (osi_Time() - now);
76 afs_osi_Wait(now, &AFS_WaitHandler, 0);
78 if (afs_termState == AFSOP_STOP_AFS) {
79 #if defined(AFS_SUN5_ENV) || defined(RXK_LISTENER_ENV)
80 afs_termState = AFSOP_STOP_RXEVENT;
82 afs_termState = AFSOP_STOP_COMPLETE;
84 afs_osi_Wakeup(&afs_termState);
92 afspag_Init(afs_int32 nfs_server_addr)
94 struct clientcred ccred;
95 struct rmtbulk idata, odata;
96 afs_int32 code, err, addr, obuf;
99 afs_uuid_create(&afs_cb_interface.uuid);
104 rx_SetBusyChannelError(RX_CALL_TIMEOUT);
105 rx_Init(htons(7001));
107 AFS_STATCNT(afs_ResourceInit);
108 AFS_RWLOCK_INIT(&afs_xuser, "afs_xuser");
109 AFS_RWLOCK_INIT(&afs_xpagcell, "afs_xpagcell");
110 AFS_RWLOCK_INIT(&afs_xpagsys, "afs_xpagsys");
111 AFS_RWLOCK_INIT(&afs_icl_lock, "afs_icl_lock");
113 afs_resourceinit_flag = 1;
114 afs_nfs_server_addr = nfs_server_addr;
115 for (i = 0; i < MAXNUMSYSNAMES; i++) {
116 afs_sysnamelist[i] = afs_osi_Alloc(MAXSYSNAME);
117 osi_Assert(afs_sysnamelist[i] != NULL);
119 afs_sysname = afs_sysnamelist[0];
120 strcpy(afs_sysname, SYS_NAME);
121 afs_sysnamecount = 1;
124 srv_secobj = rxnull_NewServerSecurityObject();
125 stats_svc = rx_NewService(0, RX_STATS_SERVICE_ID, "rpcstats", &srv_secobj,
126 1, RXSTATS_ExecuteRequest);
127 pagcb_svc = rx_NewService(0, PAGCB_SERVICEID, "pagcb", &srv_secobj,
128 1, PAGCB_ExecuteRequest);
131 clt_secobj = rxnull_NewClientSecurityObject();
132 rmtsys_conn = rx_NewConnection(nfs_server_addr, htons(7009),
133 RMTSYS_SERVICEID, clt_secobj, 0);
135 #ifdef RXK_LISTENER_ENV
136 afs_start_thread(rxk_Listener, "Rx Listener");
138 afs_start_thread((void *)(void *)rx_ServerProc, "Rx Server Thread");
139 afs_start_thread(afs_rxevent_daemon, "Rx Event Daemon");
140 afs_start_thread(afs_Daemon, "AFS PAG Daemon");
146 /* If it's reachable, tell the translator to nuke our creds.
147 * We should be more agressive about making sure this gets done,
148 * even if the translator is unreachable when we boot.
150 addr = obuf = err = 0;
151 idata.rmtbulk_len = sizeof(addr);
152 idata.rmtbulk_val = (char *)&addr;
153 odata.rmtbulk_len = sizeof(obuf);
154 odata.rmtbulk_val = (char *)&obuf;
155 memset(&ccred, 0, sizeof(ccred));
156 code = RMTSYS_Pioctl(rmtsys_conn, &ccred, NIL_PATHP, 0x4F01, 0,
157 &idata, &odata, &err);
158 } /*afs_ResourceInit */
161 /* called with the GLOCK held */
163 afspag_Shutdown(void)
165 if (afs_shuttingdown)
167 afs_shuttingdown = 1;
168 afs_termState = AFSOP_STOP_RXCALLBACK;
169 rx_WakeupServerProcs();
170 while (afs_termState == AFSOP_STOP_RXCALLBACK)
171 afs_osi_Sleep(&afs_termState);
172 /* rx_ServerProc sets AFS_STOP_AFS */
174 while (afs_termState == AFSOP_STOP_AFS) {
175 afs_osi_CancelWait(&AFS_WaitHandler);
176 afs_osi_Sleep(&afs_termState);
178 /* afs_Daemon sets AFS_STOP_RXEVENT */
180 #if defined(AFS_SUN5_ENV) || defined(RXK_LISTENER_ENV)
181 while (afs_termState == AFSOP_STOP_RXEVENT)
182 afs_osi_Sleep(&afs_termState);
183 /* afs_rxevent_daemon sets AFSOP_STOP_RXK_LISTENER */
185 #if defined(RXK_LISTENER_ENV)
186 afs_osi_UnmaskRxkSignals();
188 while (afs_termState == AFSOP_STOP_RXK_LISTENER)
189 afs_osi_Sleep(&afs_termState);
190 /* rxk_Listener sets AFSOP_STOP_COMPLETE */
196 token_conversion(char *buffer, int buf_size, int in)
198 struct ClearToken *ticket;
202 if (buf_size < 4) return;
203 lptr = (afs_int32 *)buffer;
204 buffer += 4; buf_size -= 4;
206 *lptr = ntohl(*lptr);
210 *lptr = htonl(*lptr);
212 if (n < 0 || buf_size < n) return;
213 buffer += n; buf_size -= n;
216 if (buf_size < 4) return;
217 lptr = (afs_int32 *)buffer;
218 buffer += 4; buf_size -= 4;
220 *lptr = ntohl(*lptr);
224 *lptr = htonl(*lptr);
226 if (n < 0 || buf_size < n) return;
227 if (n >= sizeof(struct ClearToken)) {
228 ticket = (struct ClearToken *)buffer;
230 ticket->AuthHandle = ntohl(ticket->AuthHandle);
231 ticket->ViceId = ntohl(ticket->ViceId);
232 ticket->BeginTimestamp = ntohl(ticket->BeginTimestamp);
233 ticket->EndTimestamp = ntohl(ticket->EndTimestamp);
235 ticket->AuthHandle = htonl(ticket->AuthHandle);
236 ticket->ViceId = htonl(ticket->ViceId);
237 ticket->BeginTimestamp = htonl(ticket->BeginTimestamp);
238 ticket->EndTimestamp = htonl(ticket->EndTimestamp);
241 buffer += n; buf_size -= n;
244 if (buf_size < 4) return;
245 lptr = (afs_int32 *)buffer;
247 *lptr = ntohl(*lptr);
249 *lptr = htonl((*lptr) & ~0x8000);
255 FetchVolumeStatus_conversion(char *buffer, int buf_size, int in)
257 AFSFetchVolumeStatus *status = (AFSFetchVolumeStatus *)buffer;
259 if (buf_size < sizeof(AFSFetchVolumeStatus))
262 status->Vid = ntohl(status->Vid);
263 status->ParentId = ntohl(status->ParentId);
264 status->Type = ntohl(status->Type);
265 status->MinQuota = ntohl(status->MinQuota);
266 status->MaxQuota = ntohl(status->MaxQuota);
267 status->BlocksInUse = ntohl(status->BlocksInUse);
268 status->PartBlocksAvail = ntohl(status->PartBlocksAvail);
269 status->PartMaxBlocks = ntohl(status->PartMaxBlocks);
271 status->Vid = htonl(status->Vid);
272 status->ParentId = htonl(status->ParentId);
273 status->Type = htonl(status->Type);
274 status->MinQuota = htonl(status->MinQuota);
275 status->MaxQuota = htonl(status->MaxQuota);
276 status->BlocksInUse = htonl(status->BlocksInUse);
277 status->PartBlocksAvail = htonl(status->PartBlocksAvail);
278 status->PartMaxBlocks = htonl(status->PartMaxBlocks);
283 inparam_conversion(int cmd, char *buffer, int buf_size, int in)
285 afs_int32 *lptr = (afs_int32 *)buffer;
287 switch (cmd & 0xffff) {
288 case (0x5600 | 3): /* VIOCSETTOK */
289 token_conversion(buffer, buf_size, in);
292 case (0x5600 | 5): /* VIOCSETVOLSTAT */
293 FetchVolumeStatus_conversion(buffer, buf_size, in);
296 case (0x5600 | 8): /* VIOCGETTOK */
297 case (0x5600 | 10): /* VIOCCKSERV */
298 case (0x5600 | 20): /* VIOCACCESS */
299 case (0x5600 | 24): /* VIOCSETCACHESIZE */
300 case (0x5600 | 27): /* VIOCGETCELL */
301 case (0x5600 | 32): /* VIOC_AFS_MARINER_HOST */
302 case (0x5600 | 34): /* VIOC_VENUSLOG */
303 case (0x5600 | 38): /* VIOC_AFS_SYSNAME */
304 case (0x5600 | 39): /* VIOC_EXPORTAFS */
305 /* one 32-bit integer */
307 if (in) lptr[0] = ntohl(lptr[0]);
308 else lptr[0] = htonl(lptr[0]);
312 case (0x5600 | 36): /* VIOCSETCELLSTATUS */
313 /* two 32-bit integers */
315 if (in) lptr[0] = ntohl(lptr[0]);
316 else lptr[0] = htonl(lptr[0]);
319 if (in) lptr[1] = ntohl(lptr[1]);
320 else lptr[1] = htonl(lptr[1]);
327 outparam_conversion(int cmd, char *buffer, int buf_size, int in)
329 afs_int32 *lptr = (afs_int32 *)buffer;
332 switch (cmd & 0xffff) {
333 case (0x5600 | 4): /* VIOCGETVOLSTAT */
334 case (0x5600 | 5): /* VIOCSETVOLSTAT */
335 FetchVolumeStatus_conversion(buffer, buf_size, in);
338 case (0x5600 | 8): /* VIOCGETTOK */
339 token_conversion(buffer, buf_size, in);
342 case (0x5600 | 12): /* VIOCCKCONN */
343 case (0x5600 | 32): /* VIOC_AFS_MARINER_HOST */
344 case (0x5600 | 34): /* VIOC_VENUSLOG */
345 case (0x5600 | 35): /* VIOC_GETCELLSTATUS */
346 case (0x5600 | 38): /* VIOC_AFS_SYSNAME */
347 case (0x5600 | 39): /* VIOC_EXPORTAFS */
348 /* one 32-bit integer */
350 if (in) lptr[0] = ntohl(lptr[0]);
351 else lptr[0] = htonl(lptr[0]);
355 case (0x5600 | 40): /* VIOCGETCACHEPARMS */
356 /* sixteen 32-bit integers */
357 for (i = 0; i < 16 && buf_size >= 4; i++) {
358 if (in) lptr[i] = ntohl(lptr[i]);
359 else lptr[i] = htonl(lptr[i]);
367 /* called with the GLOCK held */
370 afs_syscall_pioctl(path, com, cmarg, follow, rvp, credp)
374 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
375 afs_syscall_pioctl(path, com, cmarg, follow, credp)
378 afs_syscall_pioctl(path, com, cmarg, follow)
387 struct ucred *credp = crref(); /* don't free until done! */
389 #ifdef AFS_LINUX22_ENV
390 cred_t *credp = crref(); /* don't free until done! */
392 struct afs_ioctl data;
393 struct clientcred ccred;
394 struct rmtbulk idata, odata;
395 short in_size, out_size;
396 afs_int32 code = 0, pag, err;
398 char *abspath, *pathbuf = 0;
400 AFS_STATCNT(afs_syscall_pioctl);
402 follow = 1; /* compat. with old venus */
403 code = copyin_afs_ioctl(cmarg, &data);
406 if ((com & 0xff) == 90) {
407 /* PSetClientContext, in any space */
412 /* Special handling for a few pioctls */
413 switch (com & 0xffff) {
414 case (0x5600 | 3): /* VIOCSETTOK */
415 code = afspag_PSetTokens(data.in, data.in_size, &credp);
419 case (0x5600 | 9): /* VIOCUNLOG */
420 case (0x5600 | 21): /* VIOCUNPAG */
421 code = afspag_PUnlog(data.in, data.in_size, &credp);
425 case (0x5600 | 38): /* VIOC_AFS_SYSNAME */
426 code = afspag_PSetSysName(data.in, data.in_size, &credp);
431 /* Set up credentials */
432 memset(&ccred, 0, sizeof(ccred));
433 pag = PagInCred(credp);
434 ccred.uid = afs_cr_uid(credp);
436 afs_get_groups_from_pag(pag, &g0, &g1);
442 * Copy the path and convert to absolute, if one was given.
443 * NB: We can only use osI_AllocLargeSpace here as long as
444 * RMTSYS_MAXPATHLEN is less than AFS_LRALLOCSIZ.
447 pathbuf = osi_AllocLargeSpace(RMTSYS_MAXPATHLEN);
452 code = osi_abspath(path, pathbuf, RMTSYS_MAXPATHLEN, 0, &abspath);
459 /* Allocate, copy, and convert incoming data */
460 idata.rmtbulk_len = in_size = data.in_size;
461 if (in_size < 0 || in_size > MAXBUFFERLEN) {
465 if (in_size > AFS_LRALLOCSIZ)
466 idata.rmtbulk_val = osi_Alloc(in_size);
468 idata.rmtbulk_val = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
469 if (!idata.rmtbulk_val) {
474 AFS_COPYIN(data.in, idata.rmtbulk_val, in_size, code);
477 inparam_conversion(com, idata.rmtbulk_val, in_size, 0);
480 /* Allocate space for outgoing data */
481 odata.rmtbulk_len = out_size = data.out_size;
482 if (out_size < 0 || out_size > MAXBUFFERLEN) {
486 if (out_size > AFS_LRALLOCSIZ)
487 odata.rmtbulk_val = osi_Alloc(out_size);
489 odata.rmtbulk_val = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
490 if (!odata.rmtbulk_val) {
496 code = RMTSYS_Pioctl(rmtsys_conn, &ccred, abspath, com, follow,
497 &idata, &odata, &err);
502 /* Convert and copy out the result */
503 if (odata.rmtbulk_len > out_size) {
507 if (odata.rmtbulk_len) {
508 outparam_conversion(com, odata.rmtbulk_val, odata.rmtbulk_len, 1);
509 AFS_COPYOUT(odata.rmtbulk_val, data.out, odata.rmtbulk_len, code);
515 if (out_size > AFS_LRALLOCSIZ)
516 osi_Free(odata.rmtbulk_val, out_size);
518 osi_FreeLargeSpace(odata.rmtbulk_val);
521 if (in_size > AFS_LRALLOCSIZ)
522 osi_Free(idata.rmtbulk_val, in_size);
524 osi_FreeLargeSpace(idata.rmtbulk_val);
528 osi_FreeLargeSpace(pathbuf);
531 #if defined(AFS_LINUX22_ENV) || defined(AFS_AIX41_ENV)
534 #if defined(KERNEL_HAVE_UERROR)
537 return (getuerror());
545 afs_syscall_call(parm, parm2, parm3, parm4, parm5, parm6)
546 long parm, parm2, parm3, parm4, parm5, parm6;
548 /* superusers may shut us down, as with afsd --shutdown */
550 if (parm == AFSOP_SHUTDOWN && afs_suser(CRED()))
552 if (parm == AFSOP_SHUTDOWN && afs_suser(NULL))
561 /* otherwise, we don't support afs_syscall_call, period */
562 #if defined(KERNEL_HAVE_UERROR)