Linux: Add autoconf macro for structure checks
[openafs.git] / src / afs / LINUX / osi_compat.h
1 /* Kernel compatibility routines
2  *
3  * This file contains definitions to provide compatibility between different
4  * versions of the Linux kernel. It is an ifdef maze, but the idea is that
5  * by concentrating the horror here, the rest of the tree may remaing a
6  * little cleaner...
7  */
8
9 #ifndef AFS_LINUX_OSI_COMPAT_H
10 #define AFS_LINUX_OSI_COMPAT_H
11
12 #if defined(HAVE_LINUX_FREEZER_H)
13 # include <linux/freezer.h>
14 #endif
15
16 #ifndef HAVE_LINUX_DO_SYNC_READ
17 static inline int
18 do_sync_read(struct file *fp, char *buf, size_t count, loff_t *offp) {
19     return generic_file_read(fp, buf, count, offp);
20 }
21
22 static inline int
23 do_sync_write(struct file *fp, char *buf, size_t count, loff_t *offp) {
24     return generic_file_write(fp, buf, count, offp);
25 }
26
27 #endif /* DO_SYNC_READ */
28
29 static inline int
30 afs_posix_lock_file(struct file *fp, struct file_lock *flp) {
31 #ifdef POSIX_LOCK_FILE_WAIT_ARG
32     return posix_lock_file(fp, flp, NULL);
33 #else
34     flp->fl_flags &=~ FL_SLEEP;
35     return posix_lock_file(fp, flp);
36 #endif
37 }
38
39 static inline void
40 afs_posix_test_lock(struct file *fp, struct file_lock *flp) {
41 #if defined(POSIX_TEST_LOCK_CONFLICT_ARG)
42     struct file_lock conflict;
43     if (posix_test_lock(fp, flp, &conflict)) {
44         locks_copy_lock(flp, &conflict);
45         flp->fl_type = F_UNLCK;
46     }
47 #elif defined(POSIX_TEST_LOCK_RETURNS_CONFLICT)
48     struct file_lock *conflict;
49     if (conflict = posix_test_lock(fp, flp)) {
50         locks_copy_lock(flp, conflict);
51         flp->fl_type = F_UNLCK;
52     }
53 #else
54     posix_test_lock(fp, flp);
55 #endif
56 }
57
58 #ifdef DCACHE_NFSFS_RENAMED
59 static inline void
60 afs_linux_clear_nfsfs_renamed(struct dentry *dp) {
61     spin_lock(&dp->d_lock);
62     dp->d_flags &= ~DCACHE_NFSFS_RENAMED;
63     spin_unlock(&dp->d_lock);
64 }
65
66 static inline void
67 afs_linux_set_nfsfs_renamed(struct dentry *dp) {
68     spin_lock(&dp->d_lock);
69     dp->d_flags |= DCACHE_NFSFS_RENAMED;
70     spin_unlock(&dp->d_lock);
71 }
72
73 static inline int
74 afs_linux_nfsfs_renamed(struct dentry *dp) {
75     return dp->d_flags & DCACHE_NFSFS_RENAMED;
76 }
77
78 #else
79 static inline void afs_linux_clear_nfsfs_renamed(void) { return; }
80 static inline void afs_linux_set_nfsfs_renamed(void) { return; }
81 #endif
82
83 #ifndef HAVE_LINUX_HLIST_UNHASHED
84 static void
85 hlist_unhashed(const struct hlist_node *h) {
86     return (!h->pprev == NULL);
87 }
88 #endif
89
90 #if defined(WRITEPAGE_ACTIVATE)
91 #define AOP_WRITEPAGE_ACTIVATE WRITEPAGE_ACTIVATE
92 #endif
93
94 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN) && !defined(HAVE_LINUX_GRAB_CACHE_PAGE_WRITE_BEGIN)
95 static inline struct page *
96 grab_cache_page_write_begin(struct address_space *mapping, pgoff_t index,
97                             unsigned int flags) {
98     return __grab_cache_page(mapping, index);
99 }
100 #endif
101
102 #if defined(HAVE_KMEM_CACHE_T)
103 #define afs_kmem_cache_t kmem_cache_t
104 #else
105 #define afs_kmem_cache_t struct kmem_cache
106 #endif
107
108 extern void init_once(void *);
109 #if defined(HAVE_KMEM_CACHE_T)
110 static inline void
111 init_once_func(void * foo, kmem_cache_t * cachep, unsigned long flags) {
112 #if defined(SLAB_CTOR_VERIFY)
113     if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
114         SLAB_CTOR_CONSTRUCTOR)
115 #endif
116     init_once(foo);
117 }
118 #elif defined(KMEM_CACHE_INIT)
119 static inline void
120 init_once_func(struct kmem_cache * cachep, void * foo) {
121     init_once(foo);
122 }
123 #elif !defined(KMEM_CACHE_CTOR_TAKES_VOID)
124 static inline void
125 init_once_func(void * foo, struct kmem_cache * cachep, unsigned long flags) {
126 #if defined(SLAB_CTOR_VERIFY)
127     if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
128         SLAB_CTOR_CONSTRUCTOR)
129 #endif
130     init_once(foo);
131 }
132 #else
133 static inline void
134 init_once_func(void * foo) {
135     init_once(foo);
136 }
137 #endif
138
139 #ifndef SLAB_RECLAIM_ACCOUNT
140 #define SLAB_RECLAIM_ACCOUNT 0
141 #endif
142
143 #if defined(SLAB_KERNEL)
144 #define KALLOC_TYPE SLAB_KERNEL
145 #else
146 #define KALLOC_TYPE GFP_KERNEL
147 #endif
148
149 static inline struct key *
150 afs_linux_key_alloc(struct key_type *type, const char *desc, uid_t uid,
151                     gid_t gid, key_perm_t perm, unsigned long flags)
152 {
153 #if defined(KEY_ALLOC_NEEDS_STRUCT_TASK)
154     return key_alloc(type, desc, uid, gid, current, perm, flags);
155 #elif defined(KEY_ALLOC_NEEDS_CRED)
156     return key_alloc(type, desc, uid, gid, current_cred(), perm, flags);
157 #else
158     return key_alloc(type, desc, uid, gid, perm, flags);
159 #endif
160 }
161
162 #if defined(STRUCT_TASK_HAS_CRED)
163 static inline struct key*
164 afs_linux_search_keyring(afs_ucred_t *cred, struct key_type *type)
165 {
166     key_ref_t key_ref;
167
168     if (cred->tgcred->session_keyring) {
169         key_ref = keyring_search(
170                       make_key_ref(cred->tgcred->session_keyring, 1),
171                       type, "_pag");
172         if (IS_ERR(key_ref))
173             return ERR_CAST(key_ref);
174
175         return key_ref_to_ptr(key_ref);
176     }
177
178     return ERR_PTR(-ENOKEY);
179 }
180
181 static inline int
182 afs_linux_cred_is_current(afs_ucred_t *cred)
183 {
184     return (cred == current_cred());
185 }
186
187 #else
188 static inline struct key*
189 afs_linux_search_keyring(afs_ucred_t *cred, struct key_type *type)
190 {
191     return request_key(type, "_pag", NULL);
192 }
193
194 static inline int
195 afs_linux_cred_is_current(afs_ucred_t *cred)
196 {
197     return 1;
198 }
199 #endif
200
201 #ifdef LINUX_KEYRING_SUPPORT
202 # ifndef KEY_ALLOC_NOT_IN_QUOTA
203 #  define KEY_ALLOC_NOT_IN_QUOTA 1
204 # endif
205 # ifndef KEY_ALLOC_IN_QUOTA
206 #  define KEY_ALLOC_IN_QUOTA 0
207 # endif
208 #endif
209 #endif
210
211 #ifndef HAVE_LINUX_PAGE_OFFSET
212 static inline loff_t
213 page_offset(struct page *pp)
214 {
215     return (((loff_t) pp->index) << PAGE_CACHE_SHIFT);
216 }
217 #endif
218
219 #ifndef HAVE_LINUX_ZERO_USER_SEGMENTS
220 static inline void
221 zero_user_segments(struct page *pp, unsigned int from1, unsigned int to1,
222                    unsigned int from2, unsigned int to2)
223 {
224     void *base = kmap_atomic(pp, KM_USER0);
225
226     if (to1 > from1)
227         memset(base + from1, 0, to1 - from1);
228
229     if (to2 > from2)
230         memset(base + from2, 0, to2 - from2);
231
232     flush_dcache_page(pp);
233     kunmap_atomic(base, KM_USER0);
234 }
235 #endif
236
237 #ifndef HAVE_LINUX_VFS_LLSEEK
238 static inline loff_t
239 vfs_llseek(struct file *filp, loff_t offset, int origin) {
240     if (filp->f_op->llseek)
241         return filp->f_op->llseek(filp, offset, origin);
242     return default_llseek(filp, offset, origin);
243 }
244 #endif
245
246 #ifndef HAVE_LINUX_KERNEL_SETSOCKOPT
247 /* Available from 2.6.19 */
248
249 static inline int
250 kernel_setsockopt(struct socket *sockp, int level, int name, char *val,
251                   unsigned int len) {
252     mm_segment_t old_fs = get_fs();
253     int ret;
254
255     set_fs(get_ds());
256     ret = sockp->ops->setsockopt(sockp, level, name, val, len);
257     set_fs(old_fs);
258
259     return ret;
260 }
261
262 static inline int
263 kernel_getsockopt(struct socket *sockp, int level, int name, char *val,
264                   int *len) {
265     mm_segment_t old_fs = get_fs();
266     int ret;
267
268     set_fs(get_ds());
269     ret = sockp->ops->setsockopt(sockp, level, name, val, len);
270     set_fs(old_fs);
271
272     return ret;
273 }
274 #endif
275
276 #ifdef HAVE_TRY_TO_FREEZE
277 static inline void
278 afs_try_to_freeze() {
279 # ifdef LINUX_REFRIGERATOR_TAKES_PF_FREEZE
280     try_to_freeze(PF_FREEZE);
281 # else
282     try_to_freeze();
283 # endif
284 }
285 #else
286 static inline void
287 afs_try_to_freeze() {
288 # ifdef CONFIG_PM
289     if (current->flags & PF_FREEZE) {
290         refrigerator(PF_FREEZE);
291 # endif
292 }
293 #endif
294