sparc-linux-20001223
[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 unsigned int sys_call_table[];  /* changed to uint because SPARC64 has syscaltable of 32bit items */
30 extern struct file_system_type afs_file_system;
31
32 static long get_page_offset(void);
33
34 #if defined(AFS_LINUX24_ENV)
35 DECLARE_MUTEX(afs_global_lock);
36 #else
37 struct semaphore afs_global_lock = MUTEX;
38 #endif
39 int afs_global_owner = 0;
40 unsigned long afs_linux_page_offset = 0; /* contains the PAGE_OFFSET value */
41
42 /* Since sys_ni_syscall is not exported, I need to cache it in order to restore
43  * it.
44  */
45 static unsigned int afs_ni_syscall = 0;
46
47 #ifdef AFS_SPARC64_LINUX20_ENV
48 static unsigned int afs_ni_syscall32 = 0;
49 asmlinkage int (*sys_setgroupsp32)(int gidsetsize, __kernel_gid_t32 *grouplist);
50 extern unsigned int sys_call_table32[];
51
52 asmlinkage int afs_syscall32(long syscall, long parm1, long parm2, long parm3,
53                              long parm4, long parm5)
54 {
55 __asm__ __volatile__ ("
56         srl %o4, 0, %o4
57         mov %o7, %i7
58         call afs_syscall
59         srl %o5, 0, %o5
60         ret
61         nop
62 ");
63 }
64 #endif
65
66 #define POINTER2SYSCALL (unsigned int)(unsigned long)
67 #define SYSCALL2POINTER (void *)(long)
68
69 int init_module(void)
70 {
71     extern int afs_syscall();
72     extern int afs_xsetgroups();
73 #ifdef AFS_SPARC64_LINUX20_ENV
74     extern int afs_xsetgroups32();
75 #endif
76
77     /* obtain PAGE_OFFSET value */
78     afs_linux_page_offset = get_page_offset();
79
80     if (afs_linux_page_offset == 0) {
81         /* couldn't obtain page offset so can't continue */
82         printf("afs: Unable to obtain PAGE_OFFSET. Exiting..");
83         return -EIO;
84     }
85
86     /* Initialize pointers to kernel syscalls. */
87     sys_settimeofdayp = SYSCALL2POINTER sys_call_table[__NR_settimeofday];
88     sys_socketcallp = SYSCALL2POINTER sys_call_table[__NR_socketcall];
89     sys_killp = SYSCALL2POINTER sys_call_table[__NR_kill];
90
91     /* setup AFS entry point. */
92     if (SYSCALL2POINTER sys_call_table[__NR_afs_syscall] == afs_syscall) {
93         printf("AFS syscall entry point already in use!\n");
94         return -EBUSY;
95     }
96
97
98     afs_ni_syscall = sys_call_table[__NR_afs_syscall];
99     sys_call_table[__NR_afs_syscall] = POINTER2SYSCALL afs_syscall;
100 #ifdef AFS_SPARC64_LINUX20_ENV
101     afs_ni_syscall32 = sys_call_table32[__NR_afs_syscall];
102     sys_call_table32[__NR_afs_syscall] = POINTER2SYSCALL afs_syscall32;
103 #endif
104
105     osi_Init();
106     register_filesystem(&afs_file_system);
107
108     /* Intercept setgroups calls */
109     sys_setgroupsp = SYSCALL2POINTER sys_call_table[__NR_setgroups];
110     sys_call_table[__NR_setgroups] = POINTER2SYSCALL afs_xsetgroups;
111 #ifdef AFS_SPARC64_LINUX20_ENV
112     sys_setgroupsp32 = SYSCALL2POINTER sys_call_table32[__NR_setgroups];
113     sys_call_table32[__NR_setgroups] = POINTER2SYSCALL afs_xsetgroups32;
114 #endif
115
116     return 0;
117 }
118
119 void cleanup_module(void)
120 {
121     struct task_struct *t;
122
123     sys_call_table[__NR_setgroups] = POINTER2SYSCALL sys_setgroupsp;
124     sys_call_table[__NR_afs_syscall] = afs_ni_syscall;
125 #ifdef AFS_SPARC64_LINUX20_ENV
126     sys_call_table32[__NR_setgroups] = POINTER2SYSCALL sys_setgroupsp32;
127     sys_call_table32[__NR_afs_syscall] = afs_ni_syscall32;
128 #endif
129
130     unregister_filesystem(&afs_file_system);
131
132     osi_linux_free_inode_pages(); /* Invalidate all pages using AFS inodes. */
133     osi_linux_free_afs_memory();
134
135     return;
136 }
137
138 static long get_page_offset(void)
139 {
140 #if defined(AFS_PPC_LINUX22_ENV) || defined(AFS_SPARC64_LINUX20_ENV) || defined(AFS_SPARC_LINUX20_ENV)
141     return PAGE_OFFSET;
142 #else
143     struct task_struct *p;
144
145     /* search backward thru the circular list */
146     for(p = current; p; p = p->prev_task)
147         if (p->pid == 1)
148             return p->addr_limit.seg;
149
150     return 0;
151 #endif
152 }