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