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) */
32 afs_int32 afs_termState = 0;
33 afs_int32 afs_gcpags = AFS_GCPAGS;
34 int afs_shuttingdown = 0;
35 int afs_cold_shutdown = 0;
36 int afs_resourceinit_flag = 0;
37 afs_int32 afs_nfs_server_addr;
38 struct interfaceAddr afs_cb_interface;
39 struct afs_osi_WaitHandle AFS_WaitHandler;
40 static struct rx_securityClass *srv_secobj;
41 static struct rx_securityClass *clt_secobj;
42 static struct rx_service *stats_svc;
43 static struct rx_service *pagcb_svc;
44 static struct rx_connection *rmtsys_conn;
45 char *afs_sysname = 0;
46 char *afs_sysnamelist[MAXNUMSYSNAMES];
47 int afs_sysnamecount = 0;
48 int afs_sysnamegen = 0;
49 afs_int32 afs_showflags = GAGUSER | GAGCONSOLE; /* show all messages */
55 afs_int32 now, last10MinCheck, last60MinCheck;
63 if (last10MinCheck + 600 < now) {
67 if (last60MinCheck + 3600 < now) {
72 now = 20000 - (osi_Time() - now);
73 afs_osi_Wait(now, &AFS_WaitHandler, 0);
75 if (afs_termState == AFSOP_STOP_AFS) {
76 #if defined(AFS_SUN5_ENV) || defined(RXK_LISTENER_ENV)
77 afs_termState = AFSOP_STOP_RXEVENT;
79 afs_termState = AFSOP_STOP_COMPLETE;
81 afs_osi_Wakeup(&afs_termState);
89 afspag_Init(afs_int32 nfs_server_addr)
91 struct clientcred ccred;
92 struct rmtbulk idata, odata;
93 afs_int32 code, err, addr, obuf;
96 afs_uuid_create(&afs_cb_interface.uuid);
101 rx_Init(htons(7001));
103 AFS_STATCNT(afs_ResourceInit);
104 AFS_RWLOCK_INIT(&afs_xuser, "afs_xuser");
105 AFS_RWLOCK_INIT(&afs_xpagcell, "afs_xpagcell");
106 AFS_RWLOCK_INIT(&afs_xpagsys, "afs_xpagsys");
107 AFS_RWLOCK_INIT(&afs_icl_lock, "afs_icl_lock");
109 afs_resourceinit_flag = 1;
110 afs_nfs_server_addr = nfs_server_addr;
111 for (i = 0; i < MAXNUMSYSNAMES; i++) {
112 afs_sysnamelist[i] = afs_osi_Alloc(MAXSYSNAME);
113 osi_Assert(afs_sysnamelist[i] != NULL);
115 afs_sysname = afs_sysnamelist[0];
116 strcpy(afs_sysname, SYS_NAME);
117 afs_sysnamecount = 1;
120 srv_secobj = rxnull_NewServerSecurityObject();
121 stats_svc = rx_NewService(0, RX_STATS_SERVICE_ID, "rpcstats", &srv_secobj,
122 1, RXSTATS_ExecuteRequest);
123 pagcb_svc = rx_NewService(0, PAGCB_SERVICEID, "pagcb", &srv_secobj,
124 1, PAGCB_ExecuteRequest);
127 clt_secobj = rxnull_NewClientSecurityObject();
128 rmtsys_conn = rx_NewConnection(nfs_server_addr, htons(7009),
129 RMTSYS_SERVICEID, clt_secobj, 0);
131 #ifdef RXK_LISTENER_ENV
132 afs_start_thread(rxk_Listener, "Rx Listener");
134 afs_start_thread((void *)(void *)rx_ServerProc, "Rx Server Thread");
135 afs_start_thread(afs_rxevent_daemon, "Rx Event Daemon");
136 afs_start_thread(afs_Daemon, "AFS PAG Daemon");
142 /* If it's reachable, tell the translator to nuke our creds.
143 * We should be more agressive about making sure this gets done,
144 * even if the translator is unreachable when we boot.
146 addr = obuf = err = 0;
147 idata.rmtbulk_len = sizeof(addr);
148 idata.rmtbulk_val = (char *)&addr;
149 odata.rmtbulk_len = sizeof(obuf);
150 odata.rmtbulk_val = (char *)&obuf;
151 memset(&ccred, 0, sizeof(ccred));
152 code = RMTSYS_Pioctl(rmtsys_conn, &ccred, NIL_PATHP, 0x4F01, 0,
153 &idata, &odata, &err);
154 } /*afs_ResourceInit */
157 /* called with the GLOCK held */
159 afspag_Shutdown(void)
161 if (afs_shuttingdown)
163 afs_shuttingdown = 1;
164 afs_termState = AFSOP_STOP_RXCALLBACK;
165 rx_WakeupServerProcs();
166 while (afs_termState == AFSOP_STOP_RXCALLBACK)
167 afs_osi_Sleep(&afs_termState);
168 /* rx_ServerProc sets AFS_STOP_AFS */
170 while (afs_termState == AFSOP_STOP_AFS) {
171 afs_osi_CancelWait(&AFS_WaitHandler);
172 afs_osi_Sleep(&afs_termState);
174 /* afs_Daemon sets AFS_STOP_RXEVENT */
176 #if defined(AFS_SUN5_ENV) || defined(RXK_LISTENER_ENV)
177 while (afs_termState == AFSOP_STOP_RXEVENT)
178 afs_osi_Sleep(&afs_termState);
179 /* afs_rxevent_daemon sets AFSOP_STOP_RXK_LISTENER */
181 #if defined(RXK_LISTENER_ENV)
182 afs_osi_UnmaskRxkSignals();
184 while (afs_termState == AFSOP_STOP_RXK_LISTENER)
185 afs_osi_Sleep(&afs_termState);
186 /* rxk_Listener sets AFSOP_STOP_COMPLETE */
192 token_conversion(char *buffer, int buf_size, int in)
194 struct ClearToken *ticket;
198 if (buf_size < 4) return;
199 lptr = (afs_int32 *)buffer;
200 buffer += 4; buf_size -= 4;
202 *lptr = ntohl(*lptr);
206 *lptr = htonl(*lptr);
208 if (n < 0 || buf_size < n) return;
209 buffer += n; buf_size -= n;
212 if (buf_size < 4) return;
213 lptr = (afs_int32 *)buffer;
214 buffer += 4; buf_size -= 4;
216 *lptr = ntohl(*lptr);
220 *lptr = htonl(*lptr);
222 if (n < 0 || buf_size < n) return;
223 if (n >= sizeof(struct ClearToken)) {
224 ticket = (struct ClearToken *)buffer;
226 ticket->AuthHandle = ntohl(ticket->AuthHandle);
227 ticket->ViceId = ntohl(ticket->ViceId);
228 ticket->BeginTimestamp = ntohl(ticket->BeginTimestamp);
229 ticket->EndTimestamp = ntohl(ticket->EndTimestamp);
231 ticket->AuthHandle = htonl(ticket->AuthHandle);
232 ticket->ViceId = htonl(ticket->ViceId);
233 ticket->BeginTimestamp = htonl(ticket->BeginTimestamp);
234 ticket->EndTimestamp = htonl(ticket->EndTimestamp);
237 buffer += n; buf_size -= n;
240 if (buf_size < 4) return;
241 lptr = (afs_int32 *)buffer;
243 *lptr = ntohl(*lptr);
245 *lptr = htonl((*lptr) & ~0x8000);
251 FetchVolumeStatus_conversion(char *buffer, int buf_size, int in)
253 AFSFetchVolumeStatus *status = (AFSFetchVolumeStatus *)buffer;
255 if (buf_size < sizeof(AFSFetchVolumeStatus))
258 status->Vid = ntohl(status->Vid);
259 status->ParentId = ntohl(status->ParentId);
260 status->Type = ntohl(status->Type);
261 status->MinQuota = ntohl(status->MinQuota);
262 status->MaxQuota = ntohl(status->MaxQuota);
263 status->BlocksInUse = ntohl(status->BlocksInUse);
264 status->PartBlocksAvail = ntohl(status->PartBlocksAvail);
265 status->PartMaxBlocks = ntohl(status->PartMaxBlocks);
267 status->Vid = htonl(status->Vid);
268 status->ParentId = htonl(status->ParentId);
269 status->Type = htonl(status->Type);
270 status->MinQuota = htonl(status->MinQuota);
271 status->MaxQuota = htonl(status->MaxQuota);
272 status->BlocksInUse = htonl(status->BlocksInUse);
273 status->PartBlocksAvail = htonl(status->PartBlocksAvail);
274 status->PartMaxBlocks = htonl(status->PartMaxBlocks);
279 inparam_conversion(int cmd, char *buffer, int buf_size, int in)
281 afs_int32 *lptr = (afs_int32 *)buffer;
283 switch (cmd & 0xffff) {
284 case (0x5600 | 3): /* VIOCSETTOK */
285 token_conversion(buffer, buf_size, in);
288 case (0x5600 | 5): /* VIOCSETVOLSTAT */
289 FetchVolumeStatus_conversion(buffer, buf_size, in);
292 case (0x5600 | 8): /* VIOCGETTOK */
293 case (0x5600 | 10): /* VIOCCKSERV */
294 case (0x5600 | 20): /* VIOCACCESS */
295 case (0x5600 | 24): /* VIOCSETCACHESIZE */
296 case (0x5600 | 27): /* VIOCGETCELL */
297 case (0x5600 | 32): /* VIOC_AFS_MARINER_HOST */
298 case (0x5600 | 34): /* VIOC_VENUSLOG */
299 case (0x5600 | 38): /* VIOC_AFS_SYSNAME */
300 case (0x5600 | 39): /* VIOC_EXPORTAFS */
301 /* one 32-bit integer */
303 if (in) lptr[0] = ntohl(lptr[0]);
304 else lptr[0] = htonl(lptr[0]);
308 case (0x5600 | 36): /* VIOCSETCELLSTATUS */
309 /* two 32-bit integers */
311 if (in) lptr[0] = ntohl(lptr[0]);
312 else lptr[0] = htonl(lptr[0]);
315 if (in) lptr[1] = ntohl(lptr[1]);
316 else lptr[1] = htonl(lptr[1]);
323 outparam_conversion(int cmd, char *buffer, int buf_size, int in)
325 afs_int32 *lptr = (afs_int32 *)buffer;
328 switch (cmd & 0xffff) {
329 case (0x5600 | 4): /* VIOCGETVOLSTAT */
330 case (0x5600 | 5): /* VIOCSETVOLSTAT */
331 FetchVolumeStatus_conversion(buffer, buf_size, in);
334 case (0x5600 | 8): /* VIOCGETTOK */
335 token_conversion(buffer, buf_size, in);
338 case (0x5600 | 12): /* VIOCCKCONN */
339 case (0x5600 | 32): /* VIOC_AFS_MARINER_HOST */
340 case (0x5600 | 34): /* VIOC_VENUSLOG */
341 case (0x5600 | 35): /* VIOC_GETCELLSTATUS */
342 case (0x5600 | 38): /* VIOC_AFS_SYSNAME */
343 case (0x5600 | 39): /* VIOC_EXPORTAFS */
344 /* one 32-bit integer */
346 if (in) lptr[0] = ntohl(lptr[0]);
347 else lptr[0] = htonl(lptr[0]);
351 case (0x5600 | 40): /* VIOCGETCACHEPARMS */
352 /* sixteen 32-bit integers */
353 for (i = 0; i < 16 && buf_size >= 4; i++) {
354 if (in) lptr[i] = ntohl(lptr[i]);
355 else lptr[i] = htonl(lptr[i]);
363 /* called with the GLOCK held */
366 afs_syscall_pioctl(path, com, cmarg, follow, rvp, credp)
370 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
371 afs_syscall_pioctl(path, com, cmarg, follow, credp)
374 afs_syscall_pioctl(path, com, cmarg, follow)
383 struct ucred *credp = crref(); /* don't free until done! */
385 #ifdef AFS_LINUX22_ENV
386 cred_t *credp = crref(); /* don't free until done! */
388 struct afs_ioctl data;
389 struct clientcred ccred;
390 struct rmtbulk idata, odata;
391 short in_size, out_size;
392 afs_int32 code = 0, pag, err;
394 char *abspath, *pathbuf = 0;
396 AFS_STATCNT(afs_syscall_pioctl);
398 follow = 1; /* compat. with old venus */
399 code = copyin_afs_ioctl(cmarg, &data);
402 if ((com & 0xff) == 90) {
403 /* PSetClientContext, in any space */
408 /* Special handling for a few pioctls */
409 switch (com & 0xffff) {
410 case (0x5600 | 3): /* VIOCSETTOK */
411 code = afspag_PSetTokens(data.in, data.in_size, &credp);
415 case (0x5600 | 9): /* VIOCUNLOG */
416 case (0x5600 | 21): /* VIOCUNPAG */
417 code = afspag_PUnlog(data.in, data.in_size, &credp);
421 case (0x5600 | 38): /* VIOC_AFS_SYSNAME */
422 code = afspag_PSetSysName(data.in, data.in_size, &credp);
427 /* Set up credentials */
428 memset(&ccred, 0, sizeof(ccred));
429 pag = PagInCred(credp);
430 ccred.uid = afs_cr_uid(credp);
432 afs_get_groups_from_pag(pag, &g0, &g1);
438 * Copy the path and convert to absolute, if one was given.
439 * NB: We can only use osI_AllocLargeSpace here as long as
440 * RMTSYS_MAXPATHLEN is less than AFS_LRALLOCSIZ.
443 pathbuf = osi_AllocLargeSpace(RMTSYS_MAXPATHLEN);
448 code = osi_abspath(path, pathbuf, RMTSYS_MAXPATHLEN, 0, &abspath);
455 /* Allocate, copy, and convert incoming data */
456 idata.rmtbulk_len = in_size = data.in_size;
457 if (in_size < 0 || in_size > MAXBUFFERLEN) {
461 if (in_size > AFS_LRALLOCSIZ)
462 idata.rmtbulk_val = osi_Alloc(in_size);
464 idata.rmtbulk_val = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
465 if (!idata.rmtbulk_val) {
470 AFS_COPYIN(data.in, idata.rmtbulk_val, in_size, code);
473 inparam_conversion(com, idata.rmtbulk_val, in_size, 0);
476 /* Allocate space for outgoing data */
477 odata.rmtbulk_len = out_size = data.out_size;
478 if (out_size < 0 || out_size > MAXBUFFERLEN) {
482 if (out_size > AFS_LRALLOCSIZ)
483 odata.rmtbulk_val = osi_Alloc(out_size);
485 odata.rmtbulk_val = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
486 if (!odata.rmtbulk_val) {
492 code = RMTSYS_Pioctl(rmtsys_conn, &ccred, abspath, com, follow,
493 &idata, &odata, &err);
498 /* Convert and copy out the result */
499 if (odata.rmtbulk_len > out_size) {
503 if (odata.rmtbulk_len) {
504 outparam_conversion(com, odata.rmtbulk_val, odata.rmtbulk_len, 1);
505 AFS_COPYOUT(odata.rmtbulk_val, data.out, odata.rmtbulk_len, code);
511 if (out_size > AFS_LRALLOCSIZ)
512 osi_Free(odata.rmtbulk_val, out_size);
514 osi_FreeLargeSpace(odata.rmtbulk_val);
517 if (in_size > AFS_LRALLOCSIZ)
518 osi_Free(idata.rmtbulk_val, in_size);
520 osi_FreeLargeSpace(idata.rmtbulk_val);
524 osi_FreeLargeSpace(pathbuf);
527 #if defined(AFS_LINUX22_ENV) || defined(AFS_AIX41_ENV)
530 #if defined(KERNEL_HAVE_UERROR)
533 return (getuerror());
541 afs_syscall_call(parm, parm2, parm3, parm4, parm5, parm6)
542 long parm, parm2, parm3, parm4, parm5, parm6;
544 /* superusers may shut us down, as with afsd --shutdown */
546 if (parm == AFSOP_SHUTDOWN && afs_suser(CRED()))
548 if (parm == AFSOP_SHUTDOWN && afs_suser(NULL))
557 /* otherwise, we don't support afs_syscall_call, period */
558 #if defined(KERNEL_HAVE_UERROR)