nfs3-no-readdirplus-20041108
[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 RCSID
19     ("$Header$");
20
21 #include "afs/stds.h"
22 #include "afs/sysincludes.h"    /* Standard vendor system headers */
23
24 #if !defined(UKERNEL)
25 #if !defined(AFS_LINUX20_ENV)
26 #include <net/if.h>
27 #endif
28 #include <netinet/in.h>
29
30 #ifdef AFS_SGI62_ENV
31 #include "h/hashing.h"
32 #endif
33 #if !defined(AFS_HPUX110_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_DARWIN60_ENV)
34 #include <netinet/in_var.h>
35 #endif /* ! AFS_HPUX110_ENV */
36 #endif /* !defined(UKERNEL) */
37
38 #include "afsincludes.h"        /* Afs-based standard headers */
39 #include "afs/afs_stats.h"      /* afs statistics */
40
41 #if     defined(AFS_SUN56_ENV)
42 #include <inet/led.h>
43 #include <inet/common.h>
44 #if     defined(AFS_SUN58_ENV)
45 #include <netinet/ip6.h>
46 #endif
47 #include <inet/ip.h>
48 #endif
49
50 #if     defined(AFS_AIX_ENV)
51 #include <sys/fp_io.h>
52 #endif
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
138 char *
139 afs_strdup(char *s)
140 {
141     char *n;
142     int cc;
143
144     cc = strlen(s) + 1;
145     n = (char *)afs_osi_Alloc(cc);
146     if (n)
147         memcpy(n, s, cc);
148
149     return n;
150 }
151
152 void
153 print_internet_address(char *preamble, struct srvAddr *sa, char *postamble,
154                        int flag)
155 {
156     register struct server *aserver = sa->server;
157     char *ptr = "\n";
158     afs_uint32 address;
159
160     AFS_STATCNT(print_internet_address);
161     address = ntohl(sa->sa_ip);
162     if (aserver->flags & SRVR_MULTIHOMED) {
163         if (flag == 1) {        /* server down mesg */
164             if (!(aserver->flags & SRVR_ISDOWN))
165                 ptr =
166                     " (multi-homed address; other same-host interfaces maybe up)\n";
167             else
168                 ptr = " (all multi-homed ip addresses down for the server)\n";
169         } else if (flag == 2) { /* server up mesg */
170             ptr =
171                 " (multi-homed address; other same-host interfaces may still be down)\n";
172         }
173     }
174     afs_warn("%s%d.%d.%d.%d in cell %s%s%s", preamble, (address >> 24),
175              (address >> 16) & 0xff, (address >> 8) & 0xff, (address) & 0xff,
176              aserver->cell->cellName, postamble, ptr);
177     afs_warnuser("%s%d.%d.%d.%d in cell %s%s%s", preamble, (address >> 24),
178                  (address >> 16) & 0xff, (address >> 8) & 0xff,
179                  (address) & 0xff, aserver->cell->cellName, postamble, ptr);
180
181 }                               /*print_internet_address */
182
183
184
185 /* * * * * * *
186  * this code badly needs to be cleaned up...  too many ugly ifdefs.
187  * XXX
188  */
189 #if 0
190 void
191 afs_warn(char *a, long b, long c, long d, long e, long f, long g, long h,
192          long i, long j)
193 #else
194 void
195 afs_warn(a, b, c, d, e, f, g, h, i, j)
196      char *a;
197 #if defined( AFS_USE_VOID_PTR)
198      void *b, *c, *d, *e, *f, *g, *h, *i, *j;
199 #else
200      long b, c, d, e, f, g, h, i, j;
201 #endif
202 #endif
203 {
204     AFS_STATCNT(afs_warn);
205
206     if (afs_showflags & GAGCONSOLE) {
207 #if defined(AFS_AIX_ENV)
208         struct file *fd;
209
210         /* cf. console_printf() in oncplus/kernext/nfs/serv/shared.c */
211         if (fp_open
212             ("/dev/console", O_WRONLY | O_NOCTTY | O_NDELAY, 0666, 0, FP_SYS,
213              &fd) == 0) {
214             char buf[1024];
215             ssize_t len;
216             ssize_t count;
217
218             sprintf(buf, a, b, c, d, e, f, g, h, i, j);
219             len = strlen(buf);
220             fp_write(fd, buf, len, 0, UIO_SYSSPACE, &count);
221             fp_close(fd);
222         }
223 #else
224         printf(a, b, c, d, e, f, g, h, i, j);
225 #endif
226     }
227 }
228
229 #if 0
230 void
231 afs_warnuser(char *a, long b, long c, long d, long e, long f, long g, long h,
232              long i, long j)
233 #else
234 void
235 afs_warnuser(a, b, c, d, e, f, g, h, i, j)
236      char *a;
237      long b, c, d, e, f, g, h, i, j;
238 #endif
239 {
240     AFS_STATCNT(afs_warnuser);
241     if (afs_showflags & GAGUSER) {
242 #ifdef AFS_GLOBAL_SUNLOCK
243         int haveGlock = ISAFS_GLOCK();
244         if (haveGlock)
245             AFS_GUNLOCK();
246 #endif /* AFS_GLOBAL_SUNLOCK */
247
248         uprintf(a, b, c, d, e, f, g, h, i, j);
249
250 #ifdef AFS_GLOBAL_SUNLOCK
251         if (haveGlock)
252             AFS_GLOCK();
253 #endif /* AFS_GLOBAL_SUNLOCK */
254     }
255 }
256
257
258 /* run everywhere, checking locks */
259 void
260 afs_CheckLocks(void)
261 {
262     register int i;
263
264     afs_warn("Looking for locked data structures.\n");
265     afs_warn("conn %x, volume %x, user %x, cell %x, server %x\n", afs_xconn,
266              afs_xvolume, afs_xuser, afs_xcell, afs_xserver);
267     {
268         register struct vcache *tvc;
269         AFS_STATCNT(afs_CheckLocks);
270
271         for (i = 0; i < VCSIZE; i++) {
272             for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
273 #ifdef  AFS_OSF_ENV
274                 if (VREFCOUNT(tvc) > 1)
275 #else /* AFS_OSF_ENV */
276                 if (VREFCOUNT(tvc))
277 #endif
278                     afs_warn("Stat cache entry at %x is held\n", tvc);
279                 if (CheckLock(&tvc->lock))
280                     afs_warn("Stat entry at %x is locked\n", tvc);
281             }
282         }
283     }
284     {
285         register struct dcache *tdc;
286         for (i = 0; i < afs_cacheFiles; i++) {
287             tdc = afs_indexTable[i];
288             if (tdc) {
289                 if (tdc->refCount)
290                     afs_warn("Disk entry %d at %x is held\n", i, tdc);
291             }
292             if (afs_indexFlags[i] & IFDataMod)
293                 afs_warn("Disk entry %d at %x has IFDataMod flag set.\n", i,
294                          tdc);
295         }
296     }
297     {
298         struct srvAddr *sa;
299         struct server *ts;
300         struct conn *tc;
301         for (i = 0; i < NSERVERS; i++) {
302             for (ts = afs_servers[i]; ts; ts = ts->next) {
303                 if (ts->flags & SRVR_ISDOWN)
304                     printf("Server entry %x is marked down\n", ts);
305                 for (sa = ts->addr; sa; sa = sa->next_sa) {
306                     for (tc = sa->conns; tc; tc = tc->next) {
307                         if (tc->refCount)
308                             afs_warn("conn at %x (server %x) is held\n", tc,
309                                      sa->sa_ip);
310                     }
311                 }
312             }
313         }
314     }
315     {
316         struct volume *tv;
317         for (i = 0; i < NVOLS; i++) {
318             for (tv = afs_volumes[i]; tv; tv = tv->next) {
319                 if (CheckLock(&tv->lock))
320                     afs_warn("volume at %x is locked\n", tv);
321                 if (tv->refCount)
322                     afs_warn("volume at %x is held\n", tv);
323             }
324         }
325     }
326     {
327         struct unixuser *tu;
328
329         for (i = 0; i < NUSERS; i++) {
330             for (tu = afs_users[i]; tu; tu = tu->next) {
331                 if (tu->refCount)
332                     printf("user at %x is held\n", tu);
333             }
334         }
335     }
336     afs_warn("Done.\n");
337 }
338
339
340 int
341 afs_noop(void)
342 {
343     AFS_STATCNT(afs_noop);
344 #ifdef  AFS_OSF30_ENV
345     return (EOPNOTSUPP);
346 #else
347     return EINVAL;
348 #endif
349 }
350
351 int
352 afs_badop(void)
353 {
354     AFS_STATCNT(afs_badop);
355     osi_Panic("afs bad vnode op");
356     return 0;
357 }
358
359 /*
360  * afs_data_pointer_to_int32() - returns least significant afs_int32 of the
361  * given data pointer, without triggering "cast truncates pointer"
362  * warnings.  We use this where we explicitly don't care whether a
363  * pointer is truncated -- it loses information where a pointer is
364  * larger than an afs_int32.
365  */
366
367 afs_int32
368 afs_data_pointer_to_int32(const void *p)
369 {
370     union {
371         afs_int32 i32[sizeof(void *) / sizeof(afs_int32)];
372         const void *p;
373     } ip;
374
375     int i32_sub;                /* subscript of least significant afs_int32 in ip.i32[] */
376
377     /* set i32_sub */
378
379     {
380         /* used to determine the byte order of the system */
381
382         union {
383             char c[sizeof(int) / sizeof(char)];
384             int i;
385         } ci;
386
387         ci.i = 1;
388         if (ci.c[0] == 1) {
389             /* little-endian system */
390             i32_sub = 0;
391         } else {
392             /* big-endian system */
393             i32_sub = (sizeof ip.i32 / sizeof ip.i32[0]) - 1;
394         }
395     }
396
397     ip.p = p;
398     return ip.i32[i32_sub];
399 }