511b882677c24f6323b740edebb2881b28f807a1
[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     conflict = posix_test_lock(fp, flp);
50     if (conflict) {
51         locks_copy_lock(flp, conflict);
52         flp->fl_type = F_UNLCK;
53     }
54 #else
55     posix_test_lock(fp, flp);
56 #endif
57 }
58
59 #ifdef DCACHE_NFSFS_RENAMED
60 static inline void
61 afs_linux_clear_nfsfs_renamed(struct dentry *dp) {
62     spin_lock(&dp->d_lock);
63     dp->d_flags &= ~DCACHE_NFSFS_RENAMED;
64     spin_unlock(&dp->d_lock);
65 }
66
67 static inline void
68 afs_linux_set_nfsfs_renamed(struct dentry *dp) {
69     spin_lock(&dp->d_lock);
70     dp->d_flags |= DCACHE_NFSFS_RENAMED;
71     spin_unlock(&dp->d_lock);
72 }
73
74 static inline int
75 afs_linux_nfsfs_renamed(struct dentry *dp) {
76     return dp->d_flags & DCACHE_NFSFS_RENAMED;
77 }
78
79 #else
80 static inline void afs_linux_clear_nfsfs_renamed(void) { return; }
81 static inline void afs_linux_set_nfsfs_renamed(void) { return; }
82 #endif
83
84 #ifndef HAVE_LINUX_HLIST_UNHASHED
85 static void
86 hlist_unhashed(const struct hlist_node *h) {
87     return (!h->pprev == NULL);
88 }
89 #endif
90
91 #if defined(WRITEPAGE_ACTIVATE)
92 #define AOP_WRITEPAGE_ACTIVATE WRITEPAGE_ACTIVATE
93 #endif
94
95 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN) && !defined(HAVE_LINUX_GRAB_CACHE_PAGE_WRITE_BEGIN)
96 static inline struct page *
97 grab_cache_page_write_begin(struct address_space *mapping, pgoff_t index,
98                             unsigned int flags) {
99     return __grab_cache_page(mapping, index);
100 }
101 #endif
102
103 #if defined(HAVE_KMEM_CACHE_T)
104 #define afs_kmem_cache_t kmem_cache_t
105 #else
106 #define afs_kmem_cache_t struct kmem_cache
107 #endif
108
109 extern void init_once(void *);
110 #if defined(HAVE_KMEM_CACHE_T)
111 static inline void
112 init_once_func(void * foo, kmem_cache_t * cachep, unsigned long flags) {
113 #if defined(SLAB_CTOR_VERIFY)
114     if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
115         SLAB_CTOR_CONSTRUCTOR)
116 #endif
117     init_once(foo);
118 }
119 #elif defined(KMEM_CACHE_INIT)
120 static inline void
121 init_once_func(struct kmem_cache * cachep, void * foo) {
122     init_once(foo);
123 }
124 #elif !defined(KMEM_CACHE_CTOR_TAKES_VOID)
125 static inline void
126 init_once_func(void * foo, struct kmem_cache * cachep, unsigned long flags) {
127 #if defined(SLAB_CTOR_VERIFY)
128     if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
129         SLAB_CTOR_CONSTRUCTOR)
130 #endif
131     init_once(foo);
132 }
133 #else
134 static inline void
135 init_once_func(void * foo) {
136     init_once(foo);
137 }
138 #endif
139
140 #ifndef SLAB_RECLAIM_ACCOUNT
141 #define SLAB_RECLAIM_ACCOUNT 0
142 #endif
143
144 #if defined(SLAB_KERNEL)
145 #define KALLOC_TYPE SLAB_KERNEL
146 #else
147 #define KALLOC_TYPE GFP_KERNEL
148 #endif
149
150 static inline struct key *
151 afs_linux_key_alloc(struct key_type *type, const char *desc, uid_t uid,
152                     gid_t gid, key_perm_t perm, unsigned long flags)
153 {
154 #if defined(KEY_ALLOC_NEEDS_STRUCT_TASK)
155     return key_alloc(type, desc, uid, gid, current, perm, flags);
156 #elif defined(KEY_ALLOC_NEEDS_CRED)
157     return key_alloc(type, desc, uid, gid, current_cred(), perm, flags);
158 #else
159     return key_alloc(type, desc, uid, gid, perm, flags);
160 #endif
161 }
162
163 #if defined(STRUCT_TASK_STRUCT_HAS_CRED)
164 static inline struct key*
165 afs_linux_search_keyring(afs_ucred_t *cred, struct key_type *type)
166 {
167     key_ref_t key_ref;
168
169     if (cred->tgcred->session_keyring) {
170         key_ref = keyring_search(
171                       make_key_ref(cred->tgcred->session_keyring, 1),
172                       type, "_pag");
173         if (IS_ERR(key_ref))
174             return ERR_CAST(key_ref);
175
176         return key_ref_to_ptr(key_ref);
177     }
178
179     return ERR_PTR(-ENOKEY);
180 }
181
182 static inline int
183 afs_linux_cred_is_current(afs_ucred_t *cred)
184 {
185     return (cred == current_cred());
186 }
187
188 #else
189 static inline struct key*
190 afs_linux_search_keyring(afs_ucred_t *cred, struct key_type *type)
191 {
192     return request_key(type, "_pag", NULL);
193 }
194
195 static inline int
196 afs_linux_cred_is_current(afs_ucred_t *cred)
197 {
198     return 1;
199 }
200 #endif
201
202 #ifdef LINUX_KEYRING_SUPPORT
203 # ifndef KEY_ALLOC_NOT_IN_QUOTA
204 #  define KEY_ALLOC_NOT_IN_QUOTA 1
205 # endif
206 # ifndef KEY_ALLOC_IN_QUOTA
207 #  define KEY_ALLOC_IN_QUOTA 0
208 # endif
209 #endif
210 #endif
211
212 #ifndef HAVE_LINUX_PAGE_OFFSET
213 static inline loff_t
214 page_offset(struct page *pp)
215 {
216     return (((loff_t) pp->index) << PAGE_CACHE_SHIFT);
217 }
218 #endif
219
220 #ifndef HAVE_LINUX_ZERO_USER_SEGMENTS
221 static inline void
222 zero_user_segments(struct page *pp, unsigned int from1, unsigned int to1,
223                    unsigned int from2, unsigned int to2)
224 {
225     void *base = kmap_atomic(pp, KM_USER0);
226
227     if (to1 > from1)
228         memset(base + from1, 0, to1 - from1);
229
230     if (to2 > from2)
231         memset(base + from2, 0, to2 - from2);
232
233     flush_dcache_page(pp);
234     kunmap_atomic(base, KM_USER0);
235 }
236
237 static inline void
238 zero_user_segment(struct page *pp, unsigned int from1, unsigned int to1)
239 {
240     zero_user_segments(pp, from1, to1, 0, 0);
241 }
242 #endif
243
244 #ifndef HAVE_LINUX_KERNEL_SETSOCKOPT
245 /* Available from 2.6.19 */
246
247 static inline int
248 kernel_setsockopt(struct socket *sockp, int level, int name, char *val,
249                   unsigned int len) {
250     mm_segment_t old_fs = get_fs();
251     int ret;
252
253     set_fs(get_ds());
254     ret = sockp->ops->setsockopt(sockp, level, name, val, len);
255     set_fs(old_fs);
256
257     return ret;
258 }
259
260 static inline int
261 kernel_getsockopt(struct socket *sockp, int level, int name, char *val,
262                   int *len) {
263     mm_segment_t old_fs = get_fs();
264     int ret;
265
266     set_fs(get_ds());
267     ret = sockp->ops->getsockopt(sockp, level, name, val, len);
268     set_fs(old_fs);
269
270     return ret;
271 }
272 #endif
273
274 #ifdef HAVE_TRY_TO_FREEZE
275 static inline void
276 afs_try_to_freeze(void) {
277 # ifdef LINUX_REFRIGERATOR_TAKES_PF_FREEZE
278     try_to_freeze(PF_FREEZE);
279 # else
280     try_to_freeze();
281 # endif
282 }
283 #else
284 static inline void
285 afs_try_to_freeze(void) {
286 # ifdef CONFIG_PM
287     if (current->flags & PF_FREEZE) {
288         refrigerator(PF_FREEZE);
289     }
290 # endif
291 }
292 #endif
293
294 #if !defined(HAVE_LINUX_PAGECHECKED)
295 # if defined(HAVE_LINUX_PAGEFSMISC)
296 #  include <linux/page-flags.h>
297
298 #  define PageChecked(p)            PageFsMisc((p))
299 #  define SetPageChecked(p)         SetPageFsMisc((p))
300 #  define ClearPageChecked(p)       ClearPageFsMisc((p))
301
302 # endif
303 #endif
304
305 #if !defined(NEW_EXPORT_OPS)
306 extern struct export_operations export_op_default;
307 #endif
308
309 static inline struct dentry *
310 afs_get_dentry_from_fh(struct super_block *afs_cacheSBp, afs_dcache_id_t *ainode,
311                 int cache_fh_len, int cache_fh_type,
312                 int (*afs_fh_acceptable)(void *, struct dentry *)) {
313 #if defined(NEW_EXPORT_OPS)
314     return afs_cacheSBp->s_export_op->fh_to_dentry(afs_cacheSBp, &ainode->ufs.fh,
315                 cache_fh_len, cache_fh_type);
316 #else
317     if (afs_cacheSBp->s_export_op && afs_cacheSBp->s_export_op->decode_fh)
318         return afs_cacheSBp->s_export_op->decode_fh(afs_cacheSBp, ainode->ufs.raw,
319                         cache_fh_len, cache_fh_type, afs_fh_acceptable, NULL);
320     else
321         return export_op_default.decode_fh(afs_cacheSBp, ainode->ufs.raw,
322                         cache_fh_len, cache_fh_type, afs_fh_acceptable, NULL);
323 #endif
324 }
325
326 static inline int
327 afs_get_fh_from_dentry(struct dentry *dp, afs_ufs_dcache_id_t *ainode, int *max_lenp) {
328     if (dp->d_sb->s_export_op->encode_fh)
329         return dp->d_sb->s_export_op->encode_fh(dp, &ainode->raw[0], max_lenp, 0);
330 #if defined(NEW_EXPORT_OPS)
331     /* If fs doesn't provide an encode_fh method, assume the default INO32 type */
332     *max_lenp = sizeof(struct fid)/4;
333     ainode->fh.i32.ino = dp->d_inode->i_ino;
334     ainode->fh.i32.gen = dp->d_inode->i_generation;
335     return FILEID_INO32_GEN;
336 #else
337     /* or call the default encoding function for the old API */
338     return export_op_default.encode_fh(dp, &ainode->raw[0], max_lenp, 0);
339 #endif
340 }
341
342 static inline void
343 afs_init_sb_export_ops(struct super_block *sb) {
344 #if !defined(NEW_EXPORT_OPS)
345     /*
346      * decode_fh will call this function.  If not defined for this FS, make
347      * sure it points to the default
348      */
349     if (!sb->s_export_op->find_exported_dentry) {
350         /* Some kernels (at least 2.6.9) do not prototype find_exported_dentry,
351          * even though it is exported, so prototype it ourselves. Newer
352          * kernels do prototype it, but as long as our protoype matches the
353          * real one (the signature never changed before NEW_EXPORT_OPS came
354          * into play), there should be no problems. */
355         extern struct dentry * find_exported_dentry(struct super_block *sb, void *obj, void *parent,
356                                                     int (*acceptable)(void *context, struct dentry *de),
357                                                     void *context);
358         sb->s_export_op->find_exported_dentry = find_exported_dentry;
359     }
360 #endif
361 }
362
363 static inline void
364 afs_linux_lock_inode(struct inode *ip) {
365 #ifdef STRUCT_INODE_HAS_I_MUTEX
366     mutex_lock(&ip->i_mutex);
367 #else
368     down(&ip->i_sem);
369 #endif
370 }
371
372 static inline void
373 afs_linux_unlock_inode(struct inode *ip) {
374 #ifdef STRUCT_INODE_HAS_I_MUTEX
375     mutex_unlock(&ip->i_mutex);
376 #else
377     up(&ip->i_sem);
378 #endif
379 }
380
381 static inline int
382 afs_inode_setattr(struct osi_file *afile, struct iattr *newattrs) {
383
384     int code = 0;
385     struct inode *inode = OSIFILE_INODE(afile);
386 #if !defined(HAVE_LINUX_INODE_SETATTR)
387     code = inode->i_op->setattr(afile->filp->f_dentry, newattrs);
388 #elif defined(INODE_SETATTR_NOT_VOID)
389     if (inode->i_op && inode->i_op->setattr)
390         code = inode->i_op->setattr(afile->filp->f_dentry, newattrs);
391     else
392         code = inode_setattr(inode, newattrs);
393 #else
394     inode_setattr(inode, newattrs);
395 #endif
396     return code;
397 }