79ca811438e97fd810571798beacca36eabbb40f
[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 "../afs/param.h"
15 #include "../afs/sysincludes.h"
16 #include "../afs/afsincludes.h"
17 #include "../h/unistd.h" /* For syscall numbers. */
18 #include "../h/mm.h"
19
20 #include <linux/module.h>
21
22
23
24 asmlinkage int (*sys_settimeofdayp)(struct timeval *tv, struct timezone *tz);
25 asmlinkage int (*sys_socketcallp)(int call, long *args);
26 asmlinkage int (*sys_killp)(int pid, int signal);
27 asmlinkage int (*sys_setgroupsp)(int gidsetsize, gid_t *grouplist);
28
29 extern void *sys_call_table[];
30 extern struct file_system_type afs_file_system;
31
32 static long get_page_offset(void);
33
34 struct semaphore afs_global_lock = MUTEX;
35 int afs_global_owner = 0;
36 unsigned long afs_linux_page_offset = 0; /* contains the PAGE_OFFSET value */
37
38 /* Since sys_ni_syscall is not exported, I need to cache it in order to restore
39  * it.
40  */
41 static void *afs_ni_syscall = NULL;
42
43 int init_module(void)
44 {
45     extern int afs_syscall();
46     extern int afs_xsetgroups();
47
48     /* obtain PAGE_OFFSET value */
49     afs_linux_page_offset = get_page_offset();
50
51     if (afs_linux_page_offset == 0) {
52         /* couldn't obtain page offset so can't continue */
53         printf("afs: Unable to obtain PAGE_OFFSET. Exiting..");
54         return -EIO;
55     }
56
57     /* Initialize pointers to kernel syscalls. */
58     sys_settimeofdayp = sys_call_table[__NR_settimeofday];
59     sys_socketcallp = sys_call_table[__NR_socketcall];
60     sys_killp = sys_call_table[__NR_kill];
61
62     /* setup AFS entry point. */
63     if (sys_call_table[__NR_afs_syscall] == afs_syscall) {
64         printf("AFS syscall entry point already in use!\n");
65         return -EBUSY;
66     }
67
68
69     afs_ni_syscall = sys_call_table[__NR_afs_syscall];
70     sys_call_table[__NR_afs_syscall] = afs_syscall;
71
72     osi_Init();
73     register_filesystem(&afs_file_system);
74
75     /* Intercept setgroups calls */
76     sys_setgroupsp = sys_call_table[__NR_setgroups];
77     sys_call_table[__NR_setgroups] = afs_xsetgroups;
78
79     return 0;
80 }
81
82 void cleanup_module(void)
83 {
84     struct task_struct *t;
85
86     sys_call_table[__NR_setgroups] = sys_setgroupsp;
87     sys_call_table[__NR_afs_syscall] = afs_ni_syscall;
88
89     unregister_filesystem(&afs_file_system);
90
91     osi_linux_free_inode_pages(); /* Invalidate all pages using AFS inodes. */
92     osi_linux_free_afs_memory();
93
94     return;
95 }
96
97 static long get_page_offset(void)
98 {
99     struct task_struct *p;
100
101     /* search backward thru the circular list */
102     for(p = current; p; p = p->prev_task)
103         if (p->pid == 1)
104             return p->addr_limit.seg;
105
106     return 0;
107 }