unix cm rx-oblivious connection pooling
[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_DARWIN_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 #ifdef AFS_LINUX20_ENV
40 #include "hcrypto/md5.h"
41 #endif
42
43 #if     defined(AFS_SUN56_ENV)
44 #include <inet/led.h>
45 #include <inet/common.h>
46 #if     defined(AFS_SUN58_ENV)
47 #include <netinet/ip6.h>
48 #endif
49 #include <inet/ip.h>
50 #endif
51
52 #if     defined(AFS_AIX_ENV)
53 #include <sys/fp_io.h>
54 #endif
55
56 afs_int32 afs_new_inum = 0;
57
58 #ifndef afs_cv2string
59 char *
60 afs_cv2string(char *ttp, afs_uint32 aval)
61 {
62     char *tp = ttp;
63     int i;
64     int any;
65
66     AFS_STATCNT(afs_cv2string);
67     any = 0;
68     *(--tp) = 0;
69     while (aval != 0) {
70         i = aval % 10;
71         *(--tp) = '0' + i;
72         aval /= 10;
73         any = 1;
74     }
75     if (!any)
76         *(--tp) = '0';
77     return tp;
78
79 }                               /*afs_cv2string */
80 #endif
81
82 /* not a generic strtoul replacement. for vol/vno/uniq, portable */
83
84 afs_int32
85 afs_strtoi_r(const char *str, char **endptr, afs_uint32 *ret)
86 {
87     char *x;
88
89     *ret = 0;
90     *endptr = (char *)str;
91
92     if (!str)
93         return ERANGE;
94
95     for (x = (char *)str; *x >= '0' && *x <= '9'; x++) {
96         /* Check for impending overflow */
97         if (*ret > 429496729) { /* ULONG_MAX/10 */
98             *ret = 0;
99             *endptr = (char *)str;
100             return EINVAL;
101         }
102
103         *ret = (*ret * 10) + (*x - '0');
104     }
105
106     *endptr = x;
107     return 0;
108 }
109
110 #ifndef afs_strcasecmp
111 int
112 afs_strcasecmp(char *s1, char *s2)
113 {
114     while (*s1 && *s2) {
115         char c1, c2;
116
117         c1 = *s1++;
118         c2 = *s2++;
119         if (c1 >= 'A' && c1 <= 'Z')
120             c1 += 0x20;
121         if (c2 >= 'A' && c2 <= 'Z')
122             c2 += 0x20;
123         if (c1 != c2)
124             return c1 - c2;
125     }
126
127     return *s1 - *s2;
128 }
129 #endif
130
131 #ifndef afs_strcat
132 char *
133 afs_strcat(char *s1, char *s2)
134 {
135     char *os1;
136
137     os1 = s1;
138     while (*s1++);
139     --s1;
140     while ((*s1++ = *s2++));
141     return (os1);
142 }
143 #endif
144
145 #ifdef AFS_OBSD34_ENV
146 char *
147 afs_strcpy(char *s1, char *s2)
148 {
149     char *os1;
150
151     os1 = s1;
152     while ((*s1++ = *s2++) != '\0');
153     return os1;
154 }
155 #endif
156
157 #ifndef afs_strchr
158 char *
159 afs_strchr(char *s, int c)
160 {
161     char *p;
162
163     for (p = s; *p; p++)
164         if (*p == c)
165             return p;
166     return NULL;
167 }
168 #endif
169 #ifndef afs_strrchr
170 char *
171 afs_strrchr(char *s, int c)
172 {
173     char *p = NULL;
174
175     do {
176         if (*s == c)
177             p = (char*) s;
178     } while (*s++);
179     return p;
180 }
181 #endif
182
183 char *
184 afs_strdup(char *s)
185 {
186     char *n;
187     int cc;
188
189     cc = strlen(s) + 1;
190     n = afs_osi_Alloc(cc);
191     if (n)
192         memcpy(n, s, cc);
193
194     return n;
195 }
196
197 void
198 print_internet_address(char *preamble, struct srvAddr *sa, char *postamble,
199                        int flag)
200 {
201     struct server *aserver = sa->server;
202     char *ptr = "\n";
203     afs_uint32 address;
204
205     AFS_STATCNT(print_internet_address);
206     address = ntohl(sa->sa_ip);
207     if (aserver->flags & SRVR_MULTIHOMED) {
208         if (flag == 1) {        /* server down mesg */
209             if (!(aserver->flags & SRVR_ISDOWN))
210                 ptr =
211                     " (multi-homed address; other same-host interfaces maybe up)\n";
212             else
213                 ptr = " (all multi-homed ip addresses down for the server)\n";
214         } else if (flag == 2) { /* server up mesg */
215             ptr =
216                 " (multi-homed address; other same-host interfaces may still be down)\n";
217         }
218     }
219     afs_warn("%s%d.%d.%d.%d in cell %s%s%s", preamble, (address >> 24),
220              (address >> 16) & 0xff, (address >> 8) & 0xff, (address) & 0xff,
221              aserver->cell->cellName, postamble, ptr);
222     afs_warnuser("%s%d.%d.%d.%d in cell %s%s%s", preamble, (address >> 24),
223                  (address >> 16) & 0xff, (address >> 8) & 0xff,
224                  (address) & 0xff, aserver->cell->cellName, postamble, ptr);
225
226 }                               /*print_internet_address */
227
228
229
230 /* run everywhere, checking locks */
231 void
232 afs_CheckLocks(void)
233 {
234     int i;
235
236     afs_warn("Looking for locked data structures.\n");
237     afs_warn("conn %p, volume %p, user %p, cell %p, server %p\n", &afs_xconn,
238              &afs_xvolume, &afs_xuser, &afs_xcell, &afs_xserver);
239     {
240         struct vcache *tvc;
241         AFS_STATCNT(afs_CheckLocks);
242
243         for (i = 0; i < VCSIZE; i++) {
244             for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
245                 if (tvc->f.states & CVInit) continue;
246 #ifdef AFS_DARWIN80_ENV
247                 if (vnode_isinuse(AFSTOV(tvc), 0))
248 #else
249                 if (VREFCOUNT(tvc))
250 #endif
251                     afs_warn("Stat cache entry at %p is held\n", tvc);
252                 if (CheckLock(&tvc->lock))
253                     afs_warn("Stat entry at %p is locked\n", tvc);
254             }
255         }
256     }
257     {
258         struct dcache *tdc;
259         for (i = 0; i < afs_cacheFiles; i++) {
260             tdc = afs_indexTable[i];
261             if (tdc) {
262                 if (tdc->refCount)
263                     afs_warn("Disk entry %d at %p is held\n", i, tdc);
264             }
265             if (afs_indexFlags[i] & IFDataMod)
266                 afs_warn("Disk entry %d at %p has IFDataMod flag set.\n", i,
267                          tdc);
268         }
269     }
270     {
271         struct srvAddr *sa;
272         struct server *ts;
273         struct sa_conn_vector *tcv;
274         for (i = 0; i < NSERVERS; i++) {
275             for (ts = afs_servers[i]; ts; ts = ts->next) {
276                 if (ts->flags & SRVR_ISDOWN)
277                     afs_warn("Server entry %p is marked down\n", ts);
278                 for (sa = ts->addr; sa; sa = sa->next_sa) {
279                     for (tcv = sa->conns; tcv; tcv = tcv->next) {
280                         if (tcv->refCount)
281                             afs_warn("conn at %p (server %x) is held\n", tcv,
282                                      sa->sa_ip);
283                     }
284                 }
285             }
286         }
287     }
288     {
289         struct volume *tv;
290         for (i = 0; i < NVOLS; i++) {
291             for (tv = afs_volumes[i]; tv; tv = tv->next) {
292                 if (CheckLock(&tv->lock))
293                     afs_warn("volume at %p is locked\n", tv);
294                 if (tv->refCount)
295                     afs_warn("volume at %p is held\n", tv);
296             }
297         }
298     }
299     {
300         struct unixuser *tu;
301
302         for (i = 0; i < NUSERS; i++) {
303             for (tu = afs_users[i]; tu; tu = tu->next) {
304                 if (tu->refCount)
305                     afs_warn("user at %lx is held\n", (unsigned long)tu);
306             }
307         }
308     }
309     afs_warn("Done.\n");
310 }
311
312
313 int
314 afs_noop(void)
315 {
316     AFS_STATCNT(afs_noop);
317     return EINVAL;
318 }
319
320 int
321 afs_badop(void)
322 {
323     AFS_STATCNT(afs_badop);
324     osi_Panic("afs bad vnode op");
325     return 0;
326 }
327
328 /*
329  * afs_data_pointer_to_int32() - returns least significant afs_int32 of the
330  * given data pointer, without triggering "cast truncates pointer"
331  * warnings.  We use this where we explicitly don't care whether a
332  * pointer is truncated -- it loses information where a pointer is
333  * larger than an afs_int32.
334  */
335
336 afs_int32
337 afs_data_pointer_to_int32(const void *p)
338 {
339     union {
340         afs_int32 i32[sizeof(void *) / sizeof(afs_int32)];
341         const void *p;
342     } ip;
343
344     int i32_sub;                /* subscript of least significant afs_int32 in ip.i32[] */
345
346     /* set i32_sub */
347
348     {
349         /* used to determine the byte order of the system */
350
351         union {
352             char c[sizeof(int) / sizeof(char)];
353             int i;
354         } ci;
355
356         ci.i = 1;
357         if (ci.c[0] == 1) {
358             /* little-endian system */
359             i32_sub = 0;
360         } else {
361             /* big-endian system */
362             i32_sub = (sizeof ip.i32 / sizeof ip.i32[0]) - 1;
363         }
364     }
365
366     ip.p = p;
367     return ip.i32[i32_sub];
368 }
369
370 #ifdef AFS_LINUX20_ENV
371
372 afs_int32
373 afs_calc_inum(afs_int32 volume, afs_int32 vnode)
374 {
375     afs_int32 ino, vno = vnode;
376     char digest[16];
377     struct md5 ct;
378
379     if (afs_new_inum) {
380         MD5_Init(&ct);
381         MD5_Update(&ct, &volume, 4);
382         MD5_Update(&ct, &vnode, 4);
383         MD5_Final(digest, &ct);
384         memcpy(&ino, digest, sizeof(afs_int32));
385         ino ^= (ino ^ vno) & 1;
386     } else {
387         ino = (volume << 16) + vnode;
388     }
389     ino &= 0x7fffffff;      /* Assumes 32 bit ino_t ..... */
390     return ino;
391 }
392
393 #else
394
395 afs_int32
396 afs_calc_inum (afs_int32 volume, afs_int32 vnode)
397 {
398     return (volume << 16) + vnode;
399 }
400
401 #endif