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)
147 n = (char *)afs_osi_Alloc(cc);
155 print_internet_address(char *preamble, struct srvAddr *sa, char *postamble,
158 register struct server *aserver = sa->server;
162 AFS_STATCNT(print_internet_address);
163 address = ntohl(sa->sa_ip);
164 if (aserver->flags & SRVR_MULTIHOMED) {
165 if (flag == 1) { /* server down mesg */
166 if (!(aserver->flags & SRVR_ISDOWN))
168 " (multi-homed address; other same-host interfaces maybe up)\n";
170 ptr = " (all multi-homed ip addresses down for the server)\n";
171 } else if (flag == 2) { /* server up mesg */
173 " (multi-homed address; other same-host interfaces may still be down)\n";
176 afs_warn("%s%d.%d.%d.%d in cell %s%s%s", preamble, (address >> 24),
177 (address >> 16) & 0xff, (address >> 8) & 0xff, (address) & 0xff,
178 aserver->cell->cellName, postamble, ptr);
179 afs_warnuser("%s%d.%d.%d.%d in cell %s%s%s", preamble, (address >> 24),
180 (address >> 16) & 0xff, (address >> 8) & 0xff,
181 (address) & 0xff, aserver->cell->cellName, postamble, ptr);
183 } /*print_internet_address */
188 * this code badly needs to be cleaned up... too many ugly ifdefs.
193 afs_warn(char *a, long b, long c, long d, long e, long f, long g, long h,
197 afs_warn(a, b, c, d, e, f, g, h, i, j)
199 #if defined( AFS_USE_VOID_PTR)
200 void *b, *c, *d, *e, *f, *g, *h, *i, *j;
202 long b, c, d, e, f, g, h, i, j;
206 AFS_STATCNT(afs_warn);
208 if (afs_showflags & GAGCONSOLE) {
209 #if defined(AFS_AIX_ENV)
212 /* cf. console_printf() in oncplus/kernext/nfs/serv/shared.c */
214 ("/dev/console", O_WRONLY | O_NOCTTY | O_NDELAY, 0666, 0, FP_SYS,
220 sprintf(buf, a, b, c, d, e, f, g, h, i, j);
222 fp_write(fd, buf, len, 0, UIO_SYSSPACE, &count);
226 printf(a, b, c, d, e, f, g, h, i, j);
233 afs_warnuser(char *a, long b, long c, long d, long e, long f, long g, long h,
237 afs_warnuser(a, b, c, d, e, f, g, h, i, j)
239 long b, c, d, e, f, g, h, i, j;
242 AFS_STATCNT(afs_warnuser);
243 if (afs_showflags & GAGUSER) {
244 #ifdef AFS_GLOBAL_SUNLOCK
245 int haveGlock = ISAFS_GLOCK();
248 #endif /* AFS_GLOBAL_SUNLOCK */
250 uprintf(a, b, c, d, e, f, g, h, i, j);
252 #ifdef AFS_GLOBAL_SUNLOCK
255 #endif /* AFS_GLOBAL_SUNLOCK */
260 /* run everywhere, checking locks */
266 afs_warn("Looking for locked data structures.\n");
267 afs_warn("conn %lx, volume %lx, user %lx, cell %lx, server %lx\n", &afs_xconn,
268 &afs_xvolume, &afs_xuser, &afs_xcell, &afs_xserver);
270 register struct vcache *tvc;
271 AFS_STATCNT(afs_CheckLocks);
273 for (i = 0; i < VCSIZE; i++) {
274 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
275 if (tvc->states & CVInit) continue;
277 if (VREFCOUNT(tvc) > 1)
278 #else /* AFS_OSF_ENV */
279 #ifdef AFS_DARWIN80_ENV
280 if (vnode_isinuse(AFSTOV(tvc), 0))
285 afs_warn("Stat cache entry at %x is held\n", tvc);
286 if (CheckLock(&tvc->lock))
287 afs_warn("Stat entry at %x is locked\n", tvc);
292 register struct dcache *tdc;
293 for (i = 0; i < afs_cacheFiles; i++) {
294 tdc = afs_indexTable[i];
297 afs_warn("Disk entry %d at %x is held\n", i, tdc);
299 if (afs_indexFlags[i] & IFDataMod)
300 afs_warn("Disk entry %d at %x has IFDataMod flag set.\n", i,
308 for (i = 0; i < NSERVERS; i++) {
309 for (ts = afs_servers[i]; ts; ts = ts->next) {
310 if (ts->flags & SRVR_ISDOWN)
311 printf("Server entry %lx is marked down\n", (unsigned long)ts);
312 for (sa = ts->addr; sa; sa = sa->next_sa) {
313 for (tc = sa->conns; tc; tc = tc->next) {
315 afs_warn("conn at %x (server %x) is held\n", tc,
324 for (i = 0; i < NVOLS; i++) {
325 for (tv = afs_volumes[i]; tv; tv = tv->next) {
326 if (CheckLock(&tv->lock))
327 afs_warn("volume at %x is locked\n", tv);
329 afs_warn("volume at %x is held\n", tv);
336 for (i = 0; i < NUSERS; i++) {
337 for (tu = afs_users[i]; tu; tu = tu->next) {
339 printf("user at %lx is held\n", (unsigned long)tu);
350 AFS_STATCNT(afs_noop);
361 AFS_STATCNT(afs_badop);
362 osi_Panic("afs bad vnode op");
367 * afs_data_pointer_to_int32() - returns least significant afs_int32 of the
368 * given data pointer, without triggering "cast truncates pointer"
369 * warnings. We use this where we explicitly don't care whether a
370 * pointer is truncated -- it loses information where a pointer is
371 * larger than an afs_int32.
375 afs_data_pointer_to_int32(const void *p)
378 afs_int32 i32[sizeof(void *) / sizeof(afs_int32)];
382 int i32_sub; /* subscript of least significant afs_int32 in ip.i32[] */
387 /* used to determine the byte order of the system */
390 char c[sizeof(int) / sizeof(char)];
396 /* little-endian system */
399 /* big-endian system */
400 i32_sub = (sizeof ip.i32 / sizeof ip.i32[0]) - 1;
405 return ip.i32[i32_sub];
408 #ifdef AFS_LINUX20_ENV
412 afs_int32 counter[4];
413 unsigned char save[64];
416 static void AFS_MD5_Init (struct afs_md5 *m);
417 static void AFS_MD5_Update (struct afs_md5 *m, const void *p, size_t len);
418 static void AFS_MD5_Final (void *res, struct afs_md5 *m); /* u_int32 res[4] */
420 #define A m->counter[0]
421 #define B m->counter[1]
422 #define C m->counter[2]
423 #define D m->counter[3]
427 AFS_MD5_Init (struct afs_md5 *m)
437 #define F(x,y,z) ((x & y) | (~x & z))
438 #define G(x,y,z) ((x & z) | (y & ~z))
439 #define H(x,y,z) (x ^ y ^ z)
440 #define I(x,y,z) (y ^ (x | ~z))
442 static inline afs_uint32
443 cshift (afs_uint32 x, unsigned int n)
445 return ((x << n) | (x >> (32 - n)));
448 #define DOIT(a,b,c,d,k,s,i,OP) \
449 a = b + cshift(a + OP(b,c,d) + X[k] + (i), s)
451 #define DO1(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,F)
452 #define DO2(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,G)
453 #define DO3(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,H)
454 #define DO4(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,I)
457 calc (struct afs_md5 *m, afs_uint32 *data)
459 afs_uint32 AA, BB, CC, DD;
468 DO1(A,B,C,D,0,7,0xd76aa478);
469 DO1(D,A,B,C,1,12,0xe8c7b756);
470 DO1(C,D,A,B,2,17,0x242070db);
471 DO1(B,C,D,A,3,22,0xc1bdceee);
473 DO1(A,B,C,D,4,7,0xf57c0faf);
474 DO1(D,A,B,C,5,12,0x4787c62a);
475 DO1(C,D,A,B,6,17,0xa8304613);
476 DO1(B,C,D,A,7,22,0xfd469501);
478 DO1(A,B,C,D,8,7,0x698098d8);
479 DO1(D,A,B,C,9,12,0x8b44f7af);
480 DO1(C,D,A,B,10,17,0xffff5bb1);
481 DO1(B,C,D,A,11,22,0x895cd7be);
483 DO1(A,B,C,D,12,7,0x6b901122);
484 DO1(D,A,B,C,13,12,0xfd987193);
485 DO1(C,D,A,B,14,17,0xa679438e);
486 DO1(B,C,D,A,15,22,0x49b40821);
490 DO2(A,B,C,D,1,5,0xf61e2562);
491 DO2(D,A,B,C,6,9,0xc040b340);
492 DO2(C,D,A,B,11,14,0x265e5a51);
493 DO2(B,C,D,A,0,20,0xe9b6c7aa);
495 DO2(A,B,C,D,5,5,0xd62f105d);
496 DO2(D,A,B,C,10,9,0x2441453);
497 DO2(C,D,A,B,15,14,0xd8a1e681);
498 DO2(B,C,D,A,4,20,0xe7d3fbc8);
500 DO2(A,B,C,D,9,5,0x21e1cde6);
501 DO2(D,A,B,C,14,9,0xc33707d6);
502 DO2(C,D,A,B,3,14,0xf4d50d87);
503 DO2(B,C,D,A,8,20,0x455a14ed);
505 DO2(A,B,C,D,13,5,0xa9e3e905);
506 DO2(D,A,B,C,2,9,0xfcefa3f8);
507 DO2(C,D,A,B,7,14,0x676f02d9);
508 DO2(B,C,D,A,12,20,0x8d2a4c8a);
512 DO3(A,B,C,D,5,4,0xfffa3942);
513 DO3(D,A,B,C,8,11,0x8771f681);
514 DO3(C,D,A,B,11,16,0x6d9d6122);
515 DO3(B,C,D,A,14,23,0xfde5380c);
517 DO3(A,B,C,D,1,4,0xa4beea44);
518 DO3(D,A,B,C,4,11,0x4bdecfa9);
519 DO3(C,D,A,B,7,16,0xf6bb4b60);
520 DO3(B,C,D,A,10,23,0xbebfbc70);
522 DO3(A,B,C,D,13,4,0x289b7ec6);
523 DO3(D,A,B,C,0,11,0xeaa127fa);
524 DO3(C,D,A,B,3,16,0xd4ef3085);
525 DO3(B,C,D,A,6,23,0x4881d05);
527 DO3(A,B,C,D,9,4,0xd9d4d039);
528 DO3(D,A,B,C,12,11,0xe6db99e5);
529 DO3(C,D,A,B,15,16,0x1fa27cf8);
530 DO3(B,C,D,A,2,23,0xc4ac5665);
534 DO4(A,B,C,D,0,6,0xf4292244);
535 DO4(D,A,B,C,7,10,0x432aff97);
536 DO4(C,D,A,B,14,15,0xab9423a7);
537 DO4(B,C,D,A,5,21,0xfc93a039);
539 DO4(A,B,C,D,12,6,0x655b59c3);
540 DO4(D,A,B,C,3,10,0x8f0ccc92);
541 DO4(C,D,A,B,10,15,0xffeff47d);
542 DO4(B,C,D,A,1,21,0x85845dd1);
544 DO4(A,B,C,D,8,6,0x6fa87e4f);
545 DO4(D,A,B,C,15,10,0xfe2ce6e0);
546 DO4(C,D,A,B,6,15,0xa3014314);
547 DO4(B,C,D,A,13,21,0x4e0811a1);
549 DO4(A,B,C,D,4,6,0xf7537e82);
550 DO4(D,A,B,C,11,10,0xbd3af235);
551 DO4(C,D,A,B,2,15,0x2ad7d2bb);
552 DO4(B,C,D,A,9,21,0xeb86d391);
561 * From `Performance analysis of MD5' by Joseph D. Touch <touch@isi.edu>
564 #if defined(AFSBIG_ENDIAN)
565 static inline afs_uint32
566 swap_u_int32_t (afs_uint32 t)
568 afs_uint32 temp1, temp2;
570 temp1 = cshift(t, 16);
575 return temp1 | temp2;
585 AFS_MD5_Update (struct afs_md5 *m, const void *v, size_t len)
587 const unsigned char *p = v;
588 size_t old_sz = m->sz[0];
592 if (m->sz[0] < old_sz)
594 offset = (old_sz / 8) % 64;
596 size_t l = MIN(len, 64 - offset);
597 memcpy(m->save + offset, p, l);
602 #if defined(AFSBIG_ENDIAN)
605 struct x32 *us = (struct x32*)m->save;
606 for(i = 0; i < 8; i++){
607 temp[2*i+0] = swap_u_int32_t(us[i].a);
608 temp[2*i+1] = swap_u_int32_t(us[i].b);
612 calc(m, (afs_uint32*)m->save);
620 AFS_MD5_Final (void *res, struct afs_md5 *m)
622 unsigned char zeros[72];
623 unsigned offset = (m->sz[0] / 8) % 64;
624 unsigned int dstart = (120 - offset - 1) % 64 + 1;
627 memset (zeros + 1, 0, sizeof(zeros) - 1);
628 zeros[dstart+0] = (m->sz[0] >> 0) & 0xff;
629 zeros[dstart+1] = (m->sz[0] >> 8) & 0xff;
630 zeros[dstart+2] = (m->sz[0] >> 16) & 0xff;
631 zeros[dstart+3] = (m->sz[0] >> 24) & 0xff;
632 zeros[dstart+4] = (m->sz[1] >> 0) & 0xff;
633 zeros[dstart+5] = (m->sz[1] >> 8) & 0xff;
634 zeros[dstart+6] = (m->sz[1] >> 16) & 0xff;
635 zeros[dstart+7] = (m->sz[1] >> 24) & 0xff;
636 AFS_MD5_Update (m, zeros, dstart + 8);
639 unsigned char *r = (unsigned char *)res;
641 for (i = 0; i < 4; ++i) {
642 r[4*i] = m->counter[i] & 0xFF;
643 r[4*i+1] = (m->counter[i] >> 8) & 0xFF;
644 r[4*i+2] = (m->counter[i] >> 16) & 0xFF;
645 r[4*i+3] = (m->counter[i] >> 24) & 0xFF;
650 afs_int32 afs_calc_inum (afs_int32 volume, afs_int32 vnode)
658 AFS_MD5_Update(&ct, &volume, 4);
659 AFS_MD5_Update(&ct, &vnode, 4);
660 AFS_MD5_Final(digest, &ct);
661 memcpy(&ino, digest, sizeof(ino_t));
663 ino = (volume << 16) + vnode;
664 ino &= 0x7fffffff; /* Assumes 32 bit ino_t ..... */
671 afs_int32 afs_calc_inum (afs_int32 volume, afs_int32 vnode)
673 return (volume << 16) + vnode;