fe8ed047f5eb2fbd2d49fbbf184a960d6159d8e4
[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 #if defined(STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT) && !defined(DCACHE_NEED_AUTOMOUNT)
31 # define DCACHE_NEED_AUTOMOUNT DMANAGED_AUTOMOUNT
32 #endif
33
34 #ifdef HAVE_LINUX_STRUCT_VFS_PATH
35 typedef struct vfs_path afs_linux_path_t;
36 #else
37 typedef struct path afs_linux_path_t;
38 #endif
39
40 #if defined(STRUCT_DENTRY_HAS_D_U_D_ALIAS)
41 # define d_alias d_u.d_alias
42 #endif
43
44 #if defined(STRUCT_FILE_HAS_F_PATH)
45 # if !defined(f_dentry)
46 #  define f_dentry f_path.dentry
47 # endif
48 #endif
49
50 #ifndef HAVE_LINUX_DO_SYNC_READ
51 static inline int
52 do_sync_read(struct file *fp, char *buf, size_t count, loff_t *offp) {
53     return generic_file_read(fp, buf, count, offp);
54 }
55
56 static inline int
57 do_sync_write(struct file *fp, char *buf, size_t count, loff_t *offp) {
58     return generic_file_write(fp, buf, count, offp);
59 }
60
61 #endif /* DO_SYNC_READ */
62
63 static inline int
64 afs_posix_lock_file(struct file *fp, struct file_lock *flp) {
65 #ifdef POSIX_LOCK_FILE_WAIT_ARG
66     return posix_lock_file(fp, flp, NULL);
67 #else
68     flp->fl_flags &=~ FL_SLEEP;
69     return posix_lock_file(fp, flp);
70 #endif
71 }
72
73 static inline void
74 afs_posix_test_lock(struct file *fp, struct file_lock *flp) {
75 #if defined(POSIX_TEST_LOCK_CONFLICT_ARG)
76     struct file_lock conflict;
77     if (posix_test_lock(fp, flp, &conflict)) {
78         locks_copy_lock(flp, &conflict);
79         flp->fl_type = F_UNLCK;
80     }
81 #elif defined(POSIX_TEST_LOCK_RETURNS_CONFLICT)
82     struct file_lock *conflict;
83     conflict = posix_test_lock(fp, flp);
84     if (conflict) {
85         locks_copy_lock(flp, conflict);
86         flp->fl_type = F_UNLCK;
87     }
88 #else
89     posix_test_lock(fp, flp);
90 #endif
91 }
92
93 #ifdef DCACHE_NFSFS_RENAMED
94 static inline void
95 afs_linux_clear_nfsfs_renamed(struct dentry *dp) {
96     spin_lock(&dp->d_lock);
97     dp->d_flags &= ~DCACHE_NFSFS_RENAMED;
98     spin_unlock(&dp->d_lock);
99 }
100
101 static inline void
102 afs_linux_set_nfsfs_renamed(struct dentry *dp) {
103     spin_lock(&dp->d_lock);
104     dp->d_flags |= DCACHE_NFSFS_RENAMED;
105     spin_unlock(&dp->d_lock);
106 }
107
108 static inline int
109 afs_linux_nfsfs_renamed(struct dentry *dp) {
110     return dp->d_flags & DCACHE_NFSFS_RENAMED;
111 }
112
113 #else
114 static inline void afs_linux_clear_nfsfs_renamed(void) { return; }
115 static inline void afs_linux_set_nfsfs_renamed(void) { return; }
116 #endif
117
118 #ifndef HAVE_LINUX_HLIST_UNHASHED
119 static void
120 hlist_unhashed(const struct hlist_node *h) {
121     return (!h->pprev == NULL);
122 }
123 #endif
124
125 #if defined(WRITEPAGE_ACTIVATE)
126 #define AOP_WRITEPAGE_ACTIVATE WRITEPAGE_ACTIVATE
127 #endif
128
129 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN) && !defined(HAVE_LINUX_GRAB_CACHE_PAGE_WRITE_BEGIN)
130 static inline struct page *
131 grab_cache_page_write_begin(struct address_space *mapping, pgoff_t index,
132                             unsigned int flags) {
133     return __grab_cache_page(mapping, index);
134 }
135 #endif
136
137 #if defined(HAVE_KMEM_CACHE_T)
138 #define afs_kmem_cache_t kmem_cache_t
139 #else
140 #define afs_kmem_cache_t struct kmem_cache
141 #endif
142
143 extern void init_once(void *);
144 #if defined(HAVE_KMEM_CACHE_T)
145 static inline void
146 init_once_func(void * foo, kmem_cache_t * cachep, unsigned long flags) {
147 #if defined(SLAB_CTOR_VERIFY)
148     if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
149         SLAB_CTOR_CONSTRUCTOR)
150 #endif
151     init_once(foo);
152 }
153 #elif defined(KMEM_CACHE_INIT)
154 static inline void
155 init_once_func(struct kmem_cache * cachep, void * foo) {
156     init_once(foo);
157 }
158 #elif !defined(KMEM_CACHE_CTOR_TAKES_VOID)
159 static inline void
160 init_once_func(void * foo, struct kmem_cache * cachep, unsigned long flags) {
161 #if defined(SLAB_CTOR_VERIFY)
162     if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
163         SLAB_CTOR_CONSTRUCTOR)
164 #endif
165     init_once(foo);
166 }
167 #else
168 static inline void
169 init_once_func(void * foo) {
170     init_once(foo);
171 }
172 #endif
173
174 #ifndef SLAB_RECLAIM_ACCOUNT
175 #define SLAB_RECLAIM_ACCOUNT 0
176 #endif
177
178 #if defined(SLAB_KERNEL)
179 #define KALLOC_TYPE SLAB_KERNEL
180 #else
181 #define KALLOC_TYPE GFP_KERNEL
182 #endif
183
184 #ifdef LINUX_KEYRING_SUPPORT
185 static inline struct key *
186 afs_linux_key_alloc(struct key_type *type, const char *desc, afs_kuid_t uid,
187                     afs_kgid_t gid, key_perm_t perm, unsigned long flags)
188 {
189 # if defined(KEY_ALLOC_NEEDS_STRUCT_TASK)
190     return key_alloc(type, desc, uid, gid, current, perm, flags);
191 # elif defined(KEY_ALLOC_NEEDS_CRED)
192     return key_alloc(type, desc, uid, gid, current_cred(), perm, flags);
193 # else
194     return key_alloc(type, desc, uid, gid, perm, flags);
195 # endif
196 }
197
198 # if defined(STRUCT_TASK_STRUCT_HAS_CRED)
199 static inline struct key *
200 afs_session_keyring(afs_ucred_t *cred)
201 {
202 #  if defined(STRUCT_CRED_HAS_SESSION_KEYRING)
203     return cred->session_keyring;
204 #  else
205     return cred->tgcred->session_keyring;
206 #  endif
207 }
208
209 static inline struct key*
210 afs_linux_search_keyring(afs_ucred_t *cred, struct key_type *type)
211 {
212     key_ref_t key_ref;
213
214     if (afs_session_keyring(cred)) {
215         key_ref = keyring_search(
216                       make_key_ref(afs_session_keyring(cred), 1),
217                       type, "_pag");
218         if (IS_ERR(key_ref))
219             return ERR_CAST(key_ref);
220
221         return key_ref_to_ptr(key_ref);
222     }
223
224     return ERR_PTR(-ENOKEY);
225 }
226 # else
227 static inline struct key*
228 afs_linux_search_keyring(afs_ucred_t *cred, struct key_type *type)
229 {
230     return request_key(type, "_pag", NULL);
231 }
232 # endif /* STRUCT_TASK_STRUCT_HAS_CRED */
233
234 static_inline struct key *
235 afs_set_session_keyring(struct key *keyring)
236 {
237     struct key *old;
238 #if defined(STRUCT_CRED_HAS_SESSION_KEYRING)
239     struct cred *new_creds;
240     old = current_session_keyring();
241     new_creds = prepare_creds();
242     rcu_assign_pointer(new_creds->session_keyring, keyring);
243     commit_creds(new_creds);
244 #else
245     spin_lock_irq(&current->sighand->siglock);
246     old = task_session_keyring(current);
247     smp_wmb();
248     task_session_keyring(current) = keyring;
249     spin_unlock_irq(&current->sighand->siglock);
250 #endif
251     return old;
252 }
253 #endif /* LINUX_KEYRING_SUPPORT */
254
255 #ifdef STRUCT_TASK_STRUCT_HAS_CRED
256 static inline int
257 afs_linux_cred_is_current(afs_ucred_t *cred)
258 {
259     return (cred == current_cred());
260 }
261 #else
262 static inline int
263 afs_linux_cred_is_current(afs_ucred_t *cred)
264 {
265     return 1;
266 }
267 #endif
268
269 #ifndef HAVE_LINUX_PAGE_OFFSET
270 static inline loff_t
271 page_offset(struct page *pp)
272 {
273     return (((loff_t) pp->index) << PAGE_CACHE_SHIFT);
274 }
275 #endif
276
277 #ifndef HAVE_LINUX_ZERO_USER_SEGMENTS
278 static inline void
279 zero_user_segments(struct page *pp, unsigned int from1, unsigned int to1,
280                    unsigned int from2, unsigned int to2)
281 {
282     void *base = kmap_atomic(pp, KM_USER0);
283
284     if (to1 > from1)
285         memset(base + from1, 0, to1 - from1);
286
287     if (to2 > from2)
288         memset(base + from2, 0, to2 - from2);
289
290     flush_dcache_page(pp);
291     kunmap_atomic(base, KM_USER0);
292 }
293
294 static inline void
295 zero_user_segment(struct page *pp, unsigned int from1, unsigned int to1)
296 {
297     zero_user_segments(pp, from1, to1, 0, 0);
298 }
299 #endif
300
301 #ifndef HAVE_LINUX_KERNEL_SETSOCKOPT
302 /* Available from 2.6.19 */
303
304 static inline int
305 kernel_setsockopt(struct socket *sockp, int level, int name, char *val,
306                   unsigned int len) {
307     mm_segment_t old_fs = get_fs();
308     int ret;
309
310     set_fs(get_ds());
311     ret = sockp->ops->setsockopt(sockp, level, name, val, len);
312     set_fs(old_fs);
313
314     return ret;
315 }
316
317 static inline int
318 kernel_getsockopt(struct socket *sockp, int level, int name, char *val,
319                   int *len) {
320     mm_segment_t old_fs = get_fs();
321     int ret;
322
323     set_fs(get_ds());
324     ret = sockp->ops->getsockopt(sockp, level, name, val, len);
325     set_fs(old_fs);
326
327     return ret;
328 }
329 #endif
330
331 #ifdef HAVE_TRY_TO_FREEZE
332 static inline int
333 afs_try_to_freeze(void) {
334 # ifdef LINUX_REFRIGERATOR_TAKES_PF_FREEZE
335     return try_to_freeze(PF_FREEZE);
336 # else
337     return try_to_freeze();
338 # endif
339 }
340 #else
341 static inline int
342 afs_try_to_freeze(void) {
343 # ifdef CONFIG_PM
344     if (current->flags & PF_FREEZE) {
345         refrigerator(PF_FREEZE);
346         return 1;
347     }
348 # endif
349     return 0;
350 }
351 #endif
352
353 /* The commit which changed refrigerator so that it takes no arguments
354  * also added freezing(), so if LINUX_REFRIGERATOR_TAKES_PF_FREEZE is
355  * true, the kernel doesn't have a freezing() function.
356  */
357 #ifdef LINUX_REFRIGERATOR_TAKES_PF_FREEZE
358 static inline int
359 freezing(struct task_struct *p)
360 {
361 # ifdef CONFIG_PM
362     return p->flags & PF_FREEZE;
363 # else
364     return 0;
365 # endif
366 }
367 #endif
368
369 #if !defined(HAVE_LINUX_PAGECHECKED)
370 # if defined(HAVE_LINUX_PAGEFSMISC)
371 #  include <linux/page-flags.h>
372
373 #  define PageChecked(p)            PageFsMisc((p))
374 #  define SetPageChecked(p)         SetPageFsMisc((p))
375 #  define ClearPageChecked(p)       ClearPageFsMisc((p))
376
377 # endif
378 #endif
379
380 #if !defined(NEW_EXPORT_OPS)
381 extern struct export_operations export_op_default;
382 #endif
383
384 static inline struct dentry *
385 afs_get_dentry_from_fh(struct super_block *afs_cacheSBp, afs_dcache_id_t *ainode,
386                 int cache_fh_len, int cache_fh_type,
387                 int (*afs_fh_acceptable)(void *, struct dentry *)) {
388 #if defined(NEW_EXPORT_OPS)
389     return afs_cacheSBp->s_export_op->fh_to_dentry(afs_cacheSBp, &ainode->ufs.fh,
390                 cache_fh_len, cache_fh_type);
391 #else
392     if (afs_cacheSBp->s_export_op && afs_cacheSBp->s_export_op->decode_fh)
393         return afs_cacheSBp->s_export_op->decode_fh(afs_cacheSBp, ainode->ufs.raw,
394                         cache_fh_len, cache_fh_type, afs_fh_acceptable, NULL);
395     else
396         return export_op_default.decode_fh(afs_cacheSBp, ainode->ufs.raw,
397                         cache_fh_len, cache_fh_type, afs_fh_acceptable, NULL);
398 #endif
399 }
400
401 static inline int
402 afs_get_fh_from_dentry(struct dentry *dp, afs_ufs_dcache_id_t *ainode, int *max_lenp) {
403     if (dp->d_sb->s_export_op->encode_fh)
404 #if defined(EXPORT_OP_ENCODE_FH_TAKES_INODES)
405         return dp->d_sb->s_export_op->encode_fh(dp->d_inode, &ainode->raw[0], max_lenp, NULL);
406 #else
407         return dp->d_sb->s_export_op->encode_fh(dp, &ainode->raw[0], max_lenp, 0);
408 #endif
409 #if defined(NEW_EXPORT_OPS)
410     /* If fs doesn't provide an encode_fh method, assume the default INO32 type */
411     *max_lenp = sizeof(struct fid)/4;
412     ainode->fh.i32.ino = dp->d_inode->i_ino;
413     ainode->fh.i32.gen = dp->d_inode->i_generation;
414     return FILEID_INO32_GEN;
415 #else
416     /* or call the default encoding function for the old API */
417     return export_op_default.encode_fh(dp, &ainode->raw[0], max_lenp, 0);
418 #endif
419 }
420
421 static inline void
422 afs_init_sb_export_ops(struct super_block *sb) {
423 #if !defined(NEW_EXPORT_OPS)
424     /*
425      * decode_fh will call this function.  If not defined for this FS, make
426      * sure it points to the default
427      */
428     if (!sb->s_export_op->find_exported_dentry) {
429         /* Some kernels (at least 2.6.9) do not prototype find_exported_dentry,
430          * even though it is exported, so prototype it ourselves. Newer
431          * kernels do prototype it, but as long as our protoype matches the
432          * real one (the signature never changed before NEW_EXPORT_OPS came
433          * into play), there should be no problems. */
434         extern struct dentry * find_exported_dentry(struct super_block *sb, void *obj, void *parent,
435                                                     int (*acceptable)(void *context, struct dentry *de),
436                                                     void *context);
437         sb->s_export_op->find_exported_dentry = find_exported_dentry;
438     }
439 #endif
440 }
441
442 static inline void
443 afs_linux_lock_inode(struct inode *ip) {
444 #ifdef STRUCT_INODE_HAS_I_MUTEX
445     mutex_lock(&ip->i_mutex);
446 #else
447     down(&ip->i_sem);
448 #endif
449 }
450
451 static inline void
452 afs_linux_unlock_inode(struct inode *ip) {
453 #ifdef STRUCT_INODE_HAS_I_MUTEX
454     mutex_unlock(&ip->i_mutex);
455 #else
456     up(&ip->i_sem);
457 #endif
458 }
459
460 static inline int
461 afs_inode_setattr(struct osi_file *afile, struct iattr *newattrs) {
462
463     int code = 0;
464     struct inode *inode = OSIFILE_INODE(afile);
465 #if !defined(HAVE_LINUX_INODE_SETATTR)
466     code = inode->i_op->setattr(afile->filp->f_dentry, newattrs);
467 #elif defined(INODE_SETATTR_NOT_VOID)
468     if (inode->i_op && inode->i_op->setattr)
469         code = inode->i_op->setattr(afile->filp->f_dentry, newattrs);
470     else
471         code = inode_setattr(inode, newattrs);
472 #else
473     inode_setattr(inode, newattrs);
474 #endif
475     return code;
476 }
477
478 #if defined(HAVE_LINUX_PATH_LOOKUP)
479 static inline int
480 afs_kern_path(char *aname, int flags, struct nameidata *nd) {
481     return path_lookup(aname, flags, nd);
482 }
483 #else
484 static inline int
485 afs_kern_path(char *aname, int flags, afs_linux_path_t *path) {
486     return kern_path(aname, flags, path);
487 }
488 #endif
489
490 static inline void
491 #if defined(HAVE_LINUX_PATH_LOOKUP)
492 afs_get_dentry_ref(struct nameidata *nd, struct vfsmount **mnt, struct dentry **dpp) {
493 #else
494 afs_get_dentry_ref(afs_linux_path_t *path, struct vfsmount **mnt, struct dentry **dpp) {
495 #endif
496 #if defined(HAVE_LINUX_PATH_LOOKUP)
497 # if defined(STRUCT_NAMEIDATA_HAS_PATH)
498     *dpp = dget(nd->path.dentry);
499     if (mnt)
500         *mnt = mntget(nd->path.mnt);
501     path_put(&nd->path);
502 # else
503     *dpp = dget(nd->dentry);
504     if (mnt)
505         *mnt = mntget(nd->mnt);
506     path_release(nd);
507 # endif
508 #else
509     *dpp = dget(path->dentry);
510     if (mnt)
511         *mnt = mntget(path->mnt);
512     path_put(path);
513 #endif
514 }
515
516 /* wait_event_freezable appeared with 2.6.24 */
517
518 /* These implement the original AFS wait behaviour, with respect to the
519  * refrigerator, rather than the behaviour of the current wait_event_freezable
520  * implementation.
521  */
522
523 #ifndef wait_event_freezable
524 # define wait_event_freezable(waitqueue, condition)                             \
525 ({                                                                              \
526     int _ret;                                                                   \
527     do {                                                                        \
528         _ret = wait_event_interruptible(waitqueue,                              \
529                                         (condition) || freezing(current));      \
530         if (_ret && !freezing(current))                                 \
531             break;                                                              \
532         else if (!(condition))                                                  \
533             _ret = -EINTR;                                                      \
534     } while (afs_try_to_freeze());                                              \
535     _ret;                                                                       \
536 })
537
538 # define wait_event_freezable_timeout(waitqueue, condition, timeout)            \
539 ({                                                                              \
540      int _ret;                                                                  \
541      do {                                                                       \
542         _ret = wait_event_interruptible_timeout(waitqueue,                      \
543                                                 (condition ||                   \
544                                                  freezing(current)),            \
545                                                 timeout);                       \
546      } while (afs_try_to_freeze());                                             \
547      _ret;                                                                      \
548 })
549 #endif
550
551 #if defined(STRUCT_TASK_STRUCT_HAS_CRED)
552 static inline struct file *
553 afs_dentry_open(struct dentry *dp, struct vfsmount *mnt, int flags, const struct cred *creds) {
554 #if defined(DENTRY_OPEN_TAKES_PATH)
555     afs_linux_path_t path;
556     struct file *filp;
557     path.mnt = mnt;
558     path.dentry = dp;
559     /* note that dentry_open will path_get for us */
560     filp = dentry_open(&path, flags, creds);
561     return filp;
562 #else
563     return dentry_open(dget(dp), mntget(mnt), flags, creds);
564 #endif
565 }
566 #endif
567
568 static inline int
569 afs_truncate(struct inode *inode, int len)
570 {
571     int code;
572 #if defined(STRUCT_INODE_OPERATIONS_HAS_TRUNCATE)
573     code = vmtruncate(inode, len);
574 #else
575     code = inode_newsize_ok(inode, len);
576     if (!code)
577         truncate_setsize(inode, len);
578 #endif
579     return code;
580 }
581
582 static inline struct proc_dir_entry *
583 afs_proc_create(char *name, umode_t mode, struct proc_dir_entry *parent, struct file_operations *fops) {
584 #if defined(HAVE_LINUX_PROC_CREATE)
585     return proc_create(name, mode, parent, fops);
586 #else
587     struct proc_dir_entry *entry;
588     entry = create_proc_entry(name, mode, parent);
589     if (entry)
590         entry->proc_fops = fops;
591     return entry;
592 #endif
593 }
594
595 static inline int
596 afs_dentry_count(struct dentry *dp)
597 {
598 #if defined(HAVE_LINUX_D_COUNT)
599     return d_count(dp);
600 #elif defined(D_COUNT_INT)
601     return dp->d_count;
602 #else
603     return atomic_read(&dp->d_count);
604 #endif
605 }
606
607 static inline void
608 afs_maybe_shrink_dcache(struct dentry *dp)
609 {
610 #if defined(HAVE_LINUX_D_COUNT) || defined(D_COUNT_INT)
611     spin_lock(&dp->d_lock);
612     if (afs_dentry_count(dp) > 1) {
613         spin_unlock(&dp->d_lock);
614         shrink_dcache_parent(dp);
615     } else
616         spin_unlock(&dp->d_lock);
617 #else
618     if (afs_dentry_count(dp) > 1)
619         shrink_dcache_parent(dp);
620 #endif
621 }
622
623 static inline int
624 afs_d_invalidate(struct dentry *dp)
625 {
626 #if defined(D_INVALIDATE_IS_VOID)
627     d_invalidate(dp);
628     return 0;
629 #else
630     return d_invalidate(dp);
631 #endif
632 }
633
634 #endif /* AFS_LINUX_OSI_COMPAT_H */