Linux CM: Update wait code
[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 #if defined(LINUX_KEYRING_SUPPORT)
17 # include <linux/rwsem.h>
18 # include <linux/key.h>
19 # if defined(HAVE_LINUX_KEY_TYPE_H)
20 #  include <linux/key-type.h>
21 # endif
22 # ifndef KEY_ALLOC_IN_QUOTA
23 /* Before these flags were added in Linux commit v2.6.18-rc1~816,
24  * key_alloc just took a boolean not_in_quota */
25 #  define KEY_ALLOC_IN_QUOTA 0
26 #  define KEY_ALLOC_NOT_IN_QUOTA 1
27 # endif
28 #endif
29
30 #ifndef HAVE_LINUX_DO_SYNC_READ
31 static inline int
32 do_sync_read(struct file *fp, char *buf, size_t count, loff_t *offp) {
33     return generic_file_read(fp, buf, count, offp);
34 }
35
36 static inline int
37 do_sync_write(struct file *fp, char *buf, size_t count, loff_t *offp) {
38     return generic_file_write(fp, buf, count, offp);
39 }
40
41 #endif /* DO_SYNC_READ */
42
43 static inline int
44 afs_posix_lock_file(struct file *fp, struct file_lock *flp) {
45 #ifdef POSIX_LOCK_FILE_WAIT_ARG
46     return posix_lock_file(fp, flp, NULL);
47 #else
48     flp->fl_flags &=~ FL_SLEEP;
49     return posix_lock_file(fp, flp);
50 #endif
51 }
52
53 static inline void
54 afs_posix_test_lock(struct file *fp, struct file_lock *flp) {
55 #if defined(POSIX_TEST_LOCK_CONFLICT_ARG)
56     struct file_lock conflict;
57     if (posix_test_lock(fp, flp, &conflict)) {
58         locks_copy_lock(flp, &conflict);
59         flp->fl_type = F_UNLCK;
60     }
61 #elif defined(POSIX_TEST_LOCK_RETURNS_CONFLICT)
62     struct file_lock *conflict;
63     conflict = posix_test_lock(fp, flp);
64     if (conflict) {
65         locks_copy_lock(flp, conflict);
66         flp->fl_type = F_UNLCK;
67     }
68 #else
69     posix_test_lock(fp, flp);
70 #endif
71 }
72
73 #ifdef DCACHE_NFSFS_RENAMED
74 static inline void
75 afs_linux_clear_nfsfs_renamed(struct dentry *dp) {
76     spin_lock(&dp->d_lock);
77     dp->d_flags &= ~DCACHE_NFSFS_RENAMED;
78     spin_unlock(&dp->d_lock);
79 }
80
81 static inline void
82 afs_linux_set_nfsfs_renamed(struct dentry *dp) {
83     spin_lock(&dp->d_lock);
84     dp->d_flags |= DCACHE_NFSFS_RENAMED;
85     spin_unlock(&dp->d_lock);
86 }
87
88 static inline int
89 afs_linux_nfsfs_renamed(struct dentry *dp) {
90     return dp->d_flags & DCACHE_NFSFS_RENAMED;
91 }
92
93 #else
94 static inline void afs_linux_clear_nfsfs_renamed(void) { return; }
95 static inline void afs_linux_set_nfsfs_renamed(void) { return; }
96 #endif
97
98 #ifndef HAVE_LINUX_HLIST_UNHASHED
99 static void
100 hlist_unhashed(const struct hlist_node *h) {
101     return (!h->pprev == NULL);
102 }
103 #endif
104
105 #if defined(WRITEPAGE_ACTIVATE)
106 #define AOP_WRITEPAGE_ACTIVATE WRITEPAGE_ACTIVATE
107 #endif
108
109 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN) && !defined(HAVE_LINUX_GRAB_CACHE_PAGE_WRITE_BEGIN)
110 static inline struct page *
111 grab_cache_page_write_begin(struct address_space *mapping, pgoff_t index,
112                             unsigned int flags) {
113     return __grab_cache_page(mapping, index);
114 }
115 #endif
116
117 #if defined(HAVE_KMEM_CACHE_T)
118 #define afs_kmem_cache_t kmem_cache_t
119 #else
120 #define afs_kmem_cache_t struct kmem_cache
121 #endif
122
123 extern void init_once(void *);
124 #if defined(HAVE_KMEM_CACHE_T)
125 static inline void
126 init_once_func(void * foo, kmem_cache_t * 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 #elif defined(KMEM_CACHE_INIT)
134 static inline void
135 init_once_func(struct kmem_cache * cachep, void * foo) {
136     init_once(foo);
137 }
138 #elif !defined(KMEM_CACHE_CTOR_TAKES_VOID)
139 static inline void
140 init_once_func(void * foo, struct kmem_cache * cachep, unsigned long flags) {
141 #if defined(SLAB_CTOR_VERIFY)
142     if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
143         SLAB_CTOR_CONSTRUCTOR)
144 #endif
145     init_once(foo);
146 }
147 #else
148 static inline void
149 init_once_func(void * foo) {
150     init_once(foo);
151 }
152 #endif
153
154 #ifndef SLAB_RECLAIM_ACCOUNT
155 #define SLAB_RECLAIM_ACCOUNT 0
156 #endif
157
158 #if defined(SLAB_KERNEL)
159 #define KALLOC_TYPE SLAB_KERNEL
160 #else
161 #define KALLOC_TYPE GFP_KERNEL
162 #endif
163
164 #ifdef LINUX_KEYRING_SUPPORT
165 static inline struct key *
166 afs_linux_key_alloc(struct key_type *type, const char *desc, uid_t uid,
167                     gid_t gid, key_perm_t perm, unsigned long flags)
168 {
169 # if defined(KEY_ALLOC_NEEDS_STRUCT_TASK)
170     return key_alloc(type, desc, uid, gid, current, perm, flags);
171 # elif defined(KEY_ALLOC_NEEDS_CRED)
172     return key_alloc(type, desc, uid, gid, current_cred(), perm, flags);
173 # else
174     return key_alloc(type, desc, uid, gid, perm, flags);
175 # endif
176 }
177
178 # if defined(STRUCT_TASK_STRUCT_HAS_CRED)
179 static inline struct key*
180 afs_linux_search_keyring(afs_ucred_t *cred, struct key_type *type)
181 {
182     key_ref_t key_ref;
183
184     if (cred->tgcred->session_keyring) {
185         key_ref = keyring_search(
186                       make_key_ref(cred->tgcred->session_keyring, 1),
187                       type, "_pag");
188         if (IS_ERR(key_ref))
189             return ERR_CAST(key_ref);
190
191         return key_ref_to_ptr(key_ref);
192     }
193
194     return ERR_PTR(-ENOKEY);
195 }
196 # else
197 static inline struct key*
198 afs_linux_search_keyring(afs_ucred_t *cred, struct key_type *type)
199 {
200     return request_key(type, "_pag", NULL);
201 }
202 # endif /* STRUCT_TASK_STRUCT_HAS_CRED */
203 #endif /* LINUX_KEYRING_SUPPORT */
204
205 #ifdef STRUCT_TASK_STRUCT_HAS_CRED
206 static inline int
207 afs_linux_cred_is_current(afs_ucred_t *cred)
208 {
209     return (cred == current_cred());
210 }
211 #else
212 static inline int
213 afs_linux_cred_is_current(afs_ucred_t *cred)
214 {
215     return 1;
216 }
217 #endif
218
219 #ifndef HAVE_LINUX_PAGE_OFFSET
220 static inline loff_t
221 page_offset(struct page *pp)
222 {
223     return (((loff_t) pp->index) << PAGE_CACHE_SHIFT);
224 }
225 #endif
226
227 #ifndef HAVE_LINUX_ZERO_USER_SEGMENTS
228 static inline void
229 zero_user_segments(struct page *pp, unsigned int from1, unsigned int to1,
230                    unsigned int from2, unsigned int to2)
231 {
232     void *base = kmap_atomic(pp, KM_USER0);
233
234     if (to1 > from1)
235         memset(base + from1, 0, to1 - from1);
236
237     if (to2 > from2)
238         memset(base + from2, 0, to2 - from2);
239
240     flush_dcache_page(pp);
241     kunmap_atomic(base, KM_USER0);
242 }
243
244 static inline void
245 zero_user_segment(struct page *pp, unsigned int from1, unsigned int to1)
246 {
247     zero_user_segments(pp, from1, to1, 0, 0);
248 }
249 #endif
250
251 #ifndef HAVE_LINUX_KERNEL_SETSOCKOPT
252 /* Available from 2.6.19 */
253
254 static inline int
255 kernel_setsockopt(struct socket *sockp, int level, int name, char *val,
256                   unsigned int len) {
257     mm_segment_t old_fs = get_fs();
258     int ret;
259
260     set_fs(get_ds());
261     ret = sockp->ops->setsockopt(sockp, level, name, val, len);
262     set_fs(old_fs);
263
264     return ret;
265 }
266
267 static inline int
268 kernel_getsockopt(struct socket *sockp, int level, int name, char *val,
269                   int *len) {
270     mm_segment_t old_fs = get_fs();
271     int ret;
272
273     set_fs(get_ds());
274     ret = sockp->ops->getsockopt(sockp, level, name, val, len);
275     set_fs(old_fs);
276
277     return ret;
278 }
279 #endif
280
281 #ifdef HAVE_TRY_TO_FREEZE
282 static inline int
283 afs_try_to_freeze(void) {
284 # ifdef LINUX_REFRIGERATOR_TAKES_PF_FREEZE
285     return try_to_freeze(PF_FREEZE);
286 # else
287     return try_to_freeze();
288 # endif
289 }
290 #else
291 static inline int
292 afs_try_to_freeze(void) {
293 # ifdef CONFIG_PM
294     if (current->flags & PF_FREEZE) {
295         refrigerator(PF_FREEZE);
296         return 1;
297     }
298 # endif
299     return 0;
300 }
301 #endif
302
303 /* The commit which changed refrigerator so that it takes no arguments
304  * also added freezing(), so if LINUX_REFRIGERATOR_TAKES_PF_FREEZE is
305  * true, the kernel doesn't have a freezing() function.
306  */
307 #ifdef LINUX_REFRIGERATOR_TAKES_PF_FREEZE
308 static inline int
309 freezing(struct task_struct *p)
310 {
311 # ifdef CONFIG_PM
312     return p->flags & PF_FREEZE;
313 # else
314     return 0;
315 # endif
316 }
317 #endif
318
319 #if !defined(HAVE_LINUX_PAGECHECKED)
320 # if defined(HAVE_LINUX_PAGEFSMISC)
321 #  include <linux/page-flags.h>
322
323 #  define PageChecked(p)            PageFsMisc((p))
324 #  define SetPageChecked(p)         SetPageFsMisc((p))
325 #  define ClearPageChecked(p)       ClearPageFsMisc((p))
326
327 # endif
328 #endif
329
330 #if !defined(NEW_EXPORT_OPS)
331 extern struct export_operations export_op_default;
332 #endif
333
334 static inline struct dentry *
335 afs_get_dentry_from_fh(struct super_block *afs_cacheSBp, afs_dcache_id_t *ainode,
336                 int cache_fh_len, int cache_fh_type,
337                 int (*afs_fh_acceptable)(void *, struct dentry *)) {
338 #if defined(NEW_EXPORT_OPS)
339     return afs_cacheSBp->s_export_op->fh_to_dentry(afs_cacheSBp, &ainode->ufs.fh,
340                 cache_fh_len, cache_fh_type);
341 #else
342     if (afs_cacheSBp->s_export_op && afs_cacheSBp->s_export_op->decode_fh)
343         return afs_cacheSBp->s_export_op->decode_fh(afs_cacheSBp, ainode->ufs.raw,
344                         cache_fh_len, cache_fh_type, afs_fh_acceptable, NULL);
345     else
346         return export_op_default.decode_fh(afs_cacheSBp, ainode->ufs.raw,
347                         cache_fh_len, cache_fh_type, afs_fh_acceptable, NULL);
348 #endif
349 }
350
351 static inline int
352 afs_get_fh_from_dentry(struct dentry *dp, afs_ufs_dcache_id_t *ainode, int *max_lenp) {
353     if (dp->d_sb->s_export_op->encode_fh)
354         return dp->d_sb->s_export_op->encode_fh(dp, &ainode->raw[0], max_lenp, 0);
355 #if defined(NEW_EXPORT_OPS)
356     /* If fs doesn't provide an encode_fh method, assume the default INO32 type */
357     *max_lenp = sizeof(struct fid)/4;
358     ainode->fh.i32.ino = dp->d_inode->i_ino;
359     ainode->fh.i32.gen = dp->d_inode->i_generation;
360     return FILEID_INO32_GEN;
361 #else
362     /* or call the default encoding function for the old API */
363     return export_op_default.encode_fh(dp, &ainode->raw[0], max_lenp, 0);
364 #endif
365 }
366
367 static inline void
368 afs_init_sb_export_ops(struct super_block *sb) {
369 #if !defined(NEW_EXPORT_OPS)
370     /*
371      * decode_fh will call this function.  If not defined for this FS, make
372      * sure it points to the default
373      */
374     if (!sb->s_export_op->find_exported_dentry) {
375         /* Some kernels (at least 2.6.9) do not prototype find_exported_dentry,
376          * even though it is exported, so prototype it ourselves. Newer
377          * kernels do prototype it, but as long as our protoype matches the
378          * real one (the signature never changed before NEW_EXPORT_OPS came
379          * into play), there should be no problems. */
380         extern struct dentry * find_exported_dentry(struct super_block *sb, void *obj, void *parent,
381                                                     int (*acceptable)(void *context, struct dentry *de),
382                                                     void *context);
383         sb->s_export_op->find_exported_dentry = find_exported_dentry;
384     }
385 #endif
386 }
387
388 static inline void
389 afs_linux_lock_inode(struct inode *ip) {
390 #ifdef STRUCT_INODE_HAS_I_MUTEX
391     mutex_lock(&ip->i_mutex);
392 #else
393     down(&ip->i_sem);
394 #endif
395 }
396
397 static inline void
398 afs_linux_unlock_inode(struct inode *ip) {
399 #ifdef STRUCT_INODE_HAS_I_MUTEX
400     mutex_unlock(&ip->i_mutex);
401 #else
402     up(&ip->i_sem);
403 #endif
404 }
405
406 static inline int
407 afs_inode_setattr(struct osi_file *afile, struct iattr *newattrs) {
408
409     int code = 0;
410     struct inode *inode = OSIFILE_INODE(afile);
411 #if !defined(HAVE_LINUX_INODE_SETATTR)
412     code = inode->i_op->setattr(afile->filp->f_dentry, newattrs);
413 #elif defined(INODE_SETATTR_NOT_VOID)
414     if (inode->i_op && inode->i_op->setattr)
415         code = inode->i_op->setattr(afile->filp->f_dentry, newattrs);
416     else
417         code = inode_setattr(inode, newattrs);
418 #else
419     inode_setattr(inode, newattrs);
420 #endif
421     return code;
422 }
423
424 #if defined(HAVE_LINUX_PATH_LOOKUP)
425 static inline int
426 afs_kern_path(char *aname, int flags, struct nameidata *nd) {
427     return path_lookup(aname, flags, nd);
428 }
429 #else
430 static inline int
431 afs_kern_path(char *aname, int flags, struct path *path) {
432     return kern_path(aname, flags, path);
433 }
434 #endif
435
436 static inline void
437 #if defined(HAVE_LINUX_PATH_LOOKUP)
438 afs_get_dentry_ref(struct nameidata *nd, struct vfsmount **mnt, struct dentry **dpp) {
439 #else
440 afs_get_dentry_ref(struct path *path, struct vfsmount **mnt, struct dentry **dpp) {
441 #endif
442 #if defined(STRUCT_NAMEIDATA_HAS_PATH)
443 # if defined(HAVE_LINUX_PATH_LOOKUP)
444     *dpp = dget(nd->path.dentry);
445     if (mnt)
446         *mnt = mntget(nd->path.mnt);
447     path_put(&nd->path);
448 # else
449     *dpp = dget(path->dentry);
450     if (mnt)
451         *mnt = mntget(path->mnt);
452     path_put(path);
453 # endif
454 #else
455     *dpp = dget(nd->dentry);
456     if (mnt)
457         *mnt = mntget(nd->mnt);
458     path_release(nd);
459 #endif
460 }
461
462 /* wait_event_freezable appeared with 2.6.24 */
463
464 /* These implement the original AFS wait behaviour, with respect to the
465  * refrigerator, rather than the behaviour of the current wait_event_freezable
466  * implementation.
467  */
468
469 #ifndef wait_event_freezable
470 # define wait_event_freezable(waitqueue, condition)                             \
471 ({                                                                              \
472     int _ret;                                                                   \
473     do {                                                                        \
474         _ret = wait_event_interruptible(waitqueue,                              \
475                                         (condition) || freezing(current));      \
476         if (_ret && !freezing(current))                                 \
477             break;                                                              \
478         else if (!(condition))                                                  \
479             _ret = -EINTR;                                                      \
480     } while (afs_try_to_freeze());                                              \
481     _ret;                                                                       \
482 })
483
484 # define wait_event_freezable_timeout(waitqueue, condition, timeout)            \
485 ({                                                                              \
486      int _ret;                                                                  \
487      do {                                                                       \
488         _ret = wait_event_interruptible_timeout(waitqueue,                      \
489                                                 (condition ||                   \
490                                                  freezing(current)),            \
491                                                 timeout);                       \
492      } while (afs_try_to_freeze());                                             \
493      _ret;                                                                      \
494 })
495 #endif
496
497 #endif /* AFS_LINUX_OSI_COMPAT_H */