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