Merge 1.8.0~pre4 packaging into master
[pkg-k5-afs_openafs.git] / src / afs / afs_util.c
blob8bd595cc10c98f2757e3e6ff39bfaecae946855b
1 /*
2 * Copyright 2000, International Business Machines Corporation and others.
3 * All Rights Reserved.
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 */
11 * afs_util.c - miscellaneous AFS client utility functions
13 * Implements:
15 #include <afsconfig.h>
16 #include "afs/param.h"
19 #include "afs/stds.h"
20 #include "afs/sysincludes.h" /* Standard vendor system headers */
22 #if !defined(UKERNEL)
23 #if !defined(AFS_LINUX20_ENV)
24 #include <net/if.h>
25 #endif
26 #include <netinet/in.h>
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) */
36 #include "afsincludes.h" /* Afs-based standard headers */
37 #include "afs/afs_stats.h" /* afs statistics */
38 #include "hcrypto/md5.h"
40 #if defined(AFS_AIX_ENV)
41 #include <sys/fp_io.h>
42 #endif
44 afs_int32 afs_md5inum = 0;
46 #ifndef afs_cv2string
47 char *
48 afs_cv2string(char *ttp, afs_uint32 aval)
50 char *tp = ttp;
51 int i;
52 int any;
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;
63 if (!any)
64 *(--tp) = '0';
65 return tp;
67 } /*afs_cv2string */
68 #endif
70 /* not a generic strtoul replacement. for vol/vno/uniq, portable */
72 afs_int32
73 afs_strtoi_r(const char *str, char **endptr, afs_uint32 *ret)
75 char *x;
77 *ret = 0;
78 *endptr = (char *)str;
80 if (!str)
81 return ERANGE;
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;
91 *ret = (*ret * 10) + (*x - '0');
94 *endptr = x;
95 return 0;
98 #ifndef afs_strcasecmp
99 int
100 afs_strcasecmp(const char *s1, const char *s2)
102 while (*s1 && *s2) {
103 char c1, c2;
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;
115 return *s1 - *s2;
117 #endif
119 #ifndef afs_strcat
120 char *
121 afs_strcat(char *s1, char *s2)
123 char *os1;
125 os1 = s1;
126 while (*s1++);
127 --s1;
128 while ((*s1++ = *s2++));
129 return (os1);
131 #endif
133 #ifdef AFS_OBSD34_ENV
134 char *
135 afs_strcpy(char *s1, char *s2)
137 char *os1;
139 os1 = s1;
140 while ((*s1++ = *s2++) != '\0');
141 return os1;
143 #endif
145 #ifndef afs_strchr
146 char *
147 afs_strchr(char *s, int c)
149 char *p;
151 for (p = s; *p; p++)
152 if (*p == c)
153 return p;
154 return NULL;
156 #endif
157 #ifndef afs_strrchr
158 char *
159 afs_strrchr(char *s, int c)
161 char *p = NULL;
163 do {
164 if (*s == c)
165 p = (char*) s;
166 } while (*s++);
167 return p;
169 #endif
171 char *
172 afs_strdup(char *s)
174 char *n;
175 int cc;
177 cc = strlen(s) + 1;
178 n = afs_osi_Alloc(cc);
179 if (n)
180 memcpy(n, s, cc);
182 return n;
185 void
186 print_internet_address(char *preamble, struct srvAddr *sa, char *postamble,
187 int flag, int code, struct rx_connection *rxconn)
189 struct server *aserver = sa->server;
190 char *ptr = "\n";
191 afs_uint32 address;
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";
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);
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 = "";
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);
231 } /*print_internet_address */
235 /* run everywhere, checking locks */
236 void
237 afs_CheckLocks(void)
239 int i;
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);
245 struct vcache *tvc;
246 AFS_STATCNT(afs_CheckLocks);
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);
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);
270 if (afs_indexFlags[i] & IFDataMod)
271 afs_warn("Disk entry %d at %p has IFDataMod flag set.\n", i,
272 tdc);
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);
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);
305 struct unixuser *tu;
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);
316 afs_warn("Done.\n");
321 afs_noop(void)
323 AFS_STATCNT(afs_noop);
324 return EINVAL;
328 afs_badop(void)
330 AFS_STATCNT(afs_badop);
331 osi_Panic("afs bad vnode op");
332 return 0;
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.
343 afs_int32
344 afs_data_pointer_to_int32(const void *p)
346 union {
347 afs_int32 i32[sizeof(void *) / sizeof(afs_int32)];
348 const void *p;
349 } ip;
351 int i32_sub; /* subscript of least significant afs_int32 in ip.i32[] */
353 /* set i32_sub */
356 /* used to determine the byte order of the system */
358 union {
359 char c[sizeof(int) / sizeof(char)];
360 int i;
361 } ci;
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;
373 ip.p = p;
374 return ip.i32[i32_sub];
377 static_inline afs_int32
378 afs_calc_inum_md5(afs_int32 cell, afs_int32 volume, afs_int32 vnode)
380 afs_int32 ino = 0, vno = vnode;
381 char digest[16];
382 struct md5 ct;
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);
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++) {
401 memcpy(&ino, &digest[offset], sizeof(ino));
402 ino ^= (ino ^ vno) & 1;
403 ino &= 0x7fffffff; /* Assumes 32 bit ino_t ..... */
406 return ino;
409 afs_int32
410 afs_calc_inum(afs_int32 cell, afs_int32 volume, afs_int32 vnode)
412 afs_int32 ino;
414 ino = afs_calc_inum_md5(cell, volume, vnode);
416 if (ino == 0 || ino == 1) {
417 ino = (volume << 16) + vnode;
419 ino &= 0x7fffffff; /* Assumes 32 bit ino_t ..... */
420 return ino;