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"
20 #include "afs/sysincludes.h" /* Standard vendor system headers */
23 #if !defined(AFS_LINUX20_ENV)
26 #include <netinet/in.h>
29 #include "h/hashing.h"
31 #if !defined(AFS_HPUX110_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_DARWIN60_ENV)
32 #include <netinet/in_var.h>
33 #endif /* ! AFS_HPUX110_ENV */
34 #endif /* !defined(UKERNEL) */
36 #include "afsincludes.h" /* Afs-based standard headers */
37 #include "afs/afs_stats.h" /* afs statistics */
39 #if defined(AFS_SUN56_ENV)
41 #include <inet/common.h>
42 #if defined(AFS_SUN58_ENV)
43 #include <netinet/ip6.h>
48 #if defined(AFS_AIX_ENV)
49 #include <sys/fp_io.h>
52 afs_int32 afs_new_inum = 0;
56 afs_cv2string(char *ttp, afs_uint32 aval)
58 register char *tp = ttp;
62 AFS_STATCNT(afs_cv2string);
78 #ifndef afs_strcasecmp
80 afs_strcasecmp(char *s1, char *s2)
87 if (c1 >= 'A' && c1 <= 'Z')
89 if (c2 >= 'A' && c2 <= 'Z')
101 afs_strcat(char *s1, char *s2)
108 while ((*s1++ = *s2++));
113 #ifdef AFS_OBSD34_ENV
115 afs_strcpy(char *s1, char *s2)
120 while ((*s1++ = *s2++) != '\0');
127 afs_strchr(char *s, int c)
139 afs_strrchr(char *s, int c)
158 n = (char *)afs_osi_Alloc(cc);
166 print_internet_address(char *preamble, struct srvAddr *sa, char *postamble,
169 register struct server *aserver = sa->server;
173 AFS_STATCNT(print_internet_address);
174 address = ntohl(sa->sa_ip);
175 if (aserver->flags & SRVR_MULTIHOMED) {
176 if (flag == 1) { /* server down mesg */
177 if (!(aserver->flags & SRVR_ISDOWN))
179 " (multi-homed address; other same-host interfaces maybe up)\n";
181 ptr = " (all multi-homed ip addresses down for the server)\n";
182 } else if (flag == 2) { /* server up mesg */
184 " (multi-homed address; other same-host interfaces may still be down)\n";
187 afs_warn("%s%d.%d.%d.%d in cell %s%s%s", preamble, (address >> 24),
188 (address >> 16) & 0xff, (address >> 8) & 0xff, (address) & 0xff,
189 aserver->cell->cellName, postamble, ptr);
190 afs_warnuser("%s%d.%d.%d.%d in cell %s%s%s", preamble, (address >> 24),
191 (address >> 16) & 0xff, (address >> 8) & 0xff,
192 (address) & 0xff, aserver->cell->cellName, postamble, ptr);
194 } /*print_internet_address */
198 /* run everywhere, checking locks */
204 afs_warn("Looking for locked data structures.\n");
205 afs_warn("conn %lx, volume %lx, user %lx, cell %lx, server %lx\n", &afs_xconn,
206 &afs_xvolume, &afs_xuser, &afs_xcell, &afs_xserver);
208 register struct vcache *tvc;
209 AFS_STATCNT(afs_CheckLocks);
211 for (i = 0; i < VCSIZE; i++) {
212 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
213 if (tvc->f.states & CVInit) continue;
215 if (VREFCOUNT(tvc) > 1)
216 #else /* AFS_OSF_ENV */
217 #ifdef AFS_DARWIN80_ENV
218 if (vnode_isinuse(AFSTOV(tvc), 0))
223 afs_warn("Stat cache entry at %x is held\n", tvc);
224 if (CheckLock(&tvc->lock))
225 afs_warn("Stat entry at %x is locked\n", tvc);
230 register struct dcache *tdc;
231 for (i = 0; i < afs_cacheFiles; i++) {
232 tdc = afs_indexTable[i];
235 afs_warn("Disk entry %d at %x is held\n", i, tdc);
237 if (afs_indexFlags[i] & IFDataMod)
238 afs_warn("Disk entry %d at %x has IFDataMod flag set.\n", i,
246 for (i = 0; i < NSERVERS; i++) {
247 for (ts = afs_servers[i]; ts; ts = ts->next) {
248 if (ts->flags & SRVR_ISDOWN)
249 printf("Server entry %lx is marked down\n", (unsigned long)ts);
250 for (sa = ts->addr; sa; sa = sa->next_sa) {
251 for (tc = sa->conns; tc; tc = tc->next) {
253 afs_warn("conn at %x (server %x) is held\n", tc,
262 for (i = 0; i < NVOLS; i++) {
263 for (tv = afs_volumes[i]; tv; tv = tv->next) {
264 if (CheckLock(&tv->lock))
265 afs_warn("volume at %x is locked\n", tv);
267 afs_warn("volume at %x is held\n", tv);
274 for (i = 0; i < NUSERS; i++) {
275 for (tu = afs_users[i]; tu; tu = tu->next) {
277 printf("user at %lx is held\n", (unsigned long)tu);
288 AFS_STATCNT(afs_noop);
299 AFS_STATCNT(afs_badop);
300 osi_Panic("afs bad vnode op");
305 * afs_data_pointer_to_int32() - returns least significant afs_int32 of the
306 * given data pointer, without triggering "cast truncates pointer"
307 * warnings. We use this where we explicitly don't care whether a
308 * pointer is truncated -- it loses information where a pointer is
309 * larger than an afs_int32.
313 afs_data_pointer_to_int32(const void *p)
316 afs_int32 i32[sizeof(void *) / sizeof(afs_int32)];
320 int i32_sub; /* subscript of least significant afs_int32 in ip.i32[] */
325 /* used to determine the byte order of the system */
328 char c[sizeof(int) / sizeof(char)];
334 /* little-endian system */
337 /* big-endian system */
338 i32_sub = (sizeof ip.i32 / sizeof ip.i32[0]) - 1;
343 return ip.i32[i32_sub];
346 #ifdef AFS_LINUX20_ENV
350 afs_int32 counter[4];
351 unsigned char save[64];
354 static void AFS_MD5_Init (struct afs_md5 *m);
355 static void AFS_MD5_Update (struct afs_md5 *m, const void *p, size_t len);
356 static void AFS_MD5_Final (void *res, struct afs_md5 *m); /* u_int32 res[4] */
358 #define A m->counter[0]
359 #define B m->counter[1]
360 #define C m->counter[2]
361 #define D m->counter[3]
365 AFS_MD5_Init (struct afs_md5 *m)
375 #define F(x,y,z) ((x & y) | (~x & z))
376 #define G(x,y,z) ((x & z) | (y & ~z))
377 #define H(x,y,z) (x ^ y ^ z)
378 #define I(x,y,z) (y ^ (x | ~z))
380 static inline afs_uint32
381 cshift (afs_uint32 x, unsigned int n)
383 return ((x << n) | (x >> (32 - n)));
386 #define DOIT(a,b,c,d,k,s,i,OP) \
387 a = b + cshift(a + OP(b,c,d) + X[k] + (i), s)
389 #define DO1(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,F)
390 #define DO2(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,G)
391 #define DO3(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,H)
392 #define DO4(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,I)
395 calc (struct afs_md5 *m, afs_uint32 *data)
397 afs_uint32 AA, BB, CC, DD;
406 DO1(A,B,C,D,0,7,0xd76aa478);
407 DO1(D,A,B,C,1,12,0xe8c7b756);
408 DO1(C,D,A,B,2,17,0x242070db);
409 DO1(B,C,D,A,3,22,0xc1bdceee);
411 DO1(A,B,C,D,4,7,0xf57c0faf);
412 DO1(D,A,B,C,5,12,0x4787c62a);
413 DO1(C,D,A,B,6,17,0xa8304613);
414 DO1(B,C,D,A,7,22,0xfd469501);
416 DO1(A,B,C,D,8,7,0x698098d8);
417 DO1(D,A,B,C,9,12,0x8b44f7af);
418 DO1(C,D,A,B,10,17,0xffff5bb1);
419 DO1(B,C,D,A,11,22,0x895cd7be);
421 DO1(A,B,C,D,12,7,0x6b901122);
422 DO1(D,A,B,C,13,12,0xfd987193);
423 DO1(C,D,A,B,14,17,0xa679438e);
424 DO1(B,C,D,A,15,22,0x49b40821);
428 DO2(A,B,C,D,1,5,0xf61e2562);
429 DO2(D,A,B,C,6,9,0xc040b340);
430 DO2(C,D,A,B,11,14,0x265e5a51);
431 DO2(B,C,D,A,0,20,0xe9b6c7aa);
433 DO2(A,B,C,D,5,5,0xd62f105d);
434 DO2(D,A,B,C,10,9,0x2441453);
435 DO2(C,D,A,B,15,14,0xd8a1e681);
436 DO2(B,C,D,A,4,20,0xe7d3fbc8);
438 DO2(A,B,C,D,9,5,0x21e1cde6);
439 DO2(D,A,B,C,14,9,0xc33707d6);
440 DO2(C,D,A,B,3,14,0xf4d50d87);
441 DO2(B,C,D,A,8,20,0x455a14ed);
443 DO2(A,B,C,D,13,5,0xa9e3e905);
444 DO2(D,A,B,C,2,9,0xfcefa3f8);
445 DO2(C,D,A,B,7,14,0x676f02d9);
446 DO2(B,C,D,A,12,20,0x8d2a4c8a);
450 DO3(A,B,C,D,5,4,0xfffa3942);
451 DO3(D,A,B,C,8,11,0x8771f681);
452 DO3(C,D,A,B,11,16,0x6d9d6122);
453 DO3(B,C,D,A,14,23,0xfde5380c);
455 DO3(A,B,C,D,1,4,0xa4beea44);
456 DO3(D,A,B,C,4,11,0x4bdecfa9);
457 DO3(C,D,A,B,7,16,0xf6bb4b60);
458 DO3(B,C,D,A,10,23,0xbebfbc70);
460 DO3(A,B,C,D,13,4,0x289b7ec6);
461 DO3(D,A,B,C,0,11,0xeaa127fa);
462 DO3(C,D,A,B,3,16,0xd4ef3085);
463 DO3(B,C,D,A,6,23,0x4881d05);
465 DO3(A,B,C,D,9,4,0xd9d4d039);
466 DO3(D,A,B,C,12,11,0xe6db99e5);
467 DO3(C,D,A,B,15,16,0x1fa27cf8);
468 DO3(B,C,D,A,2,23,0xc4ac5665);
472 DO4(A,B,C,D,0,6,0xf4292244);
473 DO4(D,A,B,C,7,10,0x432aff97);
474 DO4(C,D,A,B,14,15,0xab9423a7);
475 DO4(B,C,D,A,5,21,0xfc93a039);
477 DO4(A,B,C,D,12,6,0x655b59c3);
478 DO4(D,A,B,C,3,10,0x8f0ccc92);
479 DO4(C,D,A,B,10,15,0xffeff47d);
480 DO4(B,C,D,A,1,21,0x85845dd1);
482 DO4(A,B,C,D,8,6,0x6fa87e4f);
483 DO4(D,A,B,C,15,10,0xfe2ce6e0);
484 DO4(C,D,A,B,6,15,0xa3014314);
485 DO4(B,C,D,A,13,21,0x4e0811a1);
487 DO4(A,B,C,D,4,6,0xf7537e82);
488 DO4(D,A,B,C,11,10,0xbd3af235);
489 DO4(C,D,A,B,2,15,0x2ad7d2bb);
490 DO4(B,C,D,A,9,21,0xeb86d391);
499 * From `Performance analysis of MD5' by Joseph D. Touch <touch@isi.edu>
502 #if defined(WORDS_BIGENDIAN)
503 static inline afs_uint32
504 swap_u_int32_t (afs_uint32 t)
506 afs_uint32 temp1, temp2;
508 temp1 = cshift(t, 16);
513 return temp1 | temp2;
523 AFS_MD5_Update (struct afs_md5 *m, const void *v, size_t len)
525 const unsigned char *p = v;
526 size_t old_sz = m->sz[0];
530 if (m->sz[0] < old_sz)
532 offset = (old_sz / 8) % 64;
534 size_t l = MIN(len, 64 - offset);
535 memcpy(m->save + offset, p, l);
540 #if defined(WORDS_BIGENDIAN)
543 struct x32 *us = (struct x32*)m->save;
544 for(i = 0; i < 8; i++){
545 temp[2*i+0] = swap_u_int32_t(us[i].a);
546 temp[2*i+1] = swap_u_int32_t(us[i].b);
550 calc(m, (afs_uint32*)m->save);
558 AFS_MD5_Final (void *res, struct afs_md5 *m)
560 unsigned char zeros[72];
561 unsigned offset = (m->sz[0] / 8) % 64;
562 unsigned int dstart = (120 - offset - 1) % 64 + 1;
565 memset (zeros + 1, 0, sizeof(zeros) - 1);
566 zeros[dstart+0] = (m->sz[0] >> 0) & 0xff;
567 zeros[dstart+1] = (m->sz[0] >> 8) & 0xff;
568 zeros[dstart+2] = (m->sz[0] >> 16) & 0xff;
569 zeros[dstart+3] = (m->sz[0] >> 24) & 0xff;
570 zeros[dstart+4] = (m->sz[1] >> 0) & 0xff;
571 zeros[dstart+5] = (m->sz[1] >> 8) & 0xff;
572 zeros[dstart+6] = (m->sz[1] >> 16) & 0xff;
573 zeros[dstart+7] = (m->sz[1] >> 24) & 0xff;
574 AFS_MD5_Update (m, zeros, dstart + 8);
577 unsigned char *r = (unsigned char *)res;
579 for (i = 0; i < 4; ++i) {
580 r[4*i] = m->counter[i] & 0xFF;
581 r[4*i+1] = (m->counter[i] >> 8) & 0xFF;
582 r[4*i+2] = (m->counter[i] >> 16) & 0xFF;
583 r[4*i+3] = (m->counter[i] >> 24) & 0xFF;
588 afs_int32 afs_calc_inum (afs_int32 volume, afs_int32 vnode)
590 afs_int32 ino, vno = vnode;
596 AFS_MD5_Update(&ct, &volume, 4);
597 AFS_MD5_Update(&ct, &vnode, 4);
598 AFS_MD5_Final(digest, &ct);
599 memcpy(&ino, digest, sizeof(ino_t));
600 ino ^= (ino ^ vno) & 1;
602 ino = (volume << 16) + vnode;
604 ino &= 0x7fffffff; /* Assumes 32 bit ino_t ..... */
610 afs_int32 afs_calc_inum (afs_int32 volume, afs_int32 vnode)
612 return (volume << 16) + vnode;