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