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