initial-solaris8-support-20001105
[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 "../afs/param.h"       /* Should be always first */
16 #include "../afs/stds.h"
17 #include "../afs/sysincludes.h" /* Standard vendor system headers */
18
19 #if !defined(UKERNEL)
20 #include <net/if.h>
21 #include <netinet/in.h>
22
23 #ifdef AFS_SGI62_ENV
24 #include "../h/hashing.h"
25 #endif
26 #if !defined(AFS_HPUX110_ENV) && !defined(AFS_LINUX20_ENV)
27 #include <netinet/in_var.h>
28 #endif /* ! AFS_HPUX110_ENV */
29 #endif /* !defined(UKERNEL) */
30
31 #include "../afs/afsincludes.h" /* Afs-based standard headers */
32 #include "../afs/afs_stats.h"   /* afs statistics */
33
34 #if     defined(AFS_SUN56_ENV)
35 #include <inet/led.h>
36 #include <inet/common.h>
37 #if     defined(AFS_SUN58_ENV)
38 #include <netinet/ip6.h>
39 #endif
40 #include <inet/ip.h>
41 #endif
42
43 #if     defined(AFS_AIX_ENV)
44 #include <sys/fp_io.h>
45 #endif
46
47 extern struct volume *afs_volumes[NVOLS];
48
49 char *afs_cv2string(char *ttp, afs_uint32 aval)
50 {
51     register char *tp = ttp;
52     register int  i;
53     int any;
54     
55     AFS_STATCNT(afs_cv2string);
56     any = 0;
57     *(--tp) = 0;
58     while (aval != 0) {
59         i = aval % 10;
60         *(--tp) = '0' + i;
61         aval /= 10;
62         any = 1;
63     }
64     if (!any)
65         *(--tp) = '0';
66     return tp;
67
68 } /*afs_cv2string*/
69
70 void print_internet_address(char *preamble, struct srvAddr *sa,
71                             char *postamble, int flag)
72 {
73     register struct server *aserver = sa->server;
74     char *ptr = "\n";
75     afs_uint32 address;
76     
77     AFS_STATCNT(print_internet_address);
78     address = ntohl(sa->sa_ip);
79     if (aserver->flags & SRVR_MULTIHOMED) {
80         if (flag == 1) {        /* server down mesg */
81             if (!(aserver->flags & SRVR_ISDOWN))
82                 ptr = " (multi-homed address; other same-host interfaces maybe up)\n";
83             else
84                 ptr = " (all multi-homed ip addresses down for the server)\n";
85         } else if (flag == 2) { /* server up mesg */
86             ptr = " (multi-homed address; other same-host interfaces may still be down)\n";
87         }
88     }
89     afs_warn("%s%d.%d.%d.%d in cell %s%s%s",
90            preamble, (address >> 24), (address >> 16) & 0xff, (address >> 8) & 0xff, (address) & 0xff,
91            aserver->cell->cellName, postamble, ptr);
92     afs_warnuser("%s%d.%d.%d.%d in cell %s%s%s",
93             preamble, (address >> 24), (address >> 16) & 0xff, (address >> 8) & 0xff, (address) & 0xff,
94             aserver->cell->cellName, postamble, ptr);
95
96 } /*print_internet_address*/
97
98
99
100 /* * * * * * *
101  * this code badly needs to be cleaned up...  too many ugly ifdefs.
102  * XXX
103  */
104 extern afs_int32 afs_showflags;
105
106 afs_warn(a,b,c,d,e,f,g,h,i,j)
107 char *a;
108 long b,c,d,e,f,g,h,i,j;
109 {
110     AFS_STATCNT(afs_warn);
111     
112     if (afs_showflags & GAGCONSOLE)
113     {
114 #if defined(AFS_AIX_ENV)
115         struct file *fd;
116
117         /* cf. console_printf() in oncplus/kernext/nfs/serv/shared.c */
118         if (fp_open("/dev/console",O_WRONLY|O_NOCTTY|O_NDELAY,
119                     0666,0,FP_SYS,&fd) == 0) {
120             char buf[1024];
121             ssize_t len;
122             ssize_t count;
123
124             sprintf(buf, a,b,c,d,e,f,g,h,i,j);
125             len = strlen(buf);
126             fp_write(fd, buf, len, 0, UIO_SYSSPACE, &count);
127             fp_close(fd);
128         }
129 #else
130         printf(a,b,c,d,e,f,g,h,i,j);
131 #endif
132     }
133 }
134
135 afs_warnuser(a,b,c,d,e,f,g,h,i,j)
136 char *a;
137 long b,c,d,e,f,g,h,i,j;
138 {
139     AFS_STATCNT(afs_warnuser);
140     if (afs_showflags & GAGUSER)
141     {
142 #ifdef AFS_GLOBAL_SUNLOCK
143         int haveGlock = ISAFS_GLOCK();
144         if (haveGlock)
145             AFS_GUNLOCK();
146 #endif /* AFS_GLOBAL_SUNLOCK */
147
148         uprintf(a,b,c,d,e,f,g,h,i,j);
149
150 #ifdef AFS_GLOBAL_SUNLOCK
151         if (haveGlock)
152             AFS_GLOCK();
153 #endif /* AFS_GLOBAL_SUNLOCK */
154     }
155 }
156
157
158 /* run everywhere, checking locks */
159 void afs_CheckLocks()
160
161 {
162     extern afs_rwlock_t afs_xconn, afs_xvolume, afs_xuser, afs_xcell;
163     extern afs_rwlock_t afs_xserver;
164     extern struct server *afs_servers[NSERVERS];
165     extern struct unixuser *afs_users[NUSERS];
166     extern unsigned char *afs_indexFlags;
167     register int i;
168
169     afs_warn("Looking for locked data structures.\n");
170     afs_warn("conn %x, volume %x, user %x, cell %x, server %x\n",
171             afs_xconn, afs_xvolume, afs_xuser, afs_xcell, afs_xserver);
172     {
173         register struct vcache *tvc;
174         AFS_STATCNT(afs_CheckLocks);
175
176         for(i=0;i<VCSIZE;i++) {
177             for(tvc = afs_vhashT[i]; tvc; tvc=tvc->hnext) {
178 #ifdef  AFS_OSF_ENV
179                 if (tvc->vrefCount > 1)
180 #else   /* AFS_OSF_ENV */
181                 if (tvc->vrefCount)
182 #endif
183                     afs_warn("Stat cache entry at %x is held\n", tvc);
184                 if (CheckLock(&tvc->lock))
185                     afs_warn("Stat entry at %x is locked\n", tvc);
186             }
187         }
188     }
189     {
190         register struct dcache *tdc;
191         for (i=0;i<afs_cacheFiles;i++) {
192             tdc = afs_indexTable[i];
193             if (tdc) {
194                 if (tdc->refCount)
195                     afs_warn("Disk entry %d at %x is held\n", i, tdc);
196             }
197             if (afs_indexFlags[i] & IFDataMod)
198                 afs_warn("Disk entry %d at %x has IFDataMod flag set.\n", i, tdc);
199         }
200     }
201     {
202        struct srvAddr *sa;
203        struct server *ts;
204        struct conn *tc;
205         for (i=0;i<NSERVERS;i++) {
206             for (ts = afs_servers[i]; ts; ts=ts->next) {
207                 if (ts->flags & SRVR_ISDOWN)
208                     printf("Server entry %x is marked down\n", ts);
209                 for (sa = ts->addr; sa; sa = sa->next_sa) {     
210                     for (tc = sa->conns; tc; tc=tc->next) {
211                        if (tc->refCount)
212                           afs_warn("conn at %x (server %x) is held\n", tc, sa->sa_ip);
213                     }
214                 }
215             }
216         }
217     }
218     {
219        struct volume *tv;
220         for (i=0;i<NVOLS;i++) {
221             for (tv = afs_volumes[i]; tv; tv=tv->next) {
222                 if (CheckLock(&tv->lock))
223                     afs_warn("volume at %x is locked\n", tv);
224                 if (tv->refCount)
225                     afs_warn("volume at %x is held\n", tv);
226             }
227         }
228     }
229     {
230        struct unixuser *tu;
231
232         for (i=0;i<NUSERS;i++) {
233             for (tu = afs_users[i]; tu; tu=tu->next) {
234                 if (tu->refCount) printf("user at %x is held\n", tu);
235             }
236         }
237     }
238     afs_warn("Done.\n");
239 }
240
241
242 int afs_noop() {
243     AFS_STATCNT(afs_noop);
244 #ifdef  AFS_OSF30_ENV
245     return (EOPNOTSUPP);
246 #else
247     return EINVAL;
248 #endif
249 }
250
251 int afs_badop() {
252     AFS_STATCNT(afs_badop);
253     osi_Panic("afs bad vnode op");
254     return 0;                   /* make SGI C compiler happy */
255 }
256
257 /*
258  * afs_data_pointer_to_int32() - returns least significant afs_int32 of the
259  * given data pointer, without triggering "cast truncates pointer"
260  * warnings.  We use this where we explicitly don't care whether a
261  * pointer is truncated -- it loses information where a pointer is
262  * larger than an afs_int32.
263  */
264
265 afs_int32
266 afs_data_pointer_to_int32(const void *p)
267 {
268         union {
269                 afs_int32  i32[sizeof(void *)/sizeof(afs_int32)];
270                 const void *p;
271         } ip;
272
273         int i32_sub;    /* subscript of least significant afs_int32 in ip.i32[] */
274
275         /* set i32_sub */
276
277         {
278                 /* used to determine the byte order of the system */
279
280                 union {
281                         char c[sizeof(int)/sizeof(char)];
282                         int  i;
283                 } ci;
284
285                 ci.i = 1;
286                 if (ci.c[0] == 1) {
287                         /* little-endian system */
288                         i32_sub = 0;
289                 } else {
290                         /* big-endian system */
291                         i32_sub = (sizeof ip.i32 / sizeof ip.i32[0]) - 1;
292                 }
293         }
294
295         ip.p = p;
296         return ip.i32[i32_sub];
297 }