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