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