Remove CacheStoreProcs and CacheFetchProcs from the afs_cacheOps.
[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
19 #include "afs/stds.h"
20 #include "afs/sysincludes.h"    /* Standard vendor system headers */
21
22 #if !defined(UKERNEL)
23 #if !defined(AFS_LINUX20_ENV)
24 #include <net/if.h>
25 #endif
26 #include <netinet/in.h>
27
28 #ifdef AFS_SGI62_ENV
29 #include "h/hashing.h"
30 #endif
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) */
35
36 #include "afsincludes.h"        /* Afs-based standard headers */
37 #include "afs/afs_stats.h"      /* afs statistics */
38
39 #if     defined(AFS_SUN56_ENV)
40 #include <inet/led.h>
41 #include <inet/common.h>
42 #if     defined(AFS_SUN58_ENV)
43 #include <netinet/ip6.h>
44 #endif
45 #include <inet/ip.h>
46 #endif
47
48 #if     defined(AFS_AIX_ENV)
49 #include <sys/fp_io.h>
50 #endif
51
52 afs_int32 afs_new_inum = 0;
53
54 #ifndef afs_cv2string
55 char *
56 afs_cv2string(char *ttp, afs_uint32 aval)
57 {
58     register char *tp = ttp;
59     register int i;
60     int any;
61
62     AFS_STATCNT(afs_cv2string);
63     any = 0;
64     *(--tp) = 0;
65     while (aval != 0) {
66         i = aval % 10;
67         *(--tp) = '0' + i;
68         aval /= 10;
69         any = 1;
70     }
71     if (!any)
72         *(--tp) = '0';
73     return tp;
74
75 }                               /*afs_cv2string */
76 #endif
77
78 #ifndef afs_strcasecmp
79 int
80 afs_strcasecmp(char *s1, char *s2)
81 {
82     while (*s1 && *s2) {
83         char c1, c2;
84
85         c1 = *s1++;
86         c2 = *s2++;
87         if (c1 >= 'A' && c1 <= 'Z')
88             c1 += 0x20;
89         if (c2 >= 'A' && c2 <= 'Z')
90             c2 += 0x20;
91         if (c1 != c2)
92             return c1 - c2;
93     }
94
95     return *s1 - *s2;
96 }
97 #endif
98
99 #ifndef afs_strcat
100 char *
101 afs_strcat(char *s1, char *s2)
102 {
103     char *os1;
104
105     os1 = s1;
106     while (*s1++);
107     --s1;
108     while ((*s1++ = *s2++));
109     return (os1);
110 }
111 #endif
112
113 #ifdef AFS_OBSD34_ENV
114 char *
115 afs_strcpy(char *s1, char *s2)
116 {
117     char *os1;
118
119     os1 = s1;
120     while ((*s1++ = *s2++) != '\0');
121     return os1;
122 }
123 #endif
124
125 #ifndef afs_strchr
126 char *
127 afs_strchr(char *s, int c)
128 {
129     char *p;
130
131     for (p = s; *p; p++)
132         if (*p == c)
133             return p;
134     return NULL;
135 }
136 #endif
137 #ifndef afs_strrchr
138 char *
139 afs_strrchr(char *s, int c)
140 {
141     char *p = NULL;
142     
143     do {
144         if (*s == c)
145             p = (char*) s;
146     } while (*s++);
147     return p;
148 }
149 #endif
150
151 char *
152 afs_strdup(char *s)
153 {
154     char *n;
155     int cc;
156
157     cc = strlen(s) + 1;
158     n = (char *)afs_osi_Alloc(cc);
159     if (n)
160         memcpy(n, s, cc);
161
162     return n;
163 }
164
165 void
166 print_internet_address(char *preamble, struct srvAddr *sa, char *postamble,
167                        int flag)
168 {
169     register struct server *aserver = sa->server;
170     char *ptr = "\n";
171     afs_uint32 address;
172
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))
178                 ptr =
179                     " (multi-homed address; other same-host interfaces maybe up)\n";
180             else
181                 ptr = " (all multi-homed ip addresses down for the server)\n";
182         } else if (flag == 2) { /* server up mesg */
183             ptr =
184                 " (multi-homed address; other same-host interfaces may still be down)\n";
185         }
186     }
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);
193
194 }                               /*print_internet_address */
195
196
197
198 /* run everywhere, checking locks */
199 void
200 afs_CheckLocks(void)
201 {
202     register int i;
203
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);
207     {
208         register struct vcache *tvc;
209         AFS_STATCNT(afs_CheckLocks);
210
211         for (i = 0; i < VCSIZE; i++) {
212             for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
213                 if (tvc->f.states & CVInit) continue;
214 #ifdef  AFS_OSF_ENV
215                 if (VREFCOUNT(tvc) > 1)
216 #else /* AFS_OSF_ENV */
217 #ifdef AFS_DARWIN80_ENV
218                 if (vnode_isinuse(AFSTOV(tvc), 0))
219 #else
220                 if (VREFCOUNT(tvc))
221 #endif
222 #endif
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);
226             }
227         }
228     }
229     {
230         register struct dcache *tdc;
231         for (i = 0; i < afs_cacheFiles; i++) {
232             tdc = afs_indexTable[i];
233             if (tdc) {
234                 if (tdc->refCount)
235                     afs_warn("Disk entry %d at %x is held\n", i, tdc);
236             }
237             if (afs_indexFlags[i] & IFDataMod)
238                 afs_warn("Disk entry %d at %x has IFDataMod flag set.\n", i,
239                          tdc);
240         }
241     }
242     {
243         struct srvAddr *sa;
244         struct server *ts;
245         struct afs_conn *tc;
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) {
252                         if (tc->refCount)
253                             afs_warn("conn at %x (server %x) is held\n", tc,
254                                      sa->sa_ip);
255                     }
256                 }
257             }
258         }
259     }
260     {
261         struct volume *tv;
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);
266                 if (tv->refCount)
267                     afs_warn("volume at %x is held\n", tv);
268             }
269         }
270     }
271     {
272         struct unixuser *tu;
273
274         for (i = 0; i < NUSERS; i++) {
275             for (tu = afs_users[i]; tu; tu = tu->next) {
276                 if (tu->refCount)
277                     printf("user at %lx is held\n", (unsigned long)tu);
278             }
279         }
280     }
281     afs_warn("Done.\n");
282 }
283
284
285 int
286 afs_noop(void)
287 {
288     AFS_STATCNT(afs_noop);
289 #ifdef  AFS_OSF30_ENV
290     return (EOPNOTSUPP);
291 #else
292     return EINVAL;
293 #endif
294 }
295
296 int
297 afs_badop(void)
298 {
299     AFS_STATCNT(afs_badop);
300     osi_Panic("afs bad vnode op");
301     return 0;
302 }
303
304 /*
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.
310  */
311
312 afs_int32
313 afs_data_pointer_to_int32(const void *p)
314 {
315     union {
316         afs_int32 i32[sizeof(void *) / sizeof(afs_int32)];
317         const void *p;
318     } ip;
319
320     int i32_sub;                /* subscript of least significant afs_int32 in ip.i32[] */
321
322     /* set i32_sub */
323
324     {
325         /* used to determine the byte order of the system */
326
327         union {
328             char c[sizeof(int) / sizeof(char)];
329             int i;
330         } ci;
331
332         ci.i = 1;
333         if (ci.c[0] == 1) {
334             /* little-endian system */
335             i32_sub = 0;
336         } else {
337             /* big-endian system */
338             i32_sub = (sizeof ip.i32 / sizeof ip.i32[0]) - 1;
339         }
340     }
341
342     ip.p = p;
343     return ip.i32[i32_sub];
344 }
345
346 #ifdef AFS_LINUX20_ENV
347
348 struct afs_md5 {
349     unsigned int sz[2];
350     afs_int32 counter[4];
351     unsigned char save[64];
352 };
353
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] */
357
358 #define A m->counter[0]
359 #define B m->counter[1]
360 #define C m->counter[2]
361 #define D m->counter[3]
362 #define X data
363
364 static void
365 AFS_MD5_Init (struct afs_md5 *m)
366 {
367     m->sz[0] = 0;
368     m->sz[1] = 0;
369     D = 0x10325476;
370     C = 0x98badcfe;
371     B = 0xefcdab89;
372     A = 0x67452301;
373 }
374
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))
379
380 static inline afs_uint32
381 cshift (afs_uint32 x, unsigned int n)
382 {
383     return ((x << n) | (x >> (32 - n)));
384 }
385
386 #define DOIT(a,b,c,d,k,s,i,OP) \
387 a = b + cshift(a + OP(b,c,d) + X[k] + (i), s)
388
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)
393
394 static inline void
395 calc (struct afs_md5 *m, afs_uint32 *data)
396 {
397     afs_uint32 AA, BB, CC, DD;
398     
399     AA = A;
400     BB = B;
401     CC = C;
402     DD = D;
403     
404     /* Round 1 */
405     
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);
410     
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);
415     
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);
420     
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);
425     
426     /* Round 2 */
427     
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);
432     
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);
437     
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);
442     
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);
447     
448     /* Round 3 */
449     
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);
454     
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);
459     
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);
464     
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);
469     
470     /* Round 4 */
471     
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);
476     
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);
481     
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);
486     
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);
491     
492     A += AA;
493     B += BB;
494     C += CC;
495     D += DD;
496 }
497
498 /*
499  * From `Performance analysis of MD5' by Joseph D. Touch <touch@isi.edu>
500  */
501
502 #if defined(WORDS_BIGENDIAN)
503 static inline afs_uint32
504 swap_u_int32_t (afs_uint32 t)
505 {
506     afs_uint32 temp1, temp2;
507     
508     temp1   = cshift(t, 16);
509     temp2   = temp1 >> 8;
510     temp1  &= 0x00ff00ff;
511     temp2  &= 0x00ff00ff;
512     temp1 <<= 8;
513     return temp1 | temp2;
514 }
515 #endif
516
517 struct x32{
518     unsigned int a:32;
519     unsigned int b:32;
520 };
521
522 static void
523 AFS_MD5_Update (struct afs_md5 *m, const void *v, size_t len)
524 {
525     const unsigned char *p = v;
526     size_t old_sz = m->sz[0];
527     size_t offset;
528     
529     m->sz[0] += len * 8;
530     if (m->sz[0] < old_sz)
531         ++m->sz[1];
532     offset = (old_sz / 8)  % 64;
533     while(len > 0){
534         size_t l = MIN(len, 64 - offset);
535         memcpy(m->save + offset, p, l);
536         offset += l;
537         p += l;
538         len -= l;
539         if(offset == 64){
540 #if defined(WORDS_BIGENDIAN)
541             int i;
542             afs_uint32 temp[16];
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);
547             }
548             calc(m, temp);
549 #else
550             calc(m, (afs_uint32*)m->save);
551 #endif
552             offset = 0;
553         }
554     }
555 }
556
557 static void
558 AFS_MD5_Final (void *res, struct afs_md5 *m)
559 {
560     unsigned char zeros[72];
561     unsigned offset = (m->sz[0] / 8) % 64;
562     unsigned int dstart = (120 - offset - 1) % 64 + 1;
563     
564     *zeros = 0x80;
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);
575     {
576         int i;
577         unsigned char *r = (unsigned char *)res;
578         
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;
584         }
585     }
586 }
587
588 afs_int32 afs_calc_inum (afs_int32 volume, afs_int32 vnode)
589
590     afs_int32 ino, vno = vnode;
591     char digest[16];
592     struct afs_md5 ct;
593     
594     if (afs_new_inum) {
595         AFS_MD5_Init(&ct);
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;
601     } else {
602         ino = (volume << 16) + vnode;
603     }
604     ino &= 0x7fffffff;      /* Assumes 32 bit ino_t ..... */
605     return ino;
606 }
607
608 #else
609
610 afs_int32 afs_calc_inum (afs_int32 volume, afs_int32 vnode)
611 {
612     return (volume << 16) + vnode;
613 }
614
615 #endif