make-linux-proc-cellservdb-info-20050415
[openafs.git] / src / afs / LINUX / osi_module.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  * Linux module support routines.
12  *
13  */
14 #include <afsconfig.h>
15 #include "afs/param.h"
16
17 RCSID
18     ("$Header$");
19
20 #include <linux/module.h> /* early to avoid printf->printk mapping */
21 #include "afs/sysincludes.h"
22 #include "afsincludes.h"
23 #include "h/unistd.h"           /* For syscall numbers. */
24 #include "h/mm.h"
25
26 #ifdef AFS_AMD64_LINUX20_ENV
27 #include "../asm/ia32_unistd.h"
28 #endif
29
30 #include <linux/proc_fs.h>
31 #include <linux/slab.h>
32 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
33 #include <linux/init.h>
34 #include <linux/sched.h>
35 #endif
36
37 extern struct file_system_type afs_fs_type;
38
39 #if !defined(AFS_LINUX24_ENV)
40 static long get_page_offset(void);
41 #endif
42
43 #if defined(AFS_LINUX24_ENV)
44 DECLARE_MUTEX(afs_global_lock);
45 #else
46 struct semaphore afs_global_lock = MUTEX;
47 #endif
48 int afs_global_owner = 0;
49 #if !defined(AFS_LINUX24_ENV)
50 unsigned long afs_linux_page_offset = 0;        /* contains the PAGE_OFFSET value */
51 #endif
52
53 static inline int afs_ioctl(struct inode *, struct file *, unsigned int,
54                      unsigned long);
55 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
56 static long afs_unlocked_ioctl(struct file *, unsigned int, unsigned long);
57 #endif
58
59 static struct file_operations afs_syscall_fops = {
60 #ifdef HAVE_UNLOCKED_IOCTL
61     .unlocked_ioctl = afs_unlocked_ioctl,
62 #else
63     .ioctl = afs_ioctl,
64 #endif
65 #ifdef HAVE_COMPAT_IOCTL
66     .compat_ioctl = afs_unlocked_ioctl,
67 #endif
68 };
69
70 int
71 csdbproc_info(char *buffer, char **start, off_t offset, int
72 length)
73 {
74     int len = 0;
75     off_t pos = 0;
76     int cnt;
77     struct afs_q *cq, *tq;
78     struct cell *tc;
79     char tbuffer[16];
80     /* 90 - 64 cellname, 10 for 32 bit num and index, plus
81        decor */
82     char temp[91];
83     afs_uint32 addr;
84     
85     ObtainReadLock(&afs_xcell);
86
87     for (cq = CellLRU.next; cq != &CellLRU; cq = tq) {
88         tc = QTOC(cq); tq = QNext(cq);
89
90         pos += 90;
91
92         if (pos <= offset) {
93             len = 0;
94         } else {
95             sprintf(temp, ">%s #(%d/%d)\n", tc->cellName, 
96                     tc->cellNum, tc->cellIndex);
97             sprintf(buffer + len, "%-89s\n", temp);
98             len += 90;
99             if (pos >= offset+length) {
100                 ReleaseReadLock(&afs_xcell);
101                 goto done;
102             }
103         }
104
105         for (cnt = 0; cnt < MAXCELLHOSTS; cnt++) {
106             if (!tc->cellHosts[cnt]) break;
107             pos += 90;
108             if (pos <= offset) {
109                 len = 0;
110             } else {
111                 addr = ntohl(tc->cellHosts[cnt]->addr->sa_ip);
112                 sprintf(tbuffer, "%d.%d.%d.%d", 
113                         (int)((addr>>24) & 0xff),
114 (int)((addr>>16) & 0xff),
115                         (int)((addr>>8)  & 0xff), (int)( addr & 0xff));
116                 sprintf(temp, "%s #%s%s\n", tbuffer, tbuffer);
117                 sprintf(buffer + len, "%-89s\n", temp);
118                 len += 90;
119                 if (pos >= offset+length) {
120                     ReleaseReadLock(&afs_xcell);
121                     goto done;
122                 }
123             }
124         }
125     }
126
127     ReleaseReadLock(&afs_xcell);
128     
129 done:
130     *start = buffer + len - (pos - offset);
131     len = pos - offset;
132     if (len > length)
133         len = length;
134     return len;
135 }
136
137
138 int
139 csdbproc_read(char *buffer, char **start, off_t offset, int count,
140               int *eof, void *data)
141 {
142     int len, j;
143     struct afs_q *cq, *tq;
144     struct cell *tc;
145     char tbuffer[16];
146     afs_uint32 addr;
147     
148     len = 0;
149     ObtainReadLock(&afs_xcell);
150     for (cq = CellLRU.next; cq != &CellLRU; cq = tq) {
151         tc = QTOC(cq); tq = QNext(cq);
152         len += sprintf(buffer + len, ">%s #(%d/%d)\n", tc->cellName, 
153                        tc->cellNum, tc->cellIndex);
154         for (j = 0; j < MAXCELLHOSTS; j++) {
155             if (!tc->cellHosts[j]) break;
156             addr = ntohl(tc->cellHosts[j]->addr->sa_ip);
157             sprintf(tbuffer, "%d.%d.%d.%d", 
158                     (int)((addr>>24) & 0xff), (int)((addr>>16) & 0xff),
159                     (int)((addr>>8)  & 0xff), (int)( addr      & 0xff));
160             len += sprintf(buffer + len, "%s #%s\n", tbuffer, tbuffer);
161         }
162     }
163     ReleaseReadLock(&afs_xcell);
164     
165     if (offset >= len) {
166         *start = buffer;
167         *eof = 1;
168         return 0;
169     }
170     *start = buffer + offset;
171     if ((len -= offset) > count)
172         return count;
173     *eof = 1;
174     return len;
175 }
176
177 int
178 peerproc_read(char *buffer, char **start, off_t offset, int count,
179               int *eof, void *data)
180 {
181     int len, i, j;
182     struct rx_peer *sep;
183     
184     len = 0;
185     for (i = 0, j = 0; i < 256; i++) {
186         for (sep = rx_peerHashTable[i]; sep; sep = sep->next, j++) {
187             len += sprintf(buffer + len, "%lx: next=0x%lx, host=0x%x, ", (unsigned long)sep,
188                    (unsigned long)sep->next, sep->host);
189             len += sprintf(buffer + len, "ifMTU=%d, natMTU=%d, maxMTU=%d\n", sep->ifMTU,
190                    sep->natMTU, sep->maxMTU);
191             len += sprintf(buffer + len, "\trtt=%d:%d, timeout(%d:%d), nSent=%d, reSends=%d\n",
192                    sep->rtt, sep->rtt_dev, sep->timeout.sec,
193                    sep->timeout.usec, sep->nSent, sep->reSends);
194             len += sprintf(buffer + len, "\trefCount=%d, port=%d, idleWhen=0x%x\n",
195                    sep->refCount, sep->port, sep->idleWhen);
196             len += sprintf(buffer + len, "\tCongestionQueue (0x%lx:0x%lx), inPacketSkew=0x%x, outPacketSkew=0x%x\n",
197                  (unsigned long)sep->congestionQueue.prev, (unsigned long)sep->congestionQueue.next,
198                  sep->inPacketSkew, sep->outPacketSkew);
199 #ifdef RX_ENABLE_LOCKS
200             len += sprintf(buffer + len, "\tpeer_lock=%d\n", sep->peer_lock);
201 #endif /* RX_ENABLE_LOCKS */
202         }
203     }
204     
205     if (offset >= len) {
206         *start = buffer;
207         *eof = 1;
208         return 0;
209     }
210     *start = buffer + offset;
211     if ((len -= offset) > count)
212         return count;
213     *eof = 1;
214     return len;
215 }
216
217 int
218 rxstatsproc_read(char *buffer, char **start, off_t offset, int count,
219               int *eof, void *data)
220 {
221     int len, i;
222     
223     len = 0;
224     len += sprintf(buffer + len, "packetRequests = %d\n", rx_stats.packetRequests);
225     len += sprintf(buffer + len, "noPackets[%d] = %d\n", RX_PACKET_CLASS_RECEIVE,
226            rx_stats.receivePktAllocFailures);
227     len += sprintf(buffer + len, "noPackets[%d] = %d\n", RX_PACKET_CLASS_SEND,
228            rx_stats.sendPktAllocFailures);
229     len += sprintf(buffer + len, "noPackets[%d] = %d\n", RX_PACKET_CLASS_SPECIAL,
230            rx_stats.specialPktAllocFailures);
231     len += sprintf(buffer + len, "noPackets[%d] = %d\n", RX_PACKET_CLASS_RECV_CBUF,
232            rx_stats.receiveCbufPktAllocFailures);
233     len += sprintf(buffer + len, "noPackets[%d] = %d\n", RX_PACKET_CLASS_SEND_CBUF,
234            rx_stats.sendCbufPktAllocFailures);
235     len += sprintf(buffer + len, "socketGreedy = %d\n", rx_stats.socketGreedy);
236     len += sprintf(buffer + len, "bogusPacketOnRead = %d\n", rx_stats.bogusPacketOnRead);
237     len += sprintf(buffer + len, "bogusHost = %d\n", rx_stats.bogusHost);
238     len += sprintf(buffer + len, "noPacketOnRead = %d\n", rx_stats.noPacketOnRead);
239     len += sprintf(buffer + len, "noPacketBuffersOnRead = %d\n",
240            rx_stats.noPacketBuffersOnRead);
241     len += sprintf(buffer + len, "selects = %d\n", rx_stats.selects);
242     len += sprintf(buffer + len, "sendSelects = %d\n", rx_stats.sendSelects);
243     for (i = 0; i < RX_N_PACKET_TYPES; i++)
244         len += sprintf(buffer + len, "packetsRead[%d] = %d\n", i, rx_stats.packetsRead[i]);
245     len += sprintf(buffer + len, "dataPacketsRead = %d\n", rx_stats.dataPacketsRead);
246     len += sprintf(buffer + len, "ackPacketsRead = %d\n", rx_stats.ackPacketsRead);
247     len += sprintf(buffer + len, "dupPacketsRead = %d\n", rx_stats.dupPacketsRead);
248     len += sprintf(buffer + len, "spuriousPacketsRead = %d\n", rx_stats.spuriousPacketsRead);
249     for (i = 0; i < RX_N_PACKET_TYPES; i++)
250         len += sprintf(buffer + len, "packetsSent[%d] = %d\n", i, rx_stats.packetsSent[i]);
251     len += sprintf(buffer + len, "ackPacketsSent = %d\n", rx_stats.ackPacketsSent);
252     len += sprintf(buffer + len, "pingPacketsSent = %d\n", rx_stats.pingPacketsSent);
253     len += sprintf(buffer + len, "abortPacketsSent = %d\n", rx_stats.abortPacketsSent);
254     len += sprintf(buffer + len, "busyPacketsSent = %d\n", rx_stats.busyPacketsSent);
255     len += sprintf(buffer + len, "dataPacketsSent = %d\n", rx_stats.dataPacketsSent);
256     len += sprintf(buffer + len, "dataPacketsReSent = %d\n", rx_stats.dataPacketsReSent);
257     len += sprintf(buffer + len, "dataPacketsPushed = %d\n", rx_stats.dataPacketsPushed);
258     len += sprintf(buffer + len, "ignoreAckedPacket = %d\n", rx_stats.ignoreAckedPacket);
259     len += sprintf(buffer + len, "totalRtt = %d sec, %d usec\n", rx_stats.totalRtt.sec,
260            rx_stats.totalRtt.usec);
261     len += sprintf(buffer + len, "minRtt = %d sec, %d usec\n", rx_stats.minRtt.sec,
262            rx_stats.minRtt.usec);
263     len += sprintf(buffer + len, "maxRtt = %d sec, %d usec\n", rx_stats.maxRtt.sec,
264            rx_stats.maxRtt.usec);
265     len += sprintf(buffer + len, "nRttSamples = %d\n", rx_stats.nRttSamples);
266     len += sprintf(buffer + len, "nServerConns = %d\n", rx_stats.nServerConns);
267     len += sprintf(buffer + len, "nClientConns = %d\n", rx_stats.nClientConns);
268     len += sprintf(buffer + len, "nPeerStructs = %d\n", rx_stats.nPeerStructs);
269     len += sprintf(buffer + len, "nCallStructs = %d\n", rx_stats.nCallStructs);
270     len += sprintf(buffer + len, "nFreeCallStructs = %d\n", rx_stats.nFreeCallStructs);
271     len += sprintf(buffer + len, "netSendFailures  = %d\n", rx_stats.netSendFailures);
272     len += sprintf(buffer + len, "fatalErrors      = %d\n", rx_stats.fatalErrors);
273     
274     if (offset >= len) {
275         *start = buffer;
276         *eof = 1;
277         return 0;
278     }
279     *start = buffer + offset;
280     if ((len -= offset) > count)
281         return count;
282     *eof = 1;
283     return len;
284 }
285
286 int
287 rxproc_read(char *buffer, char **start, off_t offset, int count,
288               int *eof, void *data)
289 {
290     int len, i;
291     
292     len = 0;
293     len += sprintf(buffer + len, "rx_extraQuota = %d\n", rx_extraQuota);
294     len += sprintf(buffer + len, "rx_extraPackets = %d\n", rx_extraPackets);
295     len += sprintf(buffer + len, "rx_stackSize = %d\n", rx_stackSize);
296     len += sprintf(buffer + len, "rx_connDeadTime = %d\n", rx_connDeadTime);
297     len += sprintf(buffer + len, "rx_idleConnectionTime = %d\n", rx_idleConnectionTime);
298     len += sprintf(buffer + len, "rx_idlePeerTime = %d\n", rx_idlePeerTime);
299     len += sprintf(buffer + len, "rx_initSendWindow = %d\n", rx_initSendWindow);
300     len += sprintf(buffer + len, "rxi_nSendFrags = %d\n", rxi_nSendFrags);
301     len += sprintf(buffer + len, "rx_nPackets = %d\n", rx_nPackets);
302     len += sprintf(buffer + len, "rx_nFreePackets = %d\n", rx_nFreePackets);
303     len += sprintf(buffer + len, "rx_socket = 0x%lx\n", (unsigned long)rx_socket);
304     len += sprintf(buffer + len, "rx_Port = %d\n", rx_port);
305     for (i = 0; i < RX_N_PACKET_CLASSES; i++)
306         len += sprintf(buffer + len, "\trx_packetQuota[%d] = %d\n", i, rx_packetQuota[i]);
307
308     len += sprintf(buffer + len, "rx_nextCid = 0x%x\n", rx_nextCid);
309     len += sprintf(buffer + len, "rx_epoch = 0u%u\n", rx_epoch);
310     len += sprintf(buffer + len, "rx_waitingForPackets = %x\n", rx_waitingForPackets);
311     len += sprintf(buffer + len, "rxi_nCalls = %d\n", rxi_nCalls);
312     len += sprintf(buffer + len, "rxi_dataQuota = %d\n", rxi_dataQuota);
313     len += sprintf(buffer + len, "rxi_availProcs = %d\n", rxi_availProcs);
314     len += sprintf(buffer + len, "rxi_totalMin = %d\n", rxi_totalMin);
315     len += sprintf(buffer + len, "rxi_minDeficit = %d\n", rxi_minDeficit);
316
317     len += sprintf(buffer + len, "rxevent_nFree = %d\nrxevent_nPosted = %d\n", rxevent_nFree, rxevent_nPosted);
318     
319     if (offset >= len) {
320         *start = buffer;
321         *eof = 1;
322         return 0;
323     }
324     *start = buffer + offset;
325     if ((len -= offset) > count)
326         return count;
327     *eof = 1;
328     return len;
329 }
330
331 int
332 connproc_read(char *buffer, char **start, off_t offset, int count,
333               int *eof, void *data)
334 {
335     int len, i, j;
336     struct rx_connection *sep;
337     
338     len = 0;
339     for (i = 0, j = 0; i < 256; i++) {
340         for (sep = rx_connHashTable[i]; sep; sep = sep->next, j++) {
341             len += sprintf(buffer + len, "%lx: next=0x%lx, peer=0x%lx, epoch=0x%x, cid=0x%x, ackRate=%d\n",
342                            (unsigned long)sep, (unsigned long)sep->next, (unsigned long)sep->peer,
343                            sep->epoch, sep->cid, sep->ackRate);
344             len += sprintf(buffer + len, "\tcall[%lx=%d, %lx=%d, %lx=%d, %lx=%d]\n", 
345                            (unsigned long)sep->call[0], sep->callNumber[0],
346                            (unsigned long)sep->call[1], sep->callNumber[1],
347                            (unsigned long)sep->call[2], sep->callNumber[2],
348                            (unsigned long)sep->call[3], sep->callNumber[3]);
349             len += sprintf(buffer + len, "\ttimeout=%d, flags=0x%x, type=0x%x, serviceId=%d, service=0x%lx, refCount=%d\n",
350                            sep->timeout, sep->flags, sep->type, 
351                            sep->serviceId, (unsigned long)sep->service, sep->refCount);
352             len += sprintf(buffer + len, "\tserial=%d, lastSerial=%d, secsUntilDead=%d, secsUntilPing=%d, secIndex=%d\n",
353                            sep->serial, sep->lastSerial, sep->secondsUntilDead,
354                            sep->secondsUntilPing, sep->securityIndex);
355             len += sprintf(buffer + len, "\terror=%d, secObject=0x%lx, secData=0x%lx, secHeaderSize=%d, secmaxTrailerSize=%d\n",
356                            sep->error, (unsigned long)sep->securityObject,
357                            (unsigned long)sep->securityData,
358                            sep->securityHeaderSize, sep->securityMaxTrailerSize);
359             len += sprintf(buffer + len, "\tchallEvent=0x%lx, lastSendTime=0x%x, maxSerial=%d, hardDeadTime=%d\n",
360                            (unsigned long)sep->challengeEvent, sep->lastSendTime, 
361                            sep->maxSerial, sep->hardDeadTime);
362             if (sep->flags & RX_CONN_MAKECALL_WAITING)
363                 len += sprintf(buffer + len, "\t***** Conn in RX_CONN_MAKECALL_WAITING state *****\n");
364 #ifdef RX_ENABLE_LOCKS
365             len += sprintf(buffer + len, "\tcall_lock=%d, call_cv=%d, data_lock=%d, refCount=%d\n",
366                            sep->conn_call_lock, sep->conn_call_cv, 
367                            sep->conn_data_lock, sep->refCount);
368 #endif /* RX_ENABLE_LOCKS */
369         }
370     }
371     
372     if (offset >= len) {
373         *start = buffer;
374         *eof = 1;
375         return 0;
376     }
377     *start = buffer + offset;
378     if ((len -= offset) > count)
379         return count;
380     *eof = 1;
381     return len;
382 }
383
384 int
385 servicesproc_read(char *buffer, char **start, off_t offset, int count,
386               int *eof, void *data)
387 {
388     int len, i, j;
389     struct rx_service *sentry;
390     
391     len = 0;
392     for (i = 0, j = 0; i < RX_MAX_SERVICES; i++) {
393         if ((sentry = rx_services[i])) {
394             j++;
395             len += sprintf(buffer + len,
396                            "\t%lx: serviceId=%d, port=%d, serviceName=%s, socket=0x%lx\n",
397                            (unsigned long)sentry, sentry->serviceId, sentry->servicePort,
398                            sentry->serviceName, (unsigned long)sentry->socket);
399             len += sprintf(buffer + len,
400                            "\t\tnSecObj=%d, nReqRunning=%d, maxProcs=%d, minProcs=%d, connDeadTime=%d, idleDeadTime=%d\n",
401                            sentry->nSecurityObjects, sentry->nRequestsRunning,
402                            sentry->maxProcs, sentry->minProcs, 
403                            sentry->connDeadTime, sentry->idleDeadTime);
404         }
405     }
406     
407     if (offset >= len) {
408         *start = buffer;
409         *eof = 1;
410         return 0;
411     }
412     *start = buffer + offset;
413     if ((len -= offset) > count)
414         return count;
415     *eof = 1;
416     return len;
417 }
418
419 int
420 callproc_read(char *buffer, char **start, off_t offset, int count,
421               int *eof, void *data)
422 {
423     int len, i, j, k;
424     struct rx_connection *sep;
425     
426     len = 0;
427     for (i = 0, j = 0; i < 256; i++) {
428         for (sep = rx_connHashTable[i]; sep; sep = sep->next) {
429             for (k = 0; k < RX_MAXCALLS; k++) {
430                 struct rx_call *call = sep->call[k];
431                 if (call) {
432                     j++;
433                     len += sprintf(buffer + len,
434                                    "%lx: conn=0x%lx, qiheader(0x%lx:0x%lx), tq(0x%lx:0x%lx), rq(0x%lx:0x%lx)\n",
435                                    (unsigned long)call, (unsigned long)call->conn,
436                                    (unsigned long)call->queue_item_header.prev,
437                                    (unsigned long)call->queue_item_header.next,
438                                    (unsigned long)call->tq.prev, (unsigned long)call->tq.next,
439                                    (unsigned long)call->rq.prev, (unsigned long)call->rq.next);
440                     len += sprintf(buffer + len, 
441                                    "\t: curvec=%d, curpos=%lx, nLeft=%d, nFree=%d, currPacket=0x%lx, callNumber=0x%lx\n",
442                                    call->curvec, (unsigned long)call->curpos, call->nLeft,
443                                    call->nFree, (unsigned long)call->currentPacket,
444                                    (unsigned long)call->callNumber);
445                     len += sprintf(buffer + len,
446                                    "\t: channel=%d, state=0x%x, mode=0x%x, flags=0x%x, localStatus=0x%x, remStatus=0x%x\n",
447                                    call->channel, call->state, call->mode,
448                                    call->flags, call->localStatus,
449                                    call->remoteStatus);
450                     len += sprintf(buffer + len,
451                                    "\t: error=%d, timeout=0x%x, rnext=0x%x, rprev=0x%x, rwind=0x%x, tfirst=0x%x, tnext=0x%x\n",
452                                    call->error, call->timeout, call->rnext,
453                                    call->rprev, call->rwind, call->tfirst,
454                                    call->tnext);
455                     len += sprintf(buffer + len,
456                                    "\t: twind=%d, resendEvent=0x%lx, timeoutEvent=0x%lx, keepAliveEvent=0x%lx, delayedAckEvent=0x%lx\n",
457                                    call->twind, (unsigned long)call->resendEvent,
458                                    (unsigned long)call->timeoutEvent,
459                                    (unsigned long)call->keepAliveEvent,
460                                    (unsigned long)call->delayedAckEvent);
461                     len += sprintf(buffer + len,
462                                    "\t: lastSendTime=0x%x, lastReceiveTime=0x%x, lastAcked=0x%x, startTime=0x%x, startWait=0x%x\n",
463                                    call->lastSendTime, call->lastReceiveTime,
464                                    call->lastAcked, call->startTime.sec,
465                                    call->startWait);
466                     if (call->flags & RX_CALL_WAIT_PROC)
467                         len += sprintf(buffer + len,
468                                        "\t******** Call in RX_CALL_WAIT_PROC state **********\n");
469                     if (call->flags & RX_CALL_WAIT_WINDOW_ALLOC)
470                         len += sprintf(buffer + len,
471                                        "\t******** Call in RX_CALL_WAIT_WINDOW_ALLOC state **********\n");
472                     if (call->flags & RX_CALL_READER_WAIT)
473                         len += sprintf(buffer + len,
474                                        "\t******** Conn in RX_CALL_READER_WAIT state **********\n");
475                     if (call->flags & RX_CALL_WAIT_PACKETS)
476                         len += sprintf(buffer + len,
477                                        "\t******** Conn in RX_CALL_WAIT_PACKETS state **********\n");
478 #ifdef RX_ENABLE_LOCKS
479                     len += sprintf(buffer + len,
480                                    "\t: lock=0x%x, cv_twind=0x%x, cv_rq=0x%x, refCount= %d\n",
481                                    call->lock, call->cv_twind, call->cv_rq,
482                                    call->refCount);
483 #endif /* RX_ENABLE_LOCKS */
484                     len += sprintf(buffer + len, "\t: MTU=%d\n", call->MTU);
485                 }
486             }
487         }
488     }
489     
490     if (offset >= len) {
491         *start = buffer;
492         *eof = 1;
493         return 0;
494     }
495     *start = buffer + offset;
496     if ((len -= offset) > count)
497         return count;
498     *eof = 1;
499     return len;
500 }
501
502 static struct proc_dir_entry *openafs_procfs;
503 #if defined(NEED_IOCTL32) && !defined(HAVE_COMPAT_IOCTL)
504 static int ioctl32_done;
505 #endif
506
507 #ifdef AFS_LINUX24_ENV
508 static int
509 afsproc_init(void)
510 {
511     struct proc_dir_entry *entry1;
512     struct proc_dir_entry *entry;
513
514     openafs_procfs = proc_mkdir(PROC_FSDIRNAME, proc_root_fs);
515     entry1 = create_proc_entry(PROC_SYSCALL_NAME, 0666, openafs_procfs);
516
517     entry1->proc_fops = &afs_syscall_fops;
518
519     entry1->owner = THIS_MODULE;
520
521     entry = create_proc_info_entry(PROC_CELLSERVDB_NAME, (S_IFREG|S_IRUGO), openafs_procfs, csdbproc_info);
522
523     entry = create_proc_read_entry(PROC_PEER_NAME, (S_IFREG|S_IRUGO), openafs_procfs, peerproc_read, NULL);
524
525     entry = create_proc_read_entry(PROC_CONN_NAME, (S_IFREG|S_IRUGO), openafs_procfs, connproc_read, NULL);
526
527     entry = create_proc_read_entry(PROC_CALL_NAME, (S_IFREG|S_IRUGO), openafs_procfs, connproc_read, NULL);
528
529     entry = create_proc_read_entry(PROC_RX_NAME, (S_IFREG|S_IRUGO), openafs_procfs, rxproc_read, NULL);
530
531     entry = create_proc_read_entry(PROC_SERVICES_NAME, (S_IFREG|S_IRUGO), openafs_procfs, servicesproc_read, NULL);
532
533     entry = create_proc_read_entry(PROC_RXSTATS_NAME, (S_IFREG|S_IRUGO), openafs_procfs, rxstatsproc_read, NULL);
534 #if defined(NEED_IOCTL32) && !defined(HAVE_COMPAT_IOCTL)
535     if (register_ioctl32_conversion(VIOC_SYSCALL32, NULL) == 0) 
536             ioctl32_done = 1;
537 #endif
538
539     return 0;
540 }
541
542 static void
543 afsproc_exit(void)
544 {
545     remove_proc_entry(PROC_RXSTATS_NAME, openafs_procfs);
546     remove_proc_entry(PROC_SERVICES_NAME, openafs_procfs);
547     remove_proc_entry(PROC_RX_NAME, openafs_procfs);
548     remove_proc_entry(PROC_CALL_NAME, openafs_procfs);
549     remove_proc_entry(PROC_CONN_NAME, openafs_procfs);
550     remove_proc_entry(PROC_PEER_NAME, openafs_procfs);
551     remove_proc_entry(PROC_CELLSERVDB_NAME, openafs_procfs);
552     remove_proc_entry(PROC_SYSCALL_NAME, openafs_procfs);
553     remove_proc_entry(PROC_FSDIRNAME, proc_root_fs);
554 #if defined(NEED_IOCTL32) && !defined(HAVE_COMPAT_IOCTL)
555     if (ioctl32_done)
556             unregister_ioctl32_conversion(VIOC_SYSCALL32);
557 #endif
558 }
559 #endif
560
561 extern asmlinkage long
562 afs_syscall(long syscall, long parm1, long parm2, long parm3, long parm4);
563
564 static int
565 afs_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
566           unsigned long arg)
567 {
568
569     struct afsprocdata sysargs;
570 #ifdef NEED_IOCTL32
571     struct afsprocdata32 sysargs32;
572 #endif
573
574     if (cmd != VIOC_SYSCALL && cmd != VIOC_SYSCALL32) return -EINVAL;
575
576 #ifdef NEED_IOCTL32
577 #ifdef AFS_SPARC64_LINUX24_ENV
578     if (current->thread.flags & SPARC_FLAG_32BIT)
579 #elif defined(AFS_SPARC64_LINUX20_ENV)
580     if (current->tss.flags & SPARC_FLAG_32BIT)
581 #elif defined(AFS_AMD64_LINUX20_ENV)
582 #ifdef AFS_LINUX26_ENV
583     if (test_thread_flag(TIF_IA32))
584 #else
585     if (current->thread.flags & THREAD_IA32)
586 #endif
587 #elif defined(AFS_PPC64_LINUX20_ENV)
588 #ifdef AFS_PPC64_LINUX26_ENV
589     if (current->thread_info->flags & _TIF_32BIT)
590 #else /*Linux 2.6 */
591     if (current->thread.flags & PPC_FLAG_32BIT)
592 #endif
593 #elif defined(AFS_S390X_LINUX26_ENV)
594     if (test_thread_flag(TIF_31BIT))
595 #elif defined(AFS_S390X_LINUX20_ENV)
596     if (current->thread.flags & S390_FLAG_31BIT)
597 #else
598 #error Not done for this linux type
599 #endif
600     {
601         if (copy_from_user(&sysargs32, (void *)arg,
602                            sizeof(struct afsprocdata32)))
603             return -EFAULT;
604
605         return afs_syscall((unsigned long)sysargs32.syscall,
606                            (unsigned long)sysargs32.param1,
607                            (unsigned long)sysargs32.param2,
608                            (unsigned long)sysargs32.param3,
609                            (unsigned long)sysargs32.param4);
610     } else
611 #endif
612     {
613         if (copy_from_user(&sysargs, (void *)arg, sizeof(struct afsprocdata)))
614             return -EFAULT;
615
616         return afs_syscall(sysargs.syscall, sysargs.param1,
617                            sysargs.param2, sysargs.param3, sysargs.param4);
618     }
619 }
620
621 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
622 static long afs_unlocked_ioctl(struct file *file, unsigned int cmd,
623                                unsigned long arg) {
624     return afs_ioctl(FILE_INODE(file), file, cmd, arg);
625 }
626 #endif
627
628 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
629 int __init
630 afs_init(void)
631 #else
632 int
633 init_module(void)
634 #endif
635 {
636     int e;
637     RWLOCK_INIT(&afs_xosi, "afs_xosi");
638
639 #if !defined(AFS_LINUX24_ENV)
640     /* obtain PAGE_OFFSET value */
641     afs_linux_page_offset = get_page_offset();
642
643 #ifndef AFS_S390_LINUX22_ENV
644     if (afs_linux_page_offset == 0) {
645         /* couldn't obtain page offset so can't continue */
646         printf("afs: Unable to obtain PAGE_OFFSET. Exiting..");
647         return -EIO;
648     }
649 #endif /* AFS_S390_LINUX22_ENV */
650 #endif /* !defined(AFS_LINUX24_ENV) */
651
652     osi_Init();
653
654     e = osi_syscall_init();
655     if (e) return e;
656     register_filesystem(&afs_fs_type);
657     osi_sysctl_init();
658 #ifdef AFS_LINUX24_ENV
659     afsproc_init();
660 #endif
661
662     return 0;
663 }
664
665 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
666 void __exit
667 afs_cleanup(void)
668 #else
669 void
670 cleanup_module(void)
671 #endif
672 {
673     osi_sysctl_clean();
674     osi_syscall_clean();
675     unregister_filesystem(&afs_fs_type);
676
677     osi_linux_free_inode_pages();       /* Invalidate all pages using AFS inodes. */
678     osi_linux_free_afs_memory();
679
680 #ifdef AFS_LINUX24_ENV
681     afsproc_exit();
682 #endif
683     return;
684 }
685
686 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
687 module_init(afs_init);
688 module_exit(afs_cleanup);
689 #endif
690
691
692 #if !defined(AFS_LINUX24_ENV)
693 static long
694 get_page_offset(void)
695 {
696 #if defined(AFS_PPC_LINUX22_ENV) || defined(AFS_SPARC64_LINUX20_ENV) || defined(AFS_SPARC_LINUX20_ENV) || defined(AFS_ALPHA_LINUX20_ENV) || defined(AFS_S390_LINUX22_ENV) || defined(AFS_IA64_LINUX20_ENV) || defined(AFS_PARISC_LINUX24_ENV) || defined(AFS_AMD64_LINUX20_ENV) || defined(AFS_PPC64_LINUX20_ENV)
697     return PAGE_OFFSET;
698 #else
699     struct task_struct *p, *q;
700
701     /* search backward thru the circular list */
702 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
703     read_lock(&tasklist_lock);
704 #endif
705     /* search backward thru the circular list */
706 #ifdef DEFINED_PREV_TASK
707     for (q = current; p = q; q = prev_task(p)) {
708 #else
709     for (p = current; p; p = p->prev_task) {
710 #endif
711         if (p->pid == 1) {
712 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
713             read_unlock(&tasklist_lock);
714 #endif
715             return p->addr_limit.seg;
716         }
717     }
718
719 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
720     read_unlock(&tasklist_lock);
721 #endif
722     return 0;
723 #endif
724 }
725 #endif /* !AFS_LINUX24_ENV */