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
11 * afs_util.c - miscellaneous AFS client utility functions
15 #include <afsconfig.h>
16 #include "afs/param.h"
22 #include "afs/sysincludes.h" /* Standard vendor system headers */
25 #if !defined(AFS_LINUX20_ENV)
28 #include <netinet/in.h>
31 #include "h/hashing.h"
33 #if !defined(AFS_HPUX110_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_DARWIN60_ENV)
34 #include <netinet/in_var.h>
35 #endif /* ! AFS_HPUX110_ENV */
36 #endif /* !defined(UKERNEL) */
38 #include "afsincludes.h" /* Afs-based standard headers */
39 #include "afs/afs_stats.h" /* afs statistics */
41 #if defined(AFS_SUN56_ENV)
43 #include <inet/common.h>
44 #if defined(AFS_SUN58_ENV)
45 #include <netinet/ip6.h>
50 #if defined(AFS_AIX_ENV)
51 #include <sys/fp_io.h>
54 afs_int32 afs_new_inum = 0;
58 afs_cv2string(char *ttp, afs_uint32 aval)
60 register char *tp = ttp;
64 AFS_STATCNT(afs_cv2string);
80 #ifndef afs_strcasecmp
82 afs_strcasecmp(char *s1, char *s2)
89 if (c1 >= 'A' && c1 <= 'Z')
91 if (c2 >= 'A' && c2 <= 'Z')
103 afs_strcat(char *s1, char *s2)
110 while ((*s1++ = *s2++));
115 #ifdef AFS_OBSD34_ENV
117 afs_strcpy(char *s1, char *s2)
122 while ((*s1++ = *s2++) != '\0');
129 afs_strchr(char *s, int c)
141 afs_strrchr(char *s, int c)
160 n = (char *)afs_osi_Alloc(cc);
168 print_internet_address(char *preamble, struct srvAddr *sa, char *postamble,
171 register struct server *aserver = sa->server;
175 AFS_STATCNT(print_internet_address);
176 address = ntohl(sa->sa_ip);
177 if (aserver->flags & SRVR_MULTIHOMED) {
178 if (flag == 1) { /* server down mesg */
179 if (!(aserver->flags & SRVR_ISDOWN))
181 " (multi-homed address; other same-host interfaces maybe up)\n";
183 ptr = " (all multi-homed ip addresses down for the server)\n";
184 } else if (flag == 2) { /* server up mesg */
186 " (multi-homed address; other same-host interfaces may still be down)\n";
189 afs_warn("%s%d.%d.%d.%d in cell %s%s%s", preamble, (address >> 24),
190 (address >> 16) & 0xff, (address >> 8) & 0xff, (address) & 0xff,
191 aserver->cell->cellName, postamble, ptr);
192 afs_warnuser("%s%d.%d.%d.%d in cell %s%s%s", preamble, (address >> 24),
193 (address >> 16) & 0xff, (address >> 8) & 0xff,
194 (address) & 0xff, aserver->cell->cellName, postamble, ptr);
196 } /*print_internet_address */
201 * this code badly needs to be cleaned up... too many ugly ifdefs.
206 afs_warn(char *a, long b, long c, long d, long e, long f, long g, long h,
210 afs_warn(a, b, c, d, e, f, g, h, i, j)
212 #if defined( AFS_USE_VOID_PTR)
213 void *b, *c, *d, *e, *f, *g, *h, *i, *j;
215 long b, c, d, e, f, g, h, i, j;
219 AFS_STATCNT(afs_warn);
221 if (afs_showflags & GAGCONSOLE) {
222 #if defined(AFS_AIX_ENV)
225 /* cf. console_printf() in oncplus/kernext/nfs/serv/shared.c */
227 ("/dev/console", O_WRONLY | O_NOCTTY | O_NDELAY, 0666, 0, FP_SYS,
233 sprintf(buf, a, b, c, d, e, f, g, h, i, j);
235 fp_write(fd, buf, len, 0, UIO_SYSSPACE, &count);
239 printf(a, b, c, d, e, f, g, h, i, j);
246 afs_warnuser(char *a, long b, long c, long d, long e, long f, long g, long h,
250 afs_warnuser(a, b, c, d, e, f, g, h, i, j)
252 long b, c, d, e, f, g, h, i, j;
255 AFS_STATCNT(afs_warnuser);
256 if (afs_showflags & GAGUSER) {
257 #ifdef AFS_GLOBAL_SUNLOCK
258 int haveGlock = ISAFS_GLOCK();
261 #endif /* AFS_GLOBAL_SUNLOCK */
263 uprintf(a, b, c, d, e, f, g, h, i, j);
265 #ifdef AFS_GLOBAL_SUNLOCK
268 #endif /* AFS_GLOBAL_SUNLOCK */
273 /* run everywhere, checking locks */
279 afs_warn("Looking for locked data structures.\n");
280 afs_warn("conn %lx, volume %lx, user %lx, cell %lx, server %lx\n", &afs_xconn,
281 &afs_xvolume, &afs_xuser, &afs_xcell, &afs_xserver);
283 register struct vcache *tvc;
284 AFS_STATCNT(afs_CheckLocks);
286 for (i = 0; i < VCSIZE; i++) {
287 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
288 if (tvc->states & CVInit) continue;
290 if (VREFCOUNT(tvc) > 1)
291 #else /* AFS_OSF_ENV */
292 #ifdef AFS_DARWIN80_ENV
293 if (vnode_isinuse(AFSTOV(tvc), 0))
298 afs_warn("Stat cache entry at %x is held\n", tvc);
299 if (CheckLock(&tvc->lock))
300 afs_warn("Stat entry at %x is locked\n", tvc);
305 register struct dcache *tdc;
306 for (i = 0; i < afs_cacheFiles; i++) {
307 tdc = afs_indexTable[i];
310 afs_warn("Disk entry %d at %x is held\n", i, tdc);
312 if (afs_indexFlags[i] & IFDataMod)
313 afs_warn("Disk entry %d at %x has IFDataMod flag set.\n", i,
321 for (i = 0; i < NSERVERS; i++) {
322 for (ts = afs_servers[i]; ts; ts = ts->next) {
323 if (ts->flags & SRVR_ISDOWN)
324 printf("Server entry %lx is marked down\n", (unsigned long)ts);
325 for (sa = ts->addr; sa; sa = sa->next_sa) {
326 for (tc = sa->conns; tc; tc = tc->next) {
328 afs_warn("conn at %x (server %x) is held\n", tc,
337 for (i = 0; i < NVOLS; i++) {
338 for (tv = afs_volumes[i]; tv; tv = tv->next) {
339 if (CheckLock(&tv->lock))
340 afs_warn("volume at %x is locked\n", tv);
342 afs_warn("volume at %x is held\n", tv);
349 for (i = 0; i < NUSERS; i++) {
350 for (tu = afs_users[i]; tu; tu = tu->next) {
352 printf("user at %lx is held\n", (unsigned long)tu);
363 AFS_STATCNT(afs_noop);
374 AFS_STATCNT(afs_badop);
375 osi_Panic("afs bad vnode op");
380 * afs_data_pointer_to_int32() - returns least significant afs_int32 of the
381 * given data pointer, without triggering "cast truncates pointer"
382 * warnings. We use this where we explicitly don't care whether a
383 * pointer is truncated -- it loses information where a pointer is
384 * larger than an afs_int32.
388 afs_data_pointer_to_int32(const void *p)
391 afs_int32 i32[sizeof(void *) / sizeof(afs_int32)];
395 int i32_sub; /* subscript of least significant afs_int32 in ip.i32[] */
400 /* used to determine the byte order of the system */
403 char c[sizeof(int) / sizeof(char)];
409 /* little-endian system */
412 /* big-endian system */
413 i32_sub = (sizeof ip.i32 / sizeof ip.i32[0]) - 1;
418 return ip.i32[i32_sub];
421 #ifdef AFS_LINUX20_ENV
425 afs_int32 counter[4];
426 unsigned char save[64];
429 static void AFS_MD5_Init (struct afs_md5 *m);
430 static void AFS_MD5_Update (struct afs_md5 *m, const void *p, size_t len);
431 static void AFS_MD5_Final (void *res, struct afs_md5 *m); /* u_int32 res[4] */
433 #define A m->counter[0]
434 #define B m->counter[1]
435 #define C m->counter[2]
436 #define D m->counter[3]
440 AFS_MD5_Init (struct afs_md5 *m)
450 #define F(x,y,z) ((x & y) | (~x & z))
451 #define G(x,y,z) ((x & z) | (y & ~z))
452 #define H(x,y,z) (x ^ y ^ z)
453 #define I(x,y,z) (y ^ (x | ~z))
455 static inline afs_uint32
456 cshift (afs_uint32 x, unsigned int n)
458 return ((x << n) | (x >> (32 - n)));
461 #define DOIT(a,b,c,d,k,s,i,OP) \
462 a = b + cshift(a + OP(b,c,d) + X[k] + (i), s)
464 #define DO1(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,F)
465 #define DO2(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,G)
466 #define DO3(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,H)
467 #define DO4(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,I)
470 calc (struct afs_md5 *m, afs_uint32 *data)
472 afs_uint32 AA, BB, CC, DD;
481 DO1(A,B,C,D,0,7,0xd76aa478);
482 DO1(D,A,B,C,1,12,0xe8c7b756);
483 DO1(C,D,A,B,2,17,0x242070db);
484 DO1(B,C,D,A,3,22,0xc1bdceee);
486 DO1(A,B,C,D,4,7,0xf57c0faf);
487 DO1(D,A,B,C,5,12,0x4787c62a);
488 DO1(C,D,A,B,6,17,0xa8304613);
489 DO1(B,C,D,A,7,22,0xfd469501);
491 DO1(A,B,C,D,8,7,0x698098d8);
492 DO1(D,A,B,C,9,12,0x8b44f7af);
493 DO1(C,D,A,B,10,17,0xffff5bb1);
494 DO1(B,C,D,A,11,22,0x895cd7be);
496 DO1(A,B,C,D,12,7,0x6b901122);
497 DO1(D,A,B,C,13,12,0xfd987193);
498 DO1(C,D,A,B,14,17,0xa679438e);
499 DO1(B,C,D,A,15,22,0x49b40821);
503 DO2(A,B,C,D,1,5,0xf61e2562);
504 DO2(D,A,B,C,6,9,0xc040b340);
505 DO2(C,D,A,B,11,14,0x265e5a51);
506 DO2(B,C,D,A,0,20,0xe9b6c7aa);
508 DO2(A,B,C,D,5,5,0xd62f105d);
509 DO2(D,A,B,C,10,9,0x2441453);
510 DO2(C,D,A,B,15,14,0xd8a1e681);
511 DO2(B,C,D,A,4,20,0xe7d3fbc8);
513 DO2(A,B,C,D,9,5,0x21e1cde6);
514 DO2(D,A,B,C,14,9,0xc33707d6);
515 DO2(C,D,A,B,3,14,0xf4d50d87);
516 DO2(B,C,D,A,8,20,0x455a14ed);
518 DO2(A,B,C,D,13,5,0xa9e3e905);
519 DO2(D,A,B,C,2,9,0xfcefa3f8);
520 DO2(C,D,A,B,7,14,0x676f02d9);
521 DO2(B,C,D,A,12,20,0x8d2a4c8a);
525 DO3(A,B,C,D,5,4,0xfffa3942);
526 DO3(D,A,B,C,8,11,0x8771f681);
527 DO3(C,D,A,B,11,16,0x6d9d6122);
528 DO3(B,C,D,A,14,23,0xfde5380c);
530 DO3(A,B,C,D,1,4,0xa4beea44);
531 DO3(D,A,B,C,4,11,0x4bdecfa9);
532 DO3(C,D,A,B,7,16,0xf6bb4b60);
533 DO3(B,C,D,A,10,23,0xbebfbc70);
535 DO3(A,B,C,D,13,4,0x289b7ec6);
536 DO3(D,A,B,C,0,11,0xeaa127fa);
537 DO3(C,D,A,B,3,16,0xd4ef3085);
538 DO3(B,C,D,A,6,23,0x4881d05);
540 DO3(A,B,C,D,9,4,0xd9d4d039);
541 DO3(D,A,B,C,12,11,0xe6db99e5);
542 DO3(C,D,A,B,15,16,0x1fa27cf8);
543 DO3(B,C,D,A,2,23,0xc4ac5665);
547 DO4(A,B,C,D,0,6,0xf4292244);
548 DO4(D,A,B,C,7,10,0x432aff97);
549 DO4(C,D,A,B,14,15,0xab9423a7);
550 DO4(B,C,D,A,5,21,0xfc93a039);
552 DO4(A,B,C,D,12,6,0x655b59c3);
553 DO4(D,A,B,C,3,10,0x8f0ccc92);
554 DO4(C,D,A,B,10,15,0xffeff47d);
555 DO4(B,C,D,A,1,21,0x85845dd1);
557 DO4(A,B,C,D,8,6,0x6fa87e4f);
558 DO4(D,A,B,C,15,10,0xfe2ce6e0);
559 DO4(C,D,A,B,6,15,0xa3014314);
560 DO4(B,C,D,A,13,21,0x4e0811a1);
562 DO4(A,B,C,D,4,6,0xf7537e82);
563 DO4(D,A,B,C,11,10,0xbd3af235);
564 DO4(C,D,A,B,2,15,0x2ad7d2bb);
565 DO4(B,C,D,A,9,21,0xeb86d391);
574 * From `Performance analysis of MD5' by Joseph D. Touch <touch@isi.edu>
577 #if defined(WORDS_BIGENDIAN)
578 static inline afs_uint32
579 swap_u_int32_t (afs_uint32 t)
581 afs_uint32 temp1, temp2;
583 temp1 = cshift(t, 16);
588 return temp1 | temp2;
598 AFS_MD5_Update (struct afs_md5 *m, const void *v, size_t len)
600 const unsigned char *p = v;
601 size_t old_sz = m->sz[0];
605 if (m->sz[0] < old_sz)
607 offset = (old_sz / 8) % 64;
609 size_t l = MIN(len, 64 - offset);
610 memcpy(m->save + offset, p, l);
615 #if defined(WORDS_BIGENDIAN)
618 struct x32 *us = (struct x32*)m->save;
619 for(i = 0; i < 8; i++){
620 temp[2*i+0] = swap_u_int32_t(us[i].a);
621 temp[2*i+1] = swap_u_int32_t(us[i].b);
625 calc(m, (afs_uint32*)m->save);
633 AFS_MD5_Final (void *res, struct afs_md5 *m)
635 unsigned char zeros[72];
636 unsigned offset = (m->sz[0] / 8) % 64;
637 unsigned int dstart = (120 - offset - 1) % 64 + 1;
640 memset (zeros + 1, 0, sizeof(zeros) - 1);
641 zeros[dstart+0] = (m->sz[0] >> 0) & 0xff;
642 zeros[dstart+1] = (m->sz[0] >> 8) & 0xff;
643 zeros[dstart+2] = (m->sz[0] >> 16) & 0xff;
644 zeros[dstart+3] = (m->sz[0] >> 24) & 0xff;
645 zeros[dstart+4] = (m->sz[1] >> 0) & 0xff;
646 zeros[dstart+5] = (m->sz[1] >> 8) & 0xff;
647 zeros[dstart+6] = (m->sz[1] >> 16) & 0xff;
648 zeros[dstart+7] = (m->sz[1] >> 24) & 0xff;
649 AFS_MD5_Update (m, zeros, dstart + 8);
652 unsigned char *r = (unsigned char *)res;
654 for (i = 0; i < 4; ++i) {
655 r[4*i] = m->counter[i] & 0xFF;
656 r[4*i+1] = (m->counter[i] >> 8) & 0xFF;
657 r[4*i+2] = (m->counter[i] >> 16) & 0xFF;
658 r[4*i+3] = (m->counter[i] >> 24) & 0xFF;
663 afs_int32 afs_calc_inum (afs_int32 volume, afs_int32 vnode)
665 afs_int32 ino, vno = vnode;
671 AFS_MD5_Update(&ct, &volume, 4);
672 AFS_MD5_Update(&ct, &vnode, 4);
673 AFS_MD5_Final(digest, &ct);
674 memcpy(&ino, digest, sizeof(ino_t));
675 ino ^= (ino ^ vno) & 1;
677 ino = (volume << 16) + vnode;
679 ino &= 0x7fffffff; /* Assumes 32 bit ino_t ..... */
685 afs_int32 afs_calc_inum (afs_int32 volume, afs_int32 vnode)
687 return (volume << 16) + vnode;