Linux 4.6: rm PAGE_CACHE_* and page_cache_{get,release} macros
[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_NEEDS_STRUCT_TASK)
194     return key_alloc(type, desc, uid, gid, current, perm, flags);
195 # elif defined(KEY_ALLOC_NEEDS_CRED)
196     return key_alloc(type, desc, uid, gid, current_cred(), perm, flags);
197 # else
198     return key_alloc(type, desc, uid, gid, perm, flags);
199 # endif
200 }
201
202 # if defined(STRUCT_TASK_STRUCT_HAS_CRED)
203 static inline struct key *
204 afs_session_keyring(afs_ucred_t *cred)
205 {
206 #  if defined(STRUCT_CRED_HAS_SESSION_KEYRING)
207     return cred->session_keyring;
208 #  else
209     return cred->tgcred->session_keyring;
210 #  endif
211 }
212
213 static inline struct key*
214 afs_linux_search_keyring(afs_ucred_t *cred, struct key_type *type)
215 {
216     key_ref_t key_ref;
217
218     if (afs_session_keyring(cred)) {
219         key_ref = keyring_search(
220                       make_key_ref(afs_session_keyring(cred), 1),
221                       type, "_pag");
222         if (IS_ERR(key_ref))
223             return ERR_CAST(key_ref);
224
225         return key_ref_to_ptr(key_ref);
226     }
227
228     return ERR_PTR(-ENOKEY);
229 }
230 # else
231 static inline struct key*
232 afs_linux_search_keyring(afs_ucred_t *cred, struct key_type *type)
233 {
234     return request_key(type, "_pag", NULL);
235 }
236 # endif /* STRUCT_TASK_STRUCT_HAS_CRED */
237
238 static_inline struct key *
239 afs_set_session_keyring(struct key *keyring)
240 {
241     struct key *old;
242 #if defined(STRUCT_CRED_HAS_SESSION_KEYRING)
243     struct cred *new_creds;
244     old = current_session_keyring();
245     new_creds = prepare_creds();
246     rcu_assign_pointer(new_creds->session_keyring, keyring);
247     commit_creds(new_creds);
248 #else
249     spin_lock_irq(&current->sighand->siglock);
250     old = task_session_keyring(current);
251     smp_wmb();
252     task_session_keyring(current) = keyring;
253     spin_unlock_irq(&current->sighand->siglock);
254 #endif
255     return old;
256 }
257 #endif /* LINUX_KEYRING_SUPPORT */
258
259 #ifdef STRUCT_TASK_STRUCT_HAS_CRED
260 static inline int
261 afs_linux_cred_is_current(afs_ucred_t *cred)
262 {
263     return (cred == current_cred());
264 }
265 #else
266 static inline int
267 afs_linux_cred_is_current(afs_ucred_t *cred)
268 {
269     return 1;
270 }
271 #endif
272
273 #ifndef HAVE_LINUX_PAGE_OFFSET
274 static inline loff_t
275 page_offset(struct page *pp)
276 {
277     return (((loff_t) pp->index) << PAGE_SHIFT);
278 }
279 #endif
280
281 #ifndef HAVE_LINUX_ZERO_USER_SEGMENTS
282 static inline void
283 zero_user_segments(struct page *pp, unsigned int from1, unsigned int to1,
284                    unsigned int from2, unsigned int to2)
285 {
286     void *base = kmap_atomic(pp, KM_USER0);
287
288     if (to1 > from1)
289         memset(base + from1, 0, to1 - from1);
290
291     if (to2 > from2)
292         memset(base + from2, 0, to2 - from2);
293
294     flush_dcache_page(pp);
295     kunmap_atomic(base, KM_USER0);
296 }
297
298 static inline void
299 zero_user_segment(struct page *pp, unsigned int from1, unsigned int to1)
300 {
301     zero_user_segments(pp, from1, to1, 0, 0);
302 }
303 #endif
304
305 #ifndef HAVE_LINUX_KERNEL_SETSOCKOPT
306 /* Available from 2.6.19 */
307
308 static inline int
309 kernel_setsockopt(struct socket *sockp, int level, int name, char *val,
310                   unsigned int len) {
311     mm_segment_t old_fs = get_fs();
312     int ret;
313
314     set_fs(get_ds());
315     ret = sockp->ops->setsockopt(sockp, level, name, val, len);
316     set_fs(old_fs);
317
318     return ret;
319 }
320
321 static inline int
322 kernel_getsockopt(struct socket *sockp, int level, int name, char *val,
323                   int *len) {
324     mm_segment_t old_fs = get_fs();
325     int ret;
326
327     set_fs(get_ds());
328     ret = sockp->ops->getsockopt(sockp, level, name, val, len);
329     set_fs(old_fs);
330
331     return ret;
332 }
333 #endif
334
335 #ifdef HAVE_TRY_TO_FREEZE
336 static inline int
337 afs_try_to_freeze(void) {
338 # ifdef LINUX_REFRIGERATOR_TAKES_PF_FREEZE
339     return try_to_freeze(PF_FREEZE);
340 # else
341     return try_to_freeze();
342 # endif
343 }
344 #else
345 static inline int
346 afs_try_to_freeze(void) {
347 # ifdef CONFIG_PM
348     if (current->flags & PF_FREEZE) {
349         refrigerator(PF_FREEZE);
350         return 1;
351     }
352 # endif
353     return 0;
354 }
355 #endif
356
357 /* The commit which changed refrigerator so that it takes no arguments
358  * also added freezing(), so if LINUX_REFRIGERATOR_TAKES_PF_FREEZE is
359  * true, the kernel doesn't have a freezing() function.
360  */
361 #ifdef LINUX_REFRIGERATOR_TAKES_PF_FREEZE
362 static inline int
363 freezing(struct task_struct *p)
364 {
365 # ifdef CONFIG_PM
366     return p->flags & PF_FREEZE;
367 # else
368     return 0;
369 # endif
370 }
371 #endif
372
373 #if !defined(HAVE_LINUX_PAGECHECKED)
374 # if defined(HAVE_LINUX_PAGEFSMISC)
375 #  include <linux/page-flags.h>
376
377 #  define PageChecked(p)            PageFsMisc((p))
378 #  define SetPageChecked(p)         SetPageFsMisc((p))
379 #  define ClearPageChecked(p)       ClearPageFsMisc((p))
380
381 # endif
382 #endif
383
384 #if !defined(NEW_EXPORT_OPS)
385 extern struct export_operations export_op_default;
386 #endif
387
388 static inline struct dentry *
389 afs_get_dentry_from_fh(struct super_block *afs_cacheSBp, afs_dcache_id_t *ainode,
390                 int cache_fh_len, int cache_fh_type,
391                 int (*afs_fh_acceptable)(void *, struct dentry *)) {
392 #if defined(NEW_EXPORT_OPS)
393     return afs_cacheSBp->s_export_op->fh_to_dentry(afs_cacheSBp, &ainode->ufs.fh,
394                 cache_fh_len, cache_fh_type);
395 #else
396     if (afs_cacheSBp->s_export_op && afs_cacheSBp->s_export_op->decode_fh)
397         return afs_cacheSBp->s_export_op->decode_fh(afs_cacheSBp, ainode->ufs.raw,
398                         cache_fh_len, cache_fh_type, afs_fh_acceptable, NULL);
399     else
400         return export_op_default.decode_fh(afs_cacheSBp, ainode->ufs.raw,
401                         cache_fh_len, cache_fh_type, afs_fh_acceptable, NULL);
402 #endif
403 }
404
405 static inline int
406 afs_get_fh_from_dentry(struct dentry *dp, afs_ufs_dcache_id_t *ainode, int *max_lenp) {
407     if (dp->d_sb->s_export_op->encode_fh)
408 #if defined(EXPORT_OP_ENCODE_FH_TAKES_INODES)
409         return dp->d_sb->s_export_op->encode_fh(dp->d_inode, &ainode->raw[0], max_lenp, NULL);
410 #else
411         return dp->d_sb->s_export_op->encode_fh(dp, &ainode->raw[0], max_lenp, 0);
412 #endif
413 #if defined(NEW_EXPORT_OPS)
414     /* If fs doesn't provide an encode_fh method, assume the default INO32 type */
415     *max_lenp = sizeof(struct fid)/4;
416     ainode->fh.i32.ino = dp->d_inode->i_ino;
417     ainode->fh.i32.gen = dp->d_inode->i_generation;
418     return FILEID_INO32_GEN;
419 #else
420     /* or call the default encoding function for the old API */
421     return export_op_default.encode_fh(dp, &ainode->raw[0], max_lenp, 0);
422 #endif
423 }
424
425 static inline void
426 afs_init_sb_export_ops(struct super_block *sb) {
427 #if !defined(NEW_EXPORT_OPS)
428     /*
429      * decode_fh will call this function.  If not defined for this FS, make
430      * sure it points to the default
431      */
432     if (!sb->s_export_op->find_exported_dentry) {
433         /* Some kernels (at least 2.6.9) do not prototype find_exported_dentry,
434          * even though it is exported, so prototype it ourselves. Newer
435          * kernels do prototype it, but as long as our protoype matches the
436          * real one (the signature never changed before NEW_EXPORT_OPS came
437          * into play), there should be no problems. */
438         extern struct dentry * find_exported_dentry(struct super_block *sb, void *obj, void *parent,
439                                                     int (*acceptable)(void *context, struct dentry *de),
440                                                     void *context);
441         sb->s_export_op->find_exported_dentry = find_exported_dentry;
442     }
443 #endif
444 }
445
446 static inline void
447 afs_linux_lock_inode(struct inode *ip) {
448 #if defined(HAVE_LINUX_INODE_LOCK)
449     inode_lock(ip);
450 #elif defined(STRUCT_INODE_HAS_I_MUTEX)
451     mutex_lock(&ip->i_mutex);
452 #else
453     down(&ip->i_sem);
454 #endif
455 }
456
457 static inline void
458 afs_linux_unlock_inode(struct inode *ip) {
459 #if defined(HAVE_LINUX_INODE_LOCK)
460     inode_unlock(ip);
461 #elif defined(STRUCT_INODE_HAS_I_MUTEX)
462     mutex_unlock(&ip->i_mutex);
463 #else
464     up(&ip->i_sem);
465 #endif
466 }
467
468 static inline int
469 afs_inode_setattr(struct osi_file *afile, struct iattr *newattrs) {
470
471     int code = 0;
472     struct inode *inode = OSIFILE_INODE(afile);
473 #if !defined(HAVE_LINUX_INODE_SETATTR)
474     code = inode->i_op->setattr(afile->filp->f_dentry, newattrs);
475 #elif defined(INODE_SETATTR_NOT_VOID)
476     if (inode->i_op && inode->i_op->setattr)
477         code = inode->i_op->setattr(afile->filp->f_dentry, newattrs);
478     else
479         code = inode_setattr(inode, newattrs);
480 #else
481     inode_setattr(inode, newattrs);
482 #endif
483     return code;
484 }
485
486 #if defined(HAVE_LINUX_PATH_LOOKUP)
487 static inline int
488 afs_kern_path(char *aname, int flags, struct nameidata *nd) {
489     return path_lookup(aname, flags, nd);
490 }
491 #else
492 static inline int
493 afs_kern_path(char *aname, int flags, afs_linux_path_t *path) {
494     return kern_path(aname, flags, path);
495 }
496 #endif
497
498 static inline void
499 #if defined(HAVE_LINUX_PATH_LOOKUP)
500 afs_get_dentry_ref(struct nameidata *nd, struct vfsmount **mnt, struct dentry **dpp) {
501 #else
502 afs_get_dentry_ref(afs_linux_path_t *path, struct vfsmount **mnt, struct dentry **dpp) {
503 #endif
504 #if defined(HAVE_LINUX_PATH_LOOKUP)
505 # if defined(STRUCT_NAMEIDATA_HAS_PATH)
506     *dpp = dget(nd->path.dentry);
507     if (mnt)
508         *mnt = mntget(nd->path.mnt);
509     path_put(&nd->path);
510 # else
511     *dpp = dget(nd->dentry);
512     if (mnt)
513         *mnt = mntget(nd->mnt);
514     path_release(nd);
515 # endif
516 #else
517     *dpp = dget(path->dentry);
518     if (mnt)
519         *mnt = mntget(path->mnt);
520     path_put(path);
521 #endif
522 }
523
524 /* wait_event_freezable appeared with 2.6.24 */
525
526 /* These implement the original AFS wait behaviour, with respect to the
527  * refrigerator, rather than the behaviour of the current wait_event_freezable
528  * implementation.
529  */
530
531 #ifndef wait_event_freezable
532 # define wait_event_freezable(waitqueue, condition)                             \
533 ({                                                                              \
534     int _ret;                                                                   \
535     do {                                                                        \
536         _ret = wait_event_interruptible(waitqueue,                              \
537                                         (condition) || freezing(current));      \
538         if (_ret && !freezing(current))                                 \
539             break;                                                              \
540         else if (!(condition))                                                  \
541             _ret = -EINTR;                                                      \
542     } while (afs_try_to_freeze());                                              \
543     _ret;                                                                       \
544 })
545
546 # define wait_event_freezable_timeout(waitqueue, condition, timeout)            \
547 ({                                                                              \
548      int _ret;                                                                  \
549      do {                                                                       \
550         _ret = wait_event_interruptible_timeout(waitqueue,                      \
551                                                 (condition ||                   \
552                                                  freezing(current)),            \
553                                                 timeout);                       \
554      } while (afs_try_to_freeze());                                             \
555      _ret;                                                                      \
556 })
557 #endif
558
559 #if defined(STRUCT_TASK_STRUCT_HAS_CRED)
560 static inline struct file *
561 afs_dentry_open(struct dentry *dp, struct vfsmount *mnt, int flags, const struct cred *creds) {
562 #if defined(DENTRY_OPEN_TAKES_PATH)
563     afs_linux_path_t path;
564     struct file *filp;
565     path.mnt = mnt;
566     path.dentry = dp;
567     /* note that dentry_open will path_get for us */
568     filp = dentry_open(&path, flags, creds);
569     return filp;
570 #else
571     return dentry_open(dget(dp), mntget(mnt), flags, creds);
572 #endif
573 }
574 #endif
575
576 static inline int
577 afs_truncate(struct inode *inode, int len)
578 {
579     int code;
580 #if defined(STRUCT_INODE_OPERATIONS_HAS_TRUNCATE)
581     code = vmtruncate(inode, len);
582 #else
583     code = inode_newsize_ok(inode, len);
584     if (!code)
585         truncate_setsize(inode, len);
586 #endif
587     return code;
588 }
589
590 static inline struct proc_dir_entry *
591 afs_proc_create(char *name, umode_t mode, struct proc_dir_entry *parent, struct file_operations *fops) {
592 #if defined(HAVE_LINUX_PROC_CREATE)
593     return proc_create(name, mode, parent, fops);
594 #else
595     struct proc_dir_entry *entry;
596     entry = create_proc_entry(name, mode, parent);
597     if (entry)
598         entry->proc_fops = fops;
599     return entry;
600 #endif
601 }
602
603 static inline int
604 afs_dentry_count(struct dentry *dp)
605 {
606 #if defined(HAVE_LINUX_D_COUNT)
607     return d_count(dp);
608 #elif defined(D_COUNT_INT)
609     return dp->d_count;
610 #else
611     return atomic_read(&dp->d_count);
612 #endif
613 }
614
615 static inline void
616 afs_maybe_shrink_dcache(struct dentry *dp)
617 {
618 #if defined(HAVE_LINUX_D_COUNT) || defined(D_COUNT_INT)
619     spin_lock(&dp->d_lock);
620     if (afs_dentry_count(dp) > 1) {
621         spin_unlock(&dp->d_lock);
622         shrink_dcache_parent(dp);
623     } else
624         spin_unlock(&dp->d_lock);
625 #else
626     if (afs_dentry_count(dp) > 1)
627         shrink_dcache_parent(dp);
628 #endif
629 }
630
631 static inline int
632 afs_d_invalidate(struct dentry *dp)
633 {
634 #if defined(D_INVALIDATE_IS_VOID)
635     d_invalidate(dp);
636     return 0;
637 #else
638     return d_invalidate(dp);
639 #endif
640 }
641
642 static inline int
643 afs_file_read(struct file *filp, char __user *buf, size_t len, loff_t *pos)
644 {
645 #if defined(HAVE_LINUX___VFS_READ)
646     return __vfs_read(filp, buf, len, pos);
647 #else
648     return filp->f_op->read(filp, buf, len, pos);
649 #endif
650 }
651
652 static inline int
653 afs_file_write(struct file *filp, char __user *buf, size_t len, loff_t *pos)
654 {
655 #if defined(HAVE_LINUX___VFS_READ)
656     return __vfs_write(filp, buf, len, pos);
657 #else
658     return filp->f_op->write(filp, buf, len, pos);
659 #endif
660 }
661
662 #endif /* AFS_LINUX_OSI_COMPAT_H */