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