5d29b43c57ac718b95c3d5104a421b9b2afbaa77
[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 #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 void *afs_ni_syscall = NULL;
46
47 int init_module(void)
48 {
49     extern int afs_syscall();
50     extern int afs_xsetgroups();
51
52     /* obtain PAGE_OFFSET value */
53     afs_linux_page_offset = get_page_offset();
54
55     if (afs_linux_page_offset == 0) {
56         /* couldn't obtain page offset so can't continue */
57         printf("afs: Unable to obtain PAGE_OFFSET. Exiting..");
58         return -EIO;
59     }
60
61     /* Initialize pointers to kernel syscalls. */
62     sys_settimeofdayp = sys_call_table[__NR_settimeofday];
63     sys_socketcallp = sys_call_table[__NR_socketcall];
64     sys_killp = sys_call_table[__NR_kill];
65
66     /* setup AFS entry point. */
67     if (sys_call_table[__NR_afs_syscall] == afs_syscall) {
68         printf("AFS syscall entry point already in use!\n");
69         return -EBUSY;
70     }
71
72
73     afs_ni_syscall = sys_call_table[__NR_afs_syscall];
74     sys_call_table[__NR_afs_syscall] = afs_syscall;
75
76     osi_Init();
77     register_filesystem(&afs_file_system);
78
79     /* Intercept setgroups calls */
80     sys_setgroupsp = sys_call_table[__NR_setgroups];
81     sys_call_table[__NR_setgroups] = afs_xsetgroups;
82
83     return 0;
84 }
85
86 void cleanup_module(void)
87 {
88     struct task_struct *t;
89
90     sys_call_table[__NR_setgroups] = sys_setgroupsp;
91     sys_call_table[__NR_afs_syscall] = afs_ni_syscall;
92
93     unregister_filesystem(&afs_file_system);
94
95     osi_linux_free_inode_pages(); /* Invalidate all pages using AFS inodes. */
96     osi_linux_free_afs_memory();
97
98     return;
99 }
100
101 static long get_page_offset(void)
102 {
103 #if defined(AFS_PPC_LINUX22_ENV)
104     return PAGE_OFFSET;
105 #else
106     struct task_struct *p;
107
108     /* search backward thru the circular list */
109     for(p = current; p; p = p->prev_task)
110         if (p->pid == 1)
111             return p->addr_limit.seg;
112
113     return 0;
114 #endif
115 }