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"
16 #include "afs/sysincludes.h" /* Standard vendor system headers */
17 #include "afsincludes.h" /* Afs-based standard headers */
18 #include "afs/afs_stats.h"
19 #include "rx/rx_globals.h"
20 #if !defined(UKERNEL) && !defined(AFS_LINUX20_ENV)
23 #include "h/hashing.h"
25 #if !defined(AFS_HPUX110_ENV) && !defined(AFS_DARWIN60_ENV)
26 #include "netinet/in_var.h"
28 #endif /* !defined(UKERNEL) */
29 #ifdef AFS_LINUX22_ENV
30 #include "h/smp_lock.h"
36 afs_int32 afs_termState = 0;
37 afs_int32 afs_gcpags = AFS_GCPAGS;
38 int afs_shuttingdown = 0;
39 int afs_cold_shutdown = 0;
40 int afs_resourceinit_flag = 0;
41 afs_int32 afs_nfs_server_addr;
42 struct interfaceAddr afs_cb_interface;
43 struct afs_osi_WaitHandle AFS_WaitHandler;
44 static struct rx_securityClass *srv_secobj;
45 static struct rx_securityClass *clt_secobj;
46 static struct rx_service *stats_svc;
47 static struct rx_service *pagcb_svc;
48 static struct rx_connection *rmtsys_conn;
49 char *afs_sysname = 0;
50 char *afs_sysnamelist[MAXNUMSYSNAMES];
51 int afs_sysnamecount = 0;
52 int afs_sysnamegen = 0;
53 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);
91 void afspag_Init(afs_int32 nfs_server_addr)
93 struct clientcred ccred;
94 struct rmtbulk idata, odata;
95 afs_int32 code, err, addr, obuf;
98 afs_uuid_create(&afs_cb_interface.uuid);
103 rx_Init(htons(7001));
105 AFS_STATCNT(afs_ResourceInit);
106 RWLOCK_INIT(&afs_xuser, "afs_xuser");
107 RWLOCK_INIT(&afs_xpagcell, "afs_xpagcell");
108 RWLOCK_INIT(&afs_xpagsys, "afs_xpagsys");
109 RWLOCK_INIT(&afs_icl_lock, "afs_icl_lock");
111 LOCK_INIT(&osi_fsplock, "osi_fsplock");
112 LOCK_INIT(&osi_flplock, "osi_flplock");
115 afs_resourceinit_flag = 1;
116 afs_nfs_server_addr = nfs_server_addr;
117 for (i = 0; i < MAXNUMSYSNAMES; i++)
118 afs_sysnamelist[i] = afs_osi_Alloc(MAXSYSNAME);
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(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 */
162 void afspag_Shutdown(void)
164 if (afs_shuttingdown)
166 afs_shuttingdown = 1;
167 afs_termState = AFSOP_STOP_RXCALLBACK;
168 rx_WakeupServerProcs();
169 while (afs_termState == AFSOP_STOP_RXCALLBACK)
170 afs_osi_Sleep(&afs_termState);
171 /* rx_ServerProc sets AFS_STOP_AFS */
173 while (afs_termState == AFSOP_STOP_AFS) {
174 afs_osi_CancelWait(&AFS_WaitHandler);
175 afs_osi_Sleep(&afs_termState);
177 /* afs_Daemon sets AFS_STOP_RXEVENT */
179 #if defined(AFS_SUN5_ENV) || defined(RXK_LISTENER_ENV)
180 while (afs_termState == AFSOP_STOP_RXEVENT)
181 afs_osi_Sleep(&afs_termState);
182 /* afs_rxevent_daemon sets AFSOP_STOP_RXK_LISTENER */
184 #if defined(RXK_LISTENER_ENV)
185 afs_osi_UnmaskRxkSignals();
187 while (afs_termState == AFSOP_STOP_RXK_LISTENER)
188 afs_osi_Sleep(&afs_termState);
189 /* rxk_Listener sets AFSOP_STOP_COMPLETE */
194 static void token_conversion(char *buffer, int buf_size, int in)
196 struct ClearToken *ticket;
200 if (buf_size < 4) return;
201 lptr = (afs_int32 *)buffer;
202 buffer += 4; buf_size -= 4;
204 *lptr = ntohl(*lptr);
208 *lptr = htonl(*lptr);
210 if (n < 0 || buf_size < n) return;
211 buffer += n; buf_size -= n;
214 if (buf_size < 4) return;
215 lptr = (afs_int32 *)buffer;
216 buffer += 4; buf_size -= 4;
218 *lptr = ntohl(*lptr);
222 *lptr = htonl(*lptr);
224 if (n < 0 || buf_size < n) return;
225 if (n >= sizeof(struct ClearToken)) {
226 ticket = (struct ClearToken *)buffer;
228 ticket->AuthHandle = ntohl(ticket->AuthHandle);
229 ticket->ViceId = ntohl(ticket->ViceId);
230 ticket->BeginTimestamp = ntohl(ticket->BeginTimestamp);
231 ticket->EndTimestamp = ntohl(ticket->EndTimestamp);
233 ticket->AuthHandle = htonl(ticket->AuthHandle);
234 ticket->ViceId = htonl(ticket->ViceId);
235 ticket->BeginTimestamp = htonl(ticket->BeginTimestamp);
236 ticket->EndTimestamp = htonl(ticket->EndTimestamp);
239 buffer += n; buf_size -= n;
242 if (buf_size < 4) return;
243 lptr = (afs_int32 *)buffer;
245 *lptr = ntohl(*lptr);
247 *lptr = htonl((*lptr) & ~0x8000);
252 static void FetchVolumeStatus_conversion(char *buffer, int buf_size, int in)
254 AFSFetchVolumeStatus *status = (AFSFetchVolumeStatus *)buffer;
256 if (buf_size < sizeof(AFSFetchVolumeStatus))
259 status->Vid = ntohl(status->Vid);
260 status->ParentId = ntohl(status->ParentId);
261 status->Type = ntohl(status->Type);
262 status->MinQuota = ntohl(status->MinQuota);
263 status->MaxQuota = ntohl(status->MaxQuota);
264 status->BlocksInUse = ntohl(status->BlocksInUse);
265 status->PartBlocksAvail = ntohl(status->PartBlocksAvail);
266 status->PartMaxBlocks = ntohl(status->PartMaxBlocks);
268 status->Vid = htonl(status->Vid);
269 status->ParentId = htonl(status->ParentId);
270 status->Type = htonl(status->Type);
271 status->MinQuota = htonl(status->MinQuota);
272 status->MaxQuota = htonl(status->MaxQuota);
273 status->BlocksInUse = htonl(status->BlocksInUse);
274 status->PartBlocksAvail = htonl(status->PartBlocksAvail);
275 status->PartMaxBlocks = htonl(status->PartMaxBlocks);
279 static void 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]);
322 static void outparam_conversion(int cmd, char *buffer, int buf_size, int in)
324 afs_int32 *lptr = (afs_int32 *)buffer;
327 switch (cmd & 0xffff) {
328 case (0x5600 | 4): /* VIOCGETVOLSTAT */
329 case (0x5600 | 5): /* VIOCSETVOLSTAT */
330 FetchVolumeStatus_conversion(buffer, buf_size, in);
333 case (0x5600 | 8): /* VIOCGETTOK */
334 token_conversion(buffer, buf_size, in);
337 case (0x5600 | 12): /* VIOCCKCONN */
338 case (0x5600 | 32): /* VIOC_AFS_MARINER_HOST */
339 case (0x5600 | 34): /* VIOC_VENUSLOG */
340 case (0x5600 | 35): /* VIOC_GETCELLSTATUS */
341 case (0x5600 | 38): /* VIOC_AFS_SYSNAME */
342 case (0x5600 | 39): /* VIOC_EXPORTAFS */
343 /* one 32-bit integer */
345 if (in) lptr[0] = ntohl(lptr[0]);
346 else lptr[0] = htonl(lptr[0]);
350 case (0x5600 | 40): /* VIOCGETCACHEPARMS */
351 /* sixteen 32-bit integers */
352 for (i = 0; i < 16 && buf_size >= 4; i++) {
353 if (in) lptr[i] = ntohl(lptr[i]);
354 else lptr[i] = htonl(lptr[i]);
362 /* called with the GLOCK held */
365 afs_syscall_pioctl(path, com, cmarg, follow, rvp, credp)
367 struct AFS_UCRED *credp;
369 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
370 afs_syscall_pioctl(path, com, cmarg, follow, credp)
371 struct AFS_UCRED *credp;
373 afs_syscall_pioctl(path, com, cmarg, follow)
382 struct ucred *credp = crref(); /* don't free until done! */
384 #ifdef AFS_LINUX22_ENV
385 cred_t *credp = crref(); /* don't free until done! */
387 struct afs_ioctl data;
388 struct clientcred ccred;
389 struct rmtbulk idata, odata;
390 short in_size, out_size;
391 afs_int32 code = 0, pag, err;
393 char *abspath, *pathbuf = 0;
395 AFS_STATCNT(afs_syscall_pioctl);
397 follow = 1; /* compat. with old venus */
398 code = copyin_afs_ioctl(cmarg, &data);
401 if ((com & 0xff) == 90) {
402 /* PSetClientContext, in any space */
407 /* Special handling for a few pioctls */
408 switch (com & 0xffff) {
409 case (0x5600 | 3): /* VIOCSETTOK */
410 code = afspag_PSetTokens(data.in, data.in_size, &credp);
414 case (0x5600 | 9): /* VIOCUNLOG */
415 case (0x5600 | 21): /* VIOCUNPAG */
416 code = afspag_PUnlog(data.in, data.in_size, &credp);
420 case (0x5600 | 38): /* VIOC_AFS_SYSNAME */
421 code = afspag_PSetSysName(data.in, data.in_size, &credp);
426 /* Set up credentials */
427 memset(&ccred, 0, sizeof(ccred));
428 pag = PagInCred(credp);
429 ccred.uid = credp->cr_uid;
431 afs_get_groups_from_pag(pag, &g0, &g1);
437 * Copy the path and convert to absolute, if one was given.
438 * NB: We can only use osI_AllocLargeSpace here as long as
439 * RMTSYS_MAXPATHLEN is less than AFS_LRALLOCSIZ.
442 pathbuf = osi_AllocLargeSpace(RMTSYS_MAXPATHLEN);
447 code = osi_abspath(path, pathbuf, RMTSYS_MAXPATHLEN, 0, &abspath);
454 /* Allocate, copy, and convert incoming data */
455 idata.rmtbulk_len = in_size = data.in_size;
456 if (in_size < 0 || in_size > MAXBUFFERLEN) {
460 if (in_size > AFS_LRALLOCSIZ)
461 idata.rmtbulk_val = osi_Alloc(in_size);
463 idata.rmtbulk_val = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
464 if (!idata.rmtbulk_val) {
469 AFS_COPYIN(data.in, idata.rmtbulk_val, in_size, code);
472 inparam_conversion(com, idata.rmtbulk_val, in_size, 0);
475 /* Allocate space for outgoing data */
476 odata.rmtbulk_len = out_size = data.out_size;
477 if (out_size < 0 || out_size > MAXBUFFERLEN) {
481 if (out_size > AFS_LRALLOCSIZ)
482 odata.rmtbulk_val = osi_Alloc(out_size);
484 odata.rmtbulk_val = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
485 if (!odata.rmtbulk_val) {
491 code = RMTSYS_Pioctl(rmtsys_conn, &ccred, abspath, com, follow,
492 &idata, &odata, &err);
497 /* Convert and copy out the result */
498 if (odata.rmtbulk_len > out_size) {
502 if (odata.rmtbulk_len) {
503 outparam_conversion(com, odata.rmtbulk_val, odata.rmtbulk_len, 1);
504 AFS_COPYOUT(odata.rmtbulk_val, data.out, odata.rmtbulk_len, code);
510 if (out_size > AFS_LRALLOCSIZ)
511 osi_Free(odata.rmtbulk_val, out_size);
513 osi_FreeLargeSpace(odata.rmtbulk_val);
516 if (in_size > AFS_LRALLOCSIZ)
517 osi_Free(idata.rmtbulk_val, in_size);
519 osi_FreeLargeSpace(idata.rmtbulk_val);
523 osi_FreeLargeSpace(pathbuf);
526 #if defined(AFS_LINUX22_ENV) || defined(AFS_AIX41_ENV)
529 #if defined(KERNEL_HAVE_UERROR)
532 return (getuerror());
540 afs_syscall_call(parm, parm2, parm3, parm4, parm5, parm6)
541 long parm, parm2, parm3, parm4, parm5, parm6;
543 /* superusers may shut us down, as with afsd --shutdown */
545 if (parm == AFSOP_SHUTDOWN && afs_suser(CRED()))
547 if (parm == AFSOP_SHUTDOWN && afs_suser(NULL))
556 /* otherwise, we don't support afs_syscall_call, period */
557 #if defined(KERNEL_HAVE_UERROR)