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 */
200 /* run everywhere, checking locks */
206 afs_warn("Looking for locked data structures.\n");
207 afs_warn("conn %lx, volume %lx, user %lx, cell %lx, server %lx\n", &afs_xconn,
208 &afs_xvolume, &afs_xuser, &afs_xcell, &afs_xserver);
210 register struct vcache *tvc;
211 AFS_STATCNT(afs_CheckLocks);
213 for (i = 0; i < VCSIZE; i++) {
214 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
215 if (tvc->states & CVInit) continue;
217 if (VREFCOUNT(tvc) > 1)
218 #else /* AFS_OSF_ENV */
219 #ifdef AFS_DARWIN80_ENV
220 if (vnode_isinuse(AFSTOV(tvc), 0))
225 afs_warn("Stat cache entry at %x is held\n", tvc);
226 if (CheckLock(&tvc->lock))
227 afs_warn("Stat entry at %x is locked\n", tvc);
232 register struct dcache *tdc;
233 for (i = 0; i < afs_cacheFiles; i++) {
234 tdc = afs_indexTable[i];
237 afs_warn("Disk entry %d at %x is held\n", i, tdc);
239 if (afs_indexFlags[i] & IFDataMod)
240 afs_warn("Disk entry %d at %x has IFDataMod flag set.\n", i,
248 for (i = 0; i < NSERVERS; i++) {
249 for (ts = afs_servers[i]; ts; ts = ts->next) {
250 if (ts->flags & SRVR_ISDOWN)
251 printf("Server entry %lx is marked down\n", (unsigned long)ts);
252 for (sa = ts->addr; sa; sa = sa->next_sa) {
253 for (tc = sa->conns; tc; tc = tc->next) {
255 afs_warn("conn at %x (server %x) is held\n", tc,
264 for (i = 0; i < NVOLS; i++) {
265 for (tv = afs_volumes[i]; tv; tv = tv->next) {
266 if (CheckLock(&tv->lock))
267 afs_warn("volume at %x is locked\n", tv);
269 afs_warn("volume at %x is held\n", tv);
276 for (i = 0; i < NUSERS; i++) {
277 for (tu = afs_users[i]; tu; tu = tu->next) {
279 printf("user at %lx is held\n", (unsigned long)tu);
290 AFS_STATCNT(afs_noop);
301 AFS_STATCNT(afs_badop);
302 osi_Panic("afs bad vnode op");
307 * afs_data_pointer_to_int32() - returns least significant afs_int32 of the
308 * given data pointer, without triggering "cast truncates pointer"
309 * warnings. We use this where we explicitly don't care whether a
310 * pointer is truncated -- it loses information where a pointer is
311 * larger than an afs_int32.
315 afs_data_pointer_to_int32(const void *p)
318 afs_int32 i32[sizeof(void *) / sizeof(afs_int32)];
322 int i32_sub; /* subscript of least significant afs_int32 in ip.i32[] */
327 /* used to determine the byte order of the system */
330 char c[sizeof(int) / sizeof(char)];
336 /* little-endian system */
339 /* big-endian system */
340 i32_sub = (sizeof ip.i32 / sizeof ip.i32[0]) - 1;
345 return ip.i32[i32_sub];
348 #ifdef AFS_LINUX20_ENV
352 afs_int32 counter[4];
353 unsigned char save[64];
356 static void AFS_MD5_Init (struct afs_md5 *m);
357 static void AFS_MD5_Update (struct afs_md5 *m, const void *p, size_t len);
358 static void AFS_MD5_Final (void *res, struct afs_md5 *m); /* u_int32 res[4] */
360 #define A m->counter[0]
361 #define B m->counter[1]
362 #define C m->counter[2]
363 #define D m->counter[3]
367 AFS_MD5_Init (struct afs_md5 *m)
377 #define F(x,y,z) ((x & y) | (~x & z))
378 #define G(x,y,z) ((x & z) | (y & ~z))
379 #define H(x,y,z) (x ^ y ^ z)
380 #define I(x,y,z) (y ^ (x | ~z))
382 static inline afs_uint32
383 cshift (afs_uint32 x, unsigned int n)
385 return ((x << n) | (x >> (32 - n)));
388 #define DOIT(a,b,c,d,k,s,i,OP) \
389 a = b + cshift(a + OP(b,c,d) + X[k] + (i), s)
391 #define DO1(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,F)
392 #define DO2(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,G)
393 #define DO3(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,H)
394 #define DO4(a,b,c,d,k,s,i) DOIT(a,b,c,d,k,s,i,I)
397 calc (struct afs_md5 *m, afs_uint32 *data)
399 afs_uint32 AA, BB, CC, DD;
408 DO1(A,B,C,D,0,7,0xd76aa478);
409 DO1(D,A,B,C,1,12,0xe8c7b756);
410 DO1(C,D,A,B,2,17,0x242070db);
411 DO1(B,C,D,A,3,22,0xc1bdceee);
413 DO1(A,B,C,D,4,7,0xf57c0faf);
414 DO1(D,A,B,C,5,12,0x4787c62a);
415 DO1(C,D,A,B,6,17,0xa8304613);
416 DO1(B,C,D,A,7,22,0xfd469501);
418 DO1(A,B,C,D,8,7,0x698098d8);
419 DO1(D,A,B,C,9,12,0x8b44f7af);
420 DO1(C,D,A,B,10,17,0xffff5bb1);
421 DO1(B,C,D,A,11,22,0x895cd7be);
423 DO1(A,B,C,D,12,7,0x6b901122);
424 DO1(D,A,B,C,13,12,0xfd987193);
425 DO1(C,D,A,B,14,17,0xa679438e);
426 DO1(B,C,D,A,15,22,0x49b40821);
430 DO2(A,B,C,D,1,5,0xf61e2562);
431 DO2(D,A,B,C,6,9,0xc040b340);
432 DO2(C,D,A,B,11,14,0x265e5a51);
433 DO2(B,C,D,A,0,20,0xe9b6c7aa);
435 DO2(A,B,C,D,5,5,0xd62f105d);
436 DO2(D,A,B,C,10,9,0x2441453);
437 DO2(C,D,A,B,15,14,0xd8a1e681);
438 DO2(B,C,D,A,4,20,0xe7d3fbc8);
440 DO2(A,B,C,D,9,5,0x21e1cde6);
441 DO2(D,A,B,C,14,9,0xc33707d6);
442 DO2(C,D,A,B,3,14,0xf4d50d87);
443 DO2(B,C,D,A,8,20,0x455a14ed);
445 DO2(A,B,C,D,13,5,0xa9e3e905);
446 DO2(D,A,B,C,2,9,0xfcefa3f8);
447 DO2(C,D,A,B,7,14,0x676f02d9);
448 DO2(B,C,D,A,12,20,0x8d2a4c8a);
452 DO3(A,B,C,D,5,4,0xfffa3942);
453 DO3(D,A,B,C,8,11,0x8771f681);
454 DO3(C,D,A,B,11,16,0x6d9d6122);
455 DO3(B,C,D,A,14,23,0xfde5380c);
457 DO3(A,B,C,D,1,4,0xa4beea44);
458 DO3(D,A,B,C,4,11,0x4bdecfa9);
459 DO3(C,D,A,B,7,16,0xf6bb4b60);
460 DO3(B,C,D,A,10,23,0xbebfbc70);
462 DO3(A,B,C,D,13,4,0x289b7ec6);
463 DO3(D,A,B,C,0,11,0xeaa127fa);
464 DO3(C,D,A,B,3,16,0xd4ef3085);
465 DO3(B,C,D,A,6,23,0x4881d05);
467 DO3(A,B,C,D,9,4,0xd9d4d039);
468 DO3(D,A,B,C,12,11,0xe6db99e5);
469 DO3(C,D,A,B,15,16,0x1fa27cf8);
470 DO3(B,C,D,A,2,23,0xc4ac5665);
474 DO4(A,B,C,D,0,6,0xf4292244);
475 DO4(D,A,B,C,7,10,0x432aff97);
476 DO4(C,D,A,B,14,15,0xab9423a7);
477 DO4(B,C,D,A,5,21,0xfc93a039);
479 DO4(A,B,C,D,12,6,0x655b59c3);
480 DO4(D,A,B,C,3,10,0x8f0ccc92);
481 DO4(C,D,A,B,10,15,0xffeff47d);
482 DO4(B,C,D,A,1,21,0x85845dd1);
484 DO4(A,B,C,D,8,6,0x6fa87e4f);
485 DO4(D,A,B,C,15,10,0xfe2ce6e0);
486 DO4(C,D,A,B,6,15,0xa3014314);
487 DO4(B,C,D,A,13,21,0x4e0811a1);
489 DO4(A,B,C,D,4,6,0xf7537e82);
490 DO4(D,A,B,C,11,10,0xbd3af235);
491 DO4(C,D,A,B,2,15,0x2ad7d2bb);
492 DO4(B,C,D,A,9,21,0xeb86d391);
501 * From `Performance analysis of MD5' by Joseph D. Touch <touch@isi.edu>
504 #if defined(WORDS_BIGENDIAN)
505 static inline afs_uint32
506 swap_u_int32_t (afs_uint32 t)
508 afs_uint32 temp1, temp2;
510 temp1 = cshift(t, 16);
515 return temp1 | temp2;
525 AFS_MD5_Update (struct afs_md5 *m, const void *v, size_t len)
527 const unsigned char *p = v;
528 size_t old_sz = m->sz[0];
532 if (m->sz[0] < old_sz)
534 offset = (old_sz / 8) % 64;
536 size_t l = MIN(len, 64 - offset);
537 memcpy(m->save + offset, p, l);
542 #if defined(WORDS_BIGENDIAN)
545 struct x32 *us = (struct x32*)m->save;
546 for(i = 0; i < 8; i++){
547 temp[2*i+0] = swap_u_int32_t(us[i].a);
548 temp[2*i+1] = swap_u_int32_t(us[i].b);
552 calc(m, (afs_uint32*)m->save);
560 AFS_MD5_Final (void *res, struct afs_md5 *m)
562 unsigned char zeros[72];
563 unsigned offset = (m->sz[0] / 8) % 64;
564 unsigned int dstart = (120 - offset - 1) % 64 + 1;
567 memset (zeros + 1, 0, sizeof(zeros) - 1);
568 zeros[dstart+0] = (m->sz[0] >> 0) & 0xff;
569 zeros[dstart+1] = (m->sz[0] >> 8) & 0xff;
570 zeros[dstart+2] = (m->sz[0] >> 16) & 0xff;
571 zeros[dstart+3] = (m->sz[0] >> 24) & 0xff;
572 zeros[dstart+4] = (m->sz[1] >> 0) & 0xff;
573 zeros[dstart+5] = (m->sz[1] >> 8) & 0xff;
574 zeros[dstart+6] = (m->sz[1] >> 16) & 0xff;
575 zeros[dstart+7] = (m->sz[1] >> 24) & 0xff;
576 AFS_MD5_Update (m, zeros, dstart + 8);
579 unsigned char *r = (unsigned char *)res;
581 for (i = 0; i < 4; ++i) {
582 r[4*i] = m->counter[i] & 0xFF;
583 r[4*i+1] = (m->counter[i] >> 8) & 0xFF;
584 r[4*i+2] = (m->counter[i] >> 16) & 0xFF;
585 r[4*i+3] = (m->counter[i] >> 24) & 0xFF;
590 afs_int32 afs_calc_inum (afs_int32 volume, afs_int32 vnode)
592 afs_int32 ino, vno = vnode;
598 AFS_MD5_Update(&ct, &volume, 4);
599 AFS_MD5_Update(&ct, &vnode, 4);
600 AFS_MD5_Final(digest, &ct);
601 memcpy(&ino, digest, sizeof(ino_t));
602 ino ^= (ino ^ vno) & 1;
604 ino = (volume << 16) + vnode;
606 ino &= 0x7fffffff; /* Assumes 32 bit ino_t ..... */
612 afs_int32 afs_calc_inum (afs_int32 volume, afs_int32 vnode)
614 return (volume << 16) + vnode;