Solaris: Don't access lbolt directly
[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_DARWIN80_ENV
219                 if (vnode_isinuse(AFSTOV(tvc), 0))
220 #else
221                 if (VREFCOUNT(tvc))
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     return EINVAL;
290 }
291
292 int
293 afs_badop(void)
294 {
295     AFS_STATCNT(afs_badop);
296     osi_Panic("afs bad vnode op");
297     return 0;
298 }
299
300 /*
301  * afs_data_pointer_to_int32() - returns least significant afs_int32 of the
302  * given data pointer, without triggering "cast truncates pointer"
303  * warnings.  We use this where we explicitly don't care whether a
304  * pointer is truncated -- it loses information where a pointer is
305  * larger than an afs_int32.
306  */
307
308 afs_int32
309 afs_data_pointer_to_int32(const void *p)
310 {
311     union {
312         afs_int32 i32[sizeof(void *) / sizeof(afs_int32)];
313         const void *p;
314     } ip;
315
316     int i32_sub;                /* subscript of least significant afs_int32 in ip.i32[] */
317
318     /* set i32_sub */
319
320     {
321         /* used to determine the byte order of the system */
322
323         union {
324             char c[sizeof(int) / sizeof(char)];
325             int i;
326         } ci;
327
328         ci.i = 1;
329         if (ci.c[0] == 1) {
330             /* little-endian system */
331             i32_sub = 0;
332         } else {
333             /* big-endian system */
334             i32_sub = (sizeof ip.i32 / sizeof ip.i32[0]) - 1;
335         }
336     }
337
338     ip.p = p;
339     return ip.i32[i32_sub];
340 }
341
342 #ifdef AFS_LINUX20_ENV
343
344 afs_int32 afs_calc_inum (afs_int32 volume, afs_int32 vnode)
345
346     afs_int32 ino, vno = vnode;
347     char digest[16];
348     struct afs_md5 ct;
349     
350     if (afs_new_inum) {
351         AFS_MD5_Init(&ct);
352         AFS_MD5_Update(&ct, &volume, 4);
353         AFS_MD5_Update(&ct, &vnode, 4);
354         AFS_MD5_Final(digest, &ct);
355         memcpy(&ino, digest, sizeof(afs_int32));
356         ino ^= (ino ^ vno) & 1;
357     } else {
358         ino = (volume << 16) + vnode;
359     }
360     ino &= 0x7fffffff;      /* Assumes 32 bit ino_t ..... */
361     return ino;
362 }
363
364 #else
365
366 afs_int32 afs_calc_inum (afs_int32 volume, afs_int32 vnode)
367 {
368     return (volume << 16) + vnode;
369 }
370
371 #endif