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