7947b5a13eb45cd39cea1d4b3ac9b5236f7a73e5
[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 #ifdef AFS_SPARC64_LINUX24_ENV
24 #define __NR_setgroups32      82 /* This number is not exported for some bizarre reason. */
25 #endif
26
27 asmlinkage int (*sys_settimeofdayp)(struct timeval *tv, struct timezone *tz);
28 #if !defined(AFS_ALPHA_LINUX20_ENV)
29 asmlinkage int (*sys_socketcallp)(int call, long *args);
30 #endif /* no socketcall on alpha */
31 asmlinkage int (*sys_killp)(int pid, int signal);
32 asmlinkage int (*sys_setgroupsp)(int gidsetsize, gid_t *grouplist);
33
34 #ifdef AFS_SPARC64_LINUX20_ENV
35 extern unsigned int sys_call_table[];  /* changed to uint because SPARC64 has syscaltable of 32bit items */
36 #else
37 extern void * sys_call_table[]; /* safer for other linuces */
38 #endif
39 extern struct file_system_type afs_file_system;
40
41 static long get_page_offset(void);
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 unsigned long afs_linux_page_offset = 0; /* contains the PAGE_OFFSET value */
50
51 /* Since sys_ni_syscall is not exported, I need to cache it in order to restore
52  * it.
53  */
54 #ifdef AFS_SPARC64_LINUX20_ENV
55 static unsigned int afs_ni_syscall = 0;
56 #else
57 static void* afs_ni_syscall = 0;
58 #endif
59  
60 #ifdef AFS_SPARC64_LINUX20_ENV
61 static unsigned int afs_ni_syscall32 = 0;
62 asmlinkage int (*sys32_setgroupsp)(int gidsetsize, __kernel_gid_t32 *grouplist);
63 #if defined(__NR_setgroups32)
64 asmlinkage int (*sys32_setgroups32p)(int gidsetsize, __kernel_gid_t32 *grouplist);
65 #endif
66 extern unsigned int sys_call_table32[];
67
68 asmlinkage int afs_syscall32(long syscall, long parm1, long parm2, long parm3,
69                              long parm4, long parm5)
70 {
71 __asm__ __volatile__ ("
72         srl %o4, 0, %o4
73         mov %o7, %i7
74         call afs_syscall
75         srl %o5, 0, %o5
76         ret
77         nop
78 ");
79 }
80 #endif
81
82 #ifdef AFS_LINUX24_ENV
83 asmlinkage int (*sys_setgroups32p)(int gidsetsize, __kernel_gid32_t *grouplist);
84 #endif 
85
86 #ifdef AFS_SPARC64_LINUX20_ENV
87 #define POINTER2SYSCALL (unsigned int)(unsigned long)
88 #define SYSCALL2POINTER (void *)(long)
89 #else
90 #define POINTER2SYSCALL (void *)
91 #define SYSCALL2POINTER (void *)
92 #endif
93
94 int init_module(void)
95 {
96     extern int afs_syscall();
97     extern int afs_xsetgroups();
98 #if defined(__NR_setgroups32)
99     extern int afs_xsetgroups32();
100 #endif
101 #ifdef AFS_SPARC64_LINUX20_ENV
102     extern int afs32_xsetgroups();
103 #if defined(__NR_setgroups32)
104     extern int afs32_xsetgroups32();
105 #endif
106 #endif
107
108     /* obtain PAGE_OFFSET value */
109     afs_linux_page_offset = get_page_offset();
110
111 #ifndef AFS_S390_LINUX22_ENV
112     if (afs_linux_page_offset == 0) {
113         /* couldn't obtain page offset so can't continue */
114         printf("afs: Unable to obtain PAGE_OFFSET. Exiting..");
115         return -EIO;
116     }
117 #endif
118
119     /* Initialize pointers to kernel syscalls. */
120     sys_settimeofdayp = SYSCALL2POINTER sys_call_table[__NR_settimeofday];
121 #if !defined(AFS_ALPHA_LINUX20_ENV)
122     sys_socketcallp = SYSCALL2POINTER sys_call_table[__NR_socketcall];
123 #endif /* no socketcall on alpha */
124     sys_killp = SYSCALL2POINTER sys_call_table[__NR_kill];
125
126     /* setup AFS entry point. */
127     if (SYSCALL2POINTER sys_call_table[__NR_afs_syscall] == afs_syscall) {
128         printf("AFS syscall entry point already in use!\n");
129         return -EBUSY;
130     }
131
132
133     afs_ni_syscall = sys_call_table[__NR_afs_syscall];
134     sys_call_table[__NR_afs_syscall] = POINTER2SYSCALL afs_syscall;
135 #ifdef AFS_SPARC64_LINUX20_ENV
136     afs_ni_syscall32 = sys_call_table32[__NR_afs_syscall];
137     sys_call_table32[__NR_afs_syscall] = POINTER2SYSCALL afs_syscall32;
138 #endif
139
140     osi_Init();
141     register_filesystem(&afs_file_system);
142
143     /* Intercept setgroups calls */
144     sys_setgroupsp = SYSCALL2POINTER sys_call_table[__NR_setgroups];
145     sys_call_table[__NR_setgroups] = POINTER2SYSCALL afs_xsetgroups;
146 #ifdef AFS_SPARC64_LINUX20_ENV
147     sys32_setgroupsp = SYSCALL2POINTER sys_call_table32[__NR_setgroups];
148     sys_call_table32[__NR_setgroups] = POINTER2SYSCALL afs32_xsetgroups;
149 #endif
150 #if defined(__NR_setgroups32)
151     sys_setgroups32p = SYSCALL2POINTER sys_call_table[__NR_setgroups32];
152     sys_call_table[__NR_setgroups32] = POINTER2SYSCALL afs_xsetgroups32;
153 #ifdef AFS_SPARC64_LINUX20_ENV
154     sys32_setgroups32p = SYSCALL2POINTER sys_call_table32[__NR_setgroups32];
155     sys_call_table32[__NR_setgroups32] = POINTER2SYSCALL afs32_xsetgroups32;
156 #endif
157 #endif
158
159     return 0;
160 }
161
162 void cleanup_module(void)
163 {
164     struct task_struct *t;
165
166     sys_call_table[__NR_setgroups] = POINTER2SYSCALL sys_setgroupsp;
167     sys_call_table[__NR_afs_syscall] = afs_ni_syscall;
168 #ifdef AFS_SPARC64_LINUX20_ENV
169     sys_call_table32[__NR_setgroups] = POINTER2SYSCALL sys32_setgroupsp;
170     sys_call_table32[__NR_afs_syscall] = afs_ni_syscall32;
171 #endif
172 #if defined(__NR_setgroups32)
173     sys_call_table[__NR_setgroups32] = POINTER2SYSCALL sys_setgroups32p;
174 #ifdef AFS_SPARC64_LINUX20_ENV
175     sys_call_table32[__NR_setgroups32] = POINTER2SYSCALL sys32_setgroups32p;
176 #endif
177 #endif
178     unregister_filesystem(&afs_file_system);
179
180     osi_linux_free_inode_pages(); /* Invalidate all pages using AFS inodes. */
181     osi_linux_free_afs_memory();
182
183     return;
184 }
185
186 static long get_page_offset(void)
187 {
188 #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)
189     return PAGE_OFFSET;
190 #else
191     struct task_struct *p;
192
193     /* search backward thru the circular list */
194     for(p = current; p; p = p->prev_task)
195         if (p->pid == 1)
196             return p->addr_limit.seg;
197
198     return 0;
199 #endif
200 }