29a016fdf46cacbad5cdc1d011410f4ff5104e39
[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
18     ("$Header$");
19
20 #include <linux/module.h> /* early to avoid printf->printk mapping */
21 #include "afs/sysincludes.h"
22 #include "afsincludes.h"
23 #include "h/unistd.h"           /* For syscall numbers. */
24 #include "h/mm.h"
25
26 #ifdef AFS_AMD64_LINUX20_ENV
27 #include <asm/ia32_unistd.h>
28 #endif
29 #ifdef AFS_SPARC64_LINUX20_ENV
30 #include <linux/ioctl32.h>
31 #endif
32
33 #include <linux/proc_fs.h>
34 #include <linux/slab.h>
35 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
36 #include <linux/init.h>
37 #include <linux/sched.h>
38 #include <linux/kernel.h>
39 #endif
40
41 #ifdef HAVE_KERNEL_LINUX_SEQ_FILE_H
42 #include <linux/seq_file.h>
43 #endif
44
45 extern struct file_system_type afs_fs_type;
46
47 #if !defined(AFS_LINUX24_ENV)
48 static long get_page_offset(void);
49 #endif
50
51 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
52 DEFINE_MUTEX(afs_global_lock);
53 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
54 DECLARE_MUTEX(afs_global_lock);
55 #else
56 struct semaphore afs_global_lock = MUTEX;
57 #endif
58 int afs_global_owner = 0;
59 #if !defined(AFS_LINUX24_ENV)
60 unsigned long afs_linux_page_offset = 0;        /* contains the PAGE_OFFSET value */
61 #endif
62
63
64 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
65 int __init
66 afs_init(void)
67 #else
68 int
69 init_module(void)
70 #endif
71 {
72     int err;
73     RWLOCK_INIT(&afs_xosi, "afs_xosi");
74
75 #if !defined(AFS_LINUX24_ENV)
76     /* obtain PAGE_OFFSET value */
77     afs_linux_page_offset = get_page_offset();
78
79 #ifndef AFS_S390_LINUX22_ENV
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 #endif /* AFS_S390_LINUX22_ENV */
86 #endif /* !defined(AFS_LINUX24_ENV) */
87
88     osi_Init();
89
90     err = osi_syscall_init();
91     if (err)
92         return err;
93     err = afs_init_inodecache();
94     if (err)
95         return err;
96     register_filesystem(&afs_fs_type);
97     osi_sysctl_init();
98 #ifdef AFS_LINUX24_ENV
99     afsproc_init();
100 #endif
101
102     return 0;
103 }
104
105 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
106 void __exit
107 afs_cleanup(void)
108 #else
109 void
110 cleanup_module(void)
111 #endif
112 {
113     osi_sysctl_clean();
114     osi_syscall_clean();
115     unregister_filesystem(&afs_fs_type);
116
117     afs_destroy_inodecache();
118     osi_linux_free_afs_memory();
119
120 #ifdef AFS_LINUX24_ENV
121     afsproc_exit();
122 #endif
123     return;
124 }
125
126 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
127 MODULE_LICENSE("http://www.openafs.org/dl/license10.html");
128 module_init(afs_init);
129 module_exit(afs_cleanup);
130 #endif
131
132
133 #if !defined(AFS_LINUX24_ENV)
134 static long
135 get_page_offset(void)
136 {
137 #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) || defined(AFS_IA64_LINUX20_ENV) || defined(AFS_PARISC_LINUX24_ENV) || defined(AFS_AMD64_LINUX20_ENV) || defined(AFS_PPC64_LINUX20_ENV)
138     return PAGE_OFFSET;
139 #else
140     struct task_struct *p, *q;
141
142     /* search backward thru the circular list */
143 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
144     read_lock(&tasklist_lock);
145 #endif
146     /* search backward thru the circular list */
147 #ifdef DEFINED_PREV_TASK
148     for (q = current; p = q; q = prev_task(p)) {
149 #else
150     for (p = current; p; p = p->prev_task) {
151 #endif
152         if (p->pid == 1) {
153 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
154             read_unlock(&tasklist_lock);
155 #endif
156             return p->addr_limit.seg;
157         }
158     }
159
160 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
161     read_unlock(&tasklist_lock);
162 #endif
163     return 0;
164 #endif
165 }
166 #endif /* !AFS_LINUX24_ENV */