linux-2626-updates-20080612
[openafs.git] / src / afs / afs_util.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 /*
11  * afs_util.c - miscellaneous AFS client utility functions
12  *
13  * Implements:
14  */
15 #include <afsconfig.h>
16 #include "afs/param.h"
17
18 RCSID
19     ("$Header$");
20
21 #include "afs/stds.h"
22 #include "afs/sysincludes.h"    /* Standard vendor system headers */
23
24 #if !defined(UKERNEL)
25 #if !defined(AFS_LINUX20_ENV)
26 #include <net/if.h>
27 #endif
28 #include <netinet/in.h>
29
30 #ifdef AFS_SGI62_ENV
31 #include "h/hashing.h"
32 #endif
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) */
37
38 #include "afsincludes.h"        /* Afs-based standard headers */
39 #include "afs/afs_stats.h"      /* afs statistics */
40
41 #if     defined(AFS_SUN56_ENV)
42 #include <inet/led.h>
43 #include <inet/common.h>
44 #if     defined(AFS_SUN58_ENV)
45 #include <netinet/ip6.h>
46 #endif
47 #include <inet/ip.h>
48 #endif
49
50 #if     defined(AFS_AIX_ENV)
51 #include <sys/fp_io.h>
52 #endif
53
54 afs_int32 afs_new_inum = 0;
55
56 #ifndef afs_cv2string
57 char *
58 afs_cv2string(char *ttp, afs_uint32 aval)
59 {
60     register char *tp = ttp;
61     register int i;
62     int any;
63
64     AFS_STATCNT(afs_cv2string);
65     any = 0;
66     *(--tp) = 0;
67     while (aval != 0) {
68         i = aval % 10;
69         *(--tp) = '0' + i;
70         aval /= 10;
71         any = 1;
72     }
73     if (!any)
74         *(--tp) = '0';
75     return tp;
76
77 }                               /*afs_cv2string */
78 #endif
79
80 #ifndef afs_strcasecmp
81 int
82 afs_strcasecmp(char *s1, char *s2)
83 {
84     while (*s1 && *s2) {
85         char c1, c2;
86
87         c1 = *s1++;
88         c2 = *s2++;
89         if (c1 >= 'A' && c1 <= 'Z')
90             c1 += 0x20;
91         if (c2 >= 'A' && c2 <= 'Z')
92             c2 += 0x20;
93         if (c1 != c2)
94             return c1 - c2;
95     }
96
97     return *s1 - *s2;
98 }
99 #endif
100
101 #ifndef afs_strcat
102 char *
103 afs_strcat(char *s1, char *s2)
104 {
105     char *os1;
106
107     os1 = s1;
108     while (*s1++);
109     --s1;
110     while ((*s1++ = *s2++));
111     return (os1);
112 }
113 #endif
114
115 #ifdef AFS_OBSD34_ENV
116 char *
117 afs_strcpy(char *s1, char *s2)
118 {
119     char *os1;
120
121     os1 = s1;
122     while ((*s1++ = *s2++) != '\0');
123     return os1;
124 }
125 #endif
126
127 #ifndef afs_strchr
128 char *
129 afs_strchr(char *s, int c)
130 {
131     char *p;
132
133     for (p = s; *p; p++)
134         if (*p == c)
135             return p;
136     return NULL;
137 }
138 #endif
139 #ifndef afs_strrchr
140 char *
141 afs_strrchr(char *s, int c)
142 {
143     char *p = NULL;
144     
145     do {
146         if (*s == c)
147             p = (char*) s;
148     } while (*s++);
149     return p;
150 }
151 #endif
152
153 char *
154 afs_strdup(char *s)
155 {
156     char *n;
157     int cc;
158
159     cc = strlen(s) + 1;
160     n = (char *)afs_osi_Alloc(cc);
161     if (n)
162         memcpy(n, s, cc);
163
164     return n;
165 }
166
167 void
168 print_internet_address(char *preamble, struct srvAddr *sa, char *postamble,
169                        int flag)
170 {
171     register struct server *aserver = sa->server;
172     char *ptr = "\n";
173     afs_uint32 address;
174
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))
180                 ptr =
181                     " (multi-homed address; other same-host interfaces maybe up)\n";
182             else
183                 ptr = " (all multi-homed ip addresses down for the server)\n";
184         } else if (flag == 2) { /* server up mesg */
185             ptr =
186                 " (multi-homed address; other same-host interfaces may still be down)\n";
187         }
188     }
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);
195
196 }                               /*print_internet_address */
197
198
199
200 /* * * * * * *
201  * this code badly needs to be cleaned up...  too many ugly ifdefs.
202  * XXX
203  */
204 #if 0
205 void
206 afs_warn(char *a, long b, long c, long d, long e, long f, long g, long h,
207          long i, long j)
208 #else
209 void
210 afs_warn(a, b, c, d, e, f, g, h, i, j)
211      char *a;
212 #if defined( AFS_USE_VOID_PTR)
213      void *b, *c, *d, *e, *f, *g, *h, *i, *j;
214 #else
215      long b, c, d, e, f, g, h, i, j;
216 #endif
217 #endif
218 {
219     AFS_STATCNT(afs_warn);
220
221     if (afs_showflags & GAGCONSOLE) {
222 #if defined(AFS_AIX_ENV)
223         struct file *fd;
224
225         /* cf. console_printf() in oncplus/kernext/nfs/serv/shared.c */
226         if (fp_open
227             ("/dev/console", O_WRONLY | O_NOCTTY | O_NDELAY, 0666, 0, FP_SYS,
228              &fd) == 0) {
229             char buf[1024];
230             ssize_t len;
231             ssize_t count;
232
233             sprintf(buf, a, b, c, d, e, f, g, h, i, j);
234             len = strlen(buf);
235             fp_write(fd, buf, len, 0, UIO_SYSSPACE, &count);
236             fp_close(fd);
237         }
238 #else
239         printf(a, b, c, d, e, f, g, h, i, j);
240 #endif
241     }
242 }
243
244 #if 0
245 void
246 afs_warnuser(char *a, long b, long c, long d, long e, long f, long g, long h,
247              long i, long j)
248 #else
249 void
250 afs_warnuser(a, b, c, d, e, f, g, h, i, j)
251      char *a;
252      long b, c, d, e, f, g, h, i, j;
253 #endif
254 {
255     AFS_STATCNT(afs_warnuser);
256     if (afs_showflags & GAGUSER) {
257 #ifdef AFS_GLOBAL_SUNLOCK
258         int haveGlock = ISAFS_GLOCK();
259         if (haveGlock)
260             AFS_GUNLOCK();
261 #endif /* AFS_GLOBAL_SUNLOCK */
262
263         uprintf(a, b, c, d, e, f, g, h, i, j);
264
265 #ifdef AFS_GLOBAL_SUNLOCK
266         if (haveGlock)
267             AFS_GLOCK();
268 #endif /* AFS_GLOBAL_SUNLOCK */
269     }
270 }
271
272
273 /* run everywhere, checking locks */
274 void
275 afs_CheckLocks(void)
276 {
277     register int i;
278
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);
282     {
283         register struct vcache *tvc;
284         AFS_STATCNT(afs_CheckLocks);
285
286         for (i = 0; i < VCSIZE; i++) {
287             for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
288                 if (tvc->states & CVInit) continue;
289 #ifdef  AFS_OSF_ENV
290                 if (VREFCOUNT(tvc) > 1)
291 #else /* AFS_OSF_ENV */
292 #ifdef AFS_DARWIN80_ENV
293                 if (vnode_isinuse(AFSTOV(tvc), 0))
294 #else
295                 if (VREFCOUNT(tvc))
296 #endif
297 #endif
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);
301             }
302         }
303     }
304     {
305         register struct dcache *tdc;
306         for (i = 0; i < afs_cacheFiles; i++) {
307             tdc = afs_indexTable[i];
308             if (tdc) {
309                 if (tdc->refCount)
310                     afs_warn("Disk entry %d at %x is held\n", i, tdc);
311             }
312             if (afs_indexFlags[i] & IFDataMod)
313                 afs_warn("Disk entry %d at %x has IFDataMod flag set.\n", i,
314                          tdc);
315         }
316     }
317     {
318         struct srvAddr *sa;
319         struct server *ts;
320         struct conn *tc;
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) {
327                         if (tc->refCount)
328                             afs_warn("conn at %x (server %x) is held\n", tc,
329                                      sa->sa_ip);
330                     }
331                 }
332             }
333         }
334     }
335     {
336         struct volume *tv;
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);
341                 if (tv->refCount)
342                     afs_warn("volume at %x is held\n", tv);
343             }
344         }
345     }
346     {
347         struct unixuser *tu;
348
349         for (i = 0; i < NUSERS; i++) {
350             for (tu = afs_users[i]; tu; tu = tu->next) {
351                 if (tu->refCount)
352                     printf("user at %lx is held\n", (unsigned long)tu);
353             }
354         }
355     }
356     afs_warn("Done.\n");
357 }
358
359
360 int
361 afs_noop(void)
362 {
363     AFS_STATCNT(afs_noop);
364 #ifdef  AFS_OSF30_ENV
365     return (EOPNOTSUPP);
366 #else
367     return EINVAL;
368 #endif
369 }
370
371 int
372 afs_badop(void)
373 {
374     AFS_STATCNT(afs_badop);
375     osi_Panic("afs bad vnode op");
376     return 0;
377 }
378
379 /*
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.
385  */
386
387 afs_int32
388 afs_data_pointer_to_int32(const void *p)
389 {
390     union {
391         afs_int32 i32[sizeof(void *) / sizeof(afs_int32)];
392         const void *p;
393     } ip;
394
395     int i32_sub;                /* subscript of least significant afs_int32 in ip.i32[] */
396
397     /* set i32_sub */
398
399     {
400         /* used to determine the byte order of the system */
401
402         union {
403             char c[sizeof(int) / sizeof(char)];
404             int i;
405         } ci;
406
407         ci.i = 1;
408         if (ci.c[0] == 1) {
409             /* little-endian system */
410             i32_sub = 0;
411         } else {
412             /* big-endian system */
413             i32_sub = (sizeof ip.i32 / sizeof ip.i32[0]) - 1;
414         }
415     }
416
417     ip.p = p;
418     return ip.i32[i32_sub];
419 }
420
421 #ifdef AFS_LINUX20_ENV
422
423 struct afs_md5 {
424     unsigned int sz[2];
425     afs_int32 counter[4];
426     unsigned char save[64];
427 };
428
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] */
432
433 #define A m->counter[0]
434 #define B m->counter[1]
435 #define C m->counter[2]
436 #define D m->counter[3]
437 #define X data
438
439 static void
440 AFS_MD5_Init (struct afs_md5 *m)
441 {
442     m->sz[0] = 0;
443     m->sz[1] = 0;
444     D = 0x10325476;
445     C = 0x98badcfe;
446     B = 0xefcdab89;
447     A = 0x67452301;
448 }
449
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))
454
455 static inline afs_uint32
456 cshift (afs_uint32 x, unsigned int n)
457 {
458     return ((x << n) | (x >> (32 - n)));
459 }
460
461 #define DOIT(a,b,c,d,k,s,i,OP) \
462 a = b + cshift(a + OP(b,c,d) + X[k] + (i), s)
463
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)
468
469 static inline void
470 calc (struct afs_md5 *m, afs_uint32 *data)
471 {
472     afs_uint32 AA, BB, CC, DD;
473     
474     AA = A;
475     BB = B;
476     CC = C;
477     DD = D;
478     
479     /* Round 1 */
480     
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);
485     
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);
490     
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);
495     
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);
500     
501     /* Round 2 */
502     
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);
507     
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);
512     
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);
517     
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);
522     
523     /* Round 3 */
524     
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);
529     
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);
534     
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);
539     
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);
544     
545     /* Round 4 */
546     
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);
551     
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);
556     
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);
561     
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);
566     
567     A += AA;
568     B += BB;
569     C += CC;
570     D += DD;
571 }
572
573 /*
574  * From `Performance analysis of MD5' by Joseph D. Touch <touch@isi.edu>
575  */
576
577 #if defined(WORDS_BIGENDIAN)
578 static inline afs_uint32
579 swap_u_int32_t (afs_uint32 t)
580 {
581     afs_uint32 temp1, temp2;
582     
583     temp1   = cshift(t, 16);
584     temp2   = temp1 >> 8;
585     temp1  &= 0x00ff00ff;
586     temp2  &= 0x00ff00ff;
587     temp1 <<= 8;
588     return temp1 | temp2;
589 }
590 #endif
591
592 struct x32{
593     unsigned int a:32;
594     unsigned int b:32;
595 };
596
597 static void
598 AFS_MD5_Update (struct afs_md5 *m, const void *v, size_t len)
599 {
600     const unsigned char *p = v;
601     size_t old_sz = m->sz[0];
602     size_t offset;
603     
604     m->sz[0] += len * 8;
605     if (m->sz[0] < old_sz)
606         ++m->sz[1];
607     offset = (old_sz / 8)  % 64;
608     while(len > 0){
609         size_t l = MIN(len, 64 - offset);
610         memcpy(m->save + offset, p, l);
611         offset += l;
612         p += l;
613         len -= l;
614         if(offset == 64){
615 #if defined(WORDS_BIGENDIAN)
616             int i;
617             afs_uint32 temp[16];
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);
622             }
623             calc(m, temp);
624 #else
625             calc(m, (afs_uint32*)m->save);
626 #endif
627             offset = 0;
628         }
629     }
630 }
631
632 static void
633 AFS_MD5_Final (void *res, struct afs_md5 *m)
634 {
635     unsigned char zeros[72];
636     unsigned offset = (m->sz[0] / 8) % 64;
637     unsigned int dstart = (120 - offset - 1) % 64 + 1;
638     
639     *zeros = 0x80;
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);
650     {
651         int i;
652         unsigned char *r = (unsigned char *)res;
653         
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;
659         }
660     }
661 }
662
663 afs_int32 afs_calc_inum (afs_int32 volume, afs_int32 vnode)
664
665     afs_int32 ino, vno = vnode;
666     char digest[16];
667     struct afs_md5 ct;
668     
669     if (afs_new_inum) {
670         AFS_MD5_Init(&ct);
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;
676     } else {
677         ino = (volume << 16) + vnode;
678     }
679     ino &= 0x7fffffff;      /* Assumes 32 bit ino_t ..... */
680     return ino;
681 }
682
683 #else
684
685 afs_int32 afs_calc_inum (afs_int32 volume, afs_int32 vnode)
686 {
687     return (volume << 16) + vnode;
688 }
689
690 #endif