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