2 * Copyright 2000, International Business Machines Corporation and others.
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
11 * afs_util.c - miscellaneous AFS client utility functions
15 #include <afsconfig.h>
16 #include "afs/param.h"
20 #include "afs/sysincludes.h" /* Standard vendor system headers */
23 #if !defined(AFS_LINUX20_ENV)
26 #include <netinet/in.h>
29 #include "h/hashing.h"
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 */
39 #ifdef AFS_LINUX20_ENV
40 #include "hcrypto/md5.h"
43 #if defined(AFS_SUN5_ENV)
45 #include <inet/common.h>
46 #include <netinet/ip6.h>
50 #if defined(AFS_AIX_ENV)
51 #include <sys/fp_io.h>
54 afs_int32 afs_new_inum = 0;
58 afs_cv2string(char *ttp, afs_uint32 aval)
64 AFS_STATCNT(afs_cv2string);
80 /* not a generic strtoul replacement. for vol/vno/uniq, portable */
83 afs_strtoi_r(const char *str, char **endptr, afs_uint32 *ret)
88 *endptr = (char *)str;
93 for (x = (char *)str; *x >= '0' && *x <= '9'; x++) {
94 /* Check for impending overflow */
95 if (*ret > 429496729) { /* ULONG_MAX/10 */
97 *endptr = (char *)str;
101 *ret = (*ret * 10) + (*x - '0');
108 #ifndef afs_strcasecmp
110 afs_strcasecmp(const char *s1, const char *s2)
117 if (c1 >= 'A' && c1 <= 'Z')
119 if (c2 >= 'A' && c2 <= 'Z')
131 afs_strcat(char *s1, char *s2)
138 while ((*s1++ = *s2++));
143 #ifdef AFS_OBSD34_ENV
145 afs_strcpy(char *s1, char *s2)
150 while ((*s1++ = *s2++) != '\0');
157 afs_strchr(char *s, int c)
169 afs_strrchr(char *s, int c)
188 n = afs_osi_Alloc(cc);
196 print_internet_address(char *preamble, struct srvAddr *sa, char *postamble,
197 int flag, int code, struct rx_connection *rxconn)
199 struct server *aserver = sa->server;
203 AFS_STATCNT(print_internet_address);
204 address = ntohl(sa->sa_ip);
205 if (aserver->flags & SRVR_MULTIHOMED) {
206 if (flag == 1) { /* server down mesg */
207 if (!(aserver->flags & SRVR_ISDOWN))
209 " (multi-homed address; other same-host interfaces maybe up)\n";
211 ptr = " (all multi-homed ip addresses down for the server)\n";
212 } else if (flag == 2) { /* server up mesg */
214 " (multi-homed address; other same-host interfaces may still be down)\n";
217 afs_warn("%s%d.%d.%d.%d in cell %s%s (code %d)%s", preamble, (address >> 24),
218 (address >> 16) & 0xff, (address >> 8) & 0xff, (address) & 0xff,
219 aserver->cell->cellName, postamble, code, ptr);
220 afs_warnuser("%s%d.%d.%d.%d in cell %s%s (code %d)%s", preamble, (address >> 24),
221 (address >> 16) & 0xff, (address >> 8) & 0xff,
222 (address) & 0xff, aserver->cell->cellName, postamble, code, ptr);
224 if (flag == 1 && rxconn) {
225 /* server was marked down, check Rx to see if this was possibly due to
226 * an ICMP error received from the network */
227 int errorigin, errtype, errcode;
229 if (rx_GetNetworkError(rxconn, &errorigin, &errtype, &errcode, &errmsg) == 0) {
230 const char *str1 = " (";
231 const char *str2 = ")";
233 errmsg = str1 = str2 = "";
235 afs_warn("afs: network error for %d.%d.%d.%d:%d: origin %d type %d code %d%s%s%s\n",
237 (address >> 16) & 0xff,
238 (address >> 8) & 0xff,
240 (int)ntohs(sa->sa_portal),
241 errorigin, errtype, errcode, str1, errmsg, str2);
242 afs_warnuser("afs: network error for %d.%d.%d.%d:%d: origin %d type %d code %d%s%s%s\n",
244 (address >> 16) & 0xff,
245 (address >> 8) & 0xff,
247 (int)ntohs(sa->sa_portal),
248 errorigin, errtype, errcode, str1, errmsg, str2);
251 } /*print_internet_address */
255 /* run everywhere, checking locks */
261 afs_warn("Looking for locked data structures.\n");
262 afs_warn("conn %p, volume %p, user %p, cell %p, server %p\n", &afs_xconn,
263 &afs_xvolume, &afs_xuser, &afs_xcell, &afs_xserver);
266 AFS_STATCNT(afs_CheckLocks);
268 for (i = 0; i < VCSIZE; i++) {
269 for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
270 if (tvc->f.states & CVInit) continue;
271 #ifdef AFS_DARWIN80_ENV
272 if (vnode_isinuse(AFSTOV(tvc), 0))
276 afs_warn("Stat cache entry at %p is held\n", tvc);
277 if (CheckLock(&tvc->lock))
278 afs_warn("Stat entry at %p is locked\n", tvc);
284 for (i = 0; i < afs_cacheFiles; i++) {
285 tdc = afs_indexTable[i];
288 afs_warn("Disk entry %d at %p is held\n", i, tdc);
290 if (afs_indexFlags[i] & IFDataMod)
291 afs_warn("Disk entry %d at %p has IFDataMod flag set.\n", i,
298 struct sa_conn_vector *tcv;
299 for (i = 0; i < NSERVERS; i++) {
300 for (ts = afs_servers[i]; ts; ts = ts->next) {
301 if (ts->flags & SRVR_ISDOWN)
302 afs_warn("Server entry %p is marked down\n", ts);
303 for (sa = ts->addr; sa; sa = sa->next_sa) {
304 for (tcv = sa->conns; tcv; tcv = tcv->next) {
306 afs_warn("conn at %p (server %x) is held\n", tcv,
315 for (i = 0; i < NVOLS; i++) {
316 for (tv = afs_volumes[i]; tv; tv = tv->next) {
317 if (CheckLock(&tv->lock))
318 afs_warn("volume at %p is locked\n", tv);
320 afs_warn("volume at %p is held\n", tv);
327 for (i = 0; i < NUSERS; i++) {
328 for (tu = afs_users[i]; tu; tu = tu->next) {
329 if (CheckLock(&tu->lock))
330 afs_warn("user at %p is locked\n", tu);
332 afs_warn("user at %lx is held\n", (unsigned long)tu);
343 AFS_STATCNT(afs_noop);
350 AFS_STATCNT(afs_badop);
351 osi_Panic("afs bad vnode op");
356 * afs_data_pointer_to_int32() - returns least significant afs_int32 of the
357 * given data pointer, without triggering "cast truncates pointer"
358 * warnings. We use this where we explicitly don't care whether a
359 * pointer is truncated -- it loses information where a pointer is
360 * larger than an afs_int32.
364 afs_data_pointer_to_int32(const void *p)
367 afs_int32 i32[sizeof(void *) / sizeof(afs_int32)];
371 int i32_sub; /* subscript of least significant afs_int32 in ip.i32[] */
376 /* used to determine the byte order of the system */
379 char c[sizeof(int) / sizeof(char)];
385 /* little-endian system */
388 /* big-endian system */
389 i32_sub = (sizeof ip.i32 / sizeof ip.i32[0]) - 1;
394 return ip.i32[i32_sub];
397 #ifdef AFS_LINUX20_ENV
398 static_inline afs_int32
399 afs_calc_inum_md5(afs_int32 cell, afs_int32 volume, afs_int32 vnode)
401 afs_int32 ino = 0, vno = vnode;
408 MD5_Update(&ct, &cell, 4);
409 MD5_Update(&ct, &volume, 4);
410 MD5_Update(&ct, &vnode, 4);
411 MD5_Final(digest, &ct);
413 /* Userspace may react oddly to an inode number of 0 or 1, so keep
414 * reading more of the md5 digest if we get back one of those.
415 * Make sure not to read beyond the end of the digest; if we somehow
416 * still have a 0, we will fall through to the non-md5 calculation. */
418 (ino == 0 || ino == 1) &&
419 offset + sizeof(ino) <= sizeof(digest);
422 memcpy(&ino, &digest[offset], sizeof(ino));
423 ino ^= (ino ^ vno) & 1;
424 ino &= 0x7fffffff; /* Assumes 32 bit ino_t ..... */
430 # define afs_calc_inum_md5(cell, volume, vnode) 0
434 afs_calc_inum(afs_int32 cell, afs_int32 volume, afs_int32 vnode)
438 ino = afs_calc_inum_md5(cell, volume, vnode);
440 if (ino == 0 || ino == 1) {
441 ino = (volume << 16) + vnode;
443 ino &= 0x7fffffff; /* Assumes 32 bit ino_t ..... */