venus: Remove dedebug
[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(HAVE_LINUX_FILELOCK_H)
17 # include <linux/filelock.h>
18 #endif
19
20 #if defined(LINUX_KEYRING_SUPPORT)
21 # include <linux/rwsem.h>
22 # include <linux/key.h>
23 # if defined(HAVE_LINUX_KEY_TYPE_H)
24 #  include <linux/key-type.h>
25 # endif
26 # ifndef KEY_ALLOC_IN_QUOTA
27 /* Before these flags were added in Linux commit v2.6.18-rc1~816,
28  * key_alloc just took a boolean not_in_quota */
29 #  define KEY_ALLOC_IN_QUOTA 0
30 #  define KEY_ALLOC_NOT_IN_QUOTA 1
31 # endif
32 #endif
33
34 #if !defined(HAVE_LINUX_KTHREAD_COMPLETE_AND_EXIT)
35 # define kthread_complete_and_exit complete_and_exit
36 #endif
37
38 #if defined(STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT) && !defined(DCACHE_NEED_AUTOMOUNT)
39 # define DCACHE_NEED_AUTOMOUNT DMANAGED_AUTOMOUNT
40 #endif
41
42 #ifdef HAVE_LINUX_STRUCT_VFS_PATH
43 typedef struct vfs_path afs_linux_path_t;
44 #else
45 typedef struct path afs_linux_path_t;
46 #endif
47
48 #if defined(STRUCT_DENTRY_HAS_D_U_D_ALIAS)
49 # define d_alias d_u.d_alias
50 #endif
51
52 #if defined(STRUCT_DENTRY_HAS_D_U_D_CHILD)
53 # define d_child d_u.d_child
54 #endif
55
56 #if defined(STRUCT_FILE_HAS_F_PATH)
57 # if !defined(f_dentry)
58 #  define f_dentry f_path.dentry
59 # endif
60 #endif
61
62 #ifndef HAVE_LINUX_FILE_DENTRY
63 #define file_dentry(file) ((file)->f_dentry)
64 #endif
65
66 #if defined(HAVE_LINUX_LOCKS_LOCK_FILE_WAIT)
67 # define flock_lock_file_wait locks_lock_file_wait
68 #endif
69
70 #if !defined(HAVE_LINUX_DO_SYNC_READ) && !defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER)
71 static inline int
72 do_sync_read(struct file *fp, char *buf, size_t count, loff_t *offp) {
73     return generic_file_read(fp, buf, count, offp);
74 }
75
76 static inline int
77 do_sync_write(struct file *fp, char *buf, size_t count, loff_t *offp) {
78     return generic_file_write(fp, buf, count, offp);
79 }
80
81 #endif /* DO_SYNC_READ */
82
83 static inline int
84 afs_posix_lock_file(struct file *fp, struct file_lock *flp) {
85 #ifdef POSIX_LOCK_FILE_WAIT_ARG
86     return posix_lock_file(fp, flp, NULL);
87 #else
88     flp->fl_flags &=~ FL_SLEEP;
89     return posix_lock_file(fp, flp);
90 #endif
91 }
92
93 static inline void
94 afs_posix_test_lock(struct file *fp, struct file_lock *flp) {
95 #if defined(POSIX_TEST_LOCK_CONFLICT_ARG)
96     struct file_lock conflict;
97     if (posix_test_lock(fp, flp, &conflict)) {
98         locks_copy_lock(flp, &conflict);
99         flp->fl_type = F_UNLCK;
100     }
101 #elif defined(POSIX_TEST_LOCK_RETURNS_CONFLICT)
102     struct file_lock *conflict;
103     conflict = posix_test_lock(fp, flp);
104     if (conflict) {
105         locks_copy_lock(flp, conflict);
106         flp->fl_type = F_UNLCK;
107     }
108 #else
109     posix_test_lock(fp, flp);
110 #endif
111 }
112
113 #ifdef DCACHE_NFSFS_RENAMED
114 static inline void
115 afs_linux_clear_nfsfs_renamed(struct dentry *dp) {
116     spin_lock(&dp->d_lock);
117     dp->d_flags &= ~DCACHE_NFSFS_RENAMED;
118     spin_unlock(&dp->d_lock);
119 }
120
121 static inline void
122 afs_linux_set_nfsfs_renamed(struct dentry *dp) {
123     spin_lock(&dp->d_lock);
124     dp->d_flags |= DCACHE_NFSFS_RENAMED;
125     spin_unlock(&dp->d_lock);
126 }
127
128 static inline int
129 afs_linux_nfsfs_renamed(struct dentry *dp) {
130     return dp->d_flags & DCACHE_NFSFS_RENAMED;
131 }
132
133 #else
134 static inline void afs_linux_clear_nfsfs_renamed(void) { return; }
135 static inline void afs_linux_set_nfsfs_renamed(void) { return; }
136 #endif
137
138 #ifndef HAVE_LINUX_HLIST_UNHASHED
139 static void
140 hlist_unhashed(const struct hlist_node *h) {
141     return (!h->pprev == NULL);
142 }
143 #endif
144
145 #if defined(WRITEPAGE_ACTIVATE)
146 #define AOP_WRITEPAGE_ACTIVATE WRITEPAGE_ACTIVATE
147 #endif
148
149 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN) && \
150     !defined(HAVE_LINUX_GRAB_CACHE_PAGE_WRITE_BEGIN_WITHFLAGS) && \
151     !defined(HAVE_LINUX_GRAB_CACHE_PAGE_WRITE_BEGIN_NOFLAGS)
152 static inline struct page *
153 grab_cache_page_write_begin(struct address_space *mapping, pgoff_t index,
154                             unsigned int flags) {
155     return __grab_cache_page(mapping, index);
156 }
157 #endif
158
159 #if defined(HAVE_KMEM_CACHE_T)
160 #define afs_kmem_cache_t kmem_cache_t
161 #else
162 #define afs_kmem_cache_t struct kmem_cache
163 #endif
164
165 extern void init_once(void *);
166 #if defined(HAVE_KMEM_CACHE_T)
167 static inline void
168 init_once_func(void * foo, kmem_cache_t * cachep, unsigned long flags) {
169 #if defined(SLAB_CTOR_VERIFY)
170     if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
171         SLAB_CTOR_CONSTRUCTOR)
172 #endif
173     init_once(foo);
174 }
175 #elif defined(KMEM_CACHE_INIT)
176 static inline void
177 init_once_func(struct kmem_cache * cachep, void * foo) {
178     init_once(foo);
179 }
180 #elif !defined(KMEM_CACHE_CTOR_TAKES_VOID)
181 static inline void
182 init_once_func(void * foo, struct kmem_cache * cachep, unsigned long flags) {
183 #if defined(SLAB_CTOR_VERIFY)
184     if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
185         SLAB_CTOR_CONSTRUCTOR)
186 #endif
187     init_once(foo);
188 }
189 #else
190 static inline void
191 init_once_func(void * foo) {
192     init_once(foo);
193 }
194 #endif
195
196 #ifndef SLAB_RECLAIM_ACCOUNT
197 #define SLAB_RECLAIM_ACCOUNT 0
198 #endif
199
200 #if defined(SLAB_KERNEL)
201 #define KALLOC_TYPE SLAB_KERNEL
202 #else
203 #define KALLOC_TYPE GFP_KERNEL
204 #endif
205
206 #ifdef LINUX_KEYRING_SUPPORT
207 static inline struct key *
208 afs_linux_key_alloc(struct key_type *type, const char *desc, afs_kuid_t uid,
209                     afs_kgid_t gid, key_perm_t perm, unsigned long flags)
210 {
211 # if defined(KEY_ALLOC_BYPASS_RESTRICTION)
212     return key_alloc(type, desc, uid, gid, current_cred(), perm, flags, NULL);
213 # elif defined(KEY_ALLOC_NEEDS_STRUCT_TASK)
214     return key_alloc(type, desc, uid, gid, current, perm, flags);
215 # elif defined(KEY_ALLOC_NEEDS_CRED)
216     return key_alloc(type, desc, uid, gid, current_cred(), perm, flags);
217 # else
218     return key_alloc(type, desc, uid, gid, perm, flags);
219 # endif
220 }
221
222 # if defined(STRUCT_TASK_STRUCT_HAS_CRED)
223 static inline struct key *
224 afs_session_keyring(afs_ucred_t *cred)
225 {
226 #  if defined(STRUCT_CRED_HAS_SESSION_KEYRING)
227     return cred->session_keyring;
228 #  else
229     return cred->tgcred->session_keyring;
230 #  endif
231 }
232
233 static inline struct key*
234 afs_linux_search_keyring(afs_ucred_t *cred, struct key_type *type)
235 {
236     key_ref_t key_ref;
237
238     if (afs_session_keyring(cred)) {
239 #  if defined(KEYRING_SEARCH_TAKES_RECURSE)
240         key_ref = keyring_search(
241                       make_key_ref(afs_session_keyring(cred), 1),
242                       type, "_pag", 1);
243 #  else
244         key_ref = keyring_search(
245                       make_key_ref(afs_session_keyring(cred), 1),
246                       type, "_pag");
247 #  endif
248         if (IS_ERR(key_ref))
249             return ERR_CAST(key_ref);
250
251         return key_ref_to_ptr(key_ref);
252     }
253
254     return ERR_PTR(-ENOKEY);
255 }
256 # else
257 static inline struct key*
258 afs_linux_search_keyring(afs_ucred_t *cred, struct key_type *type)
259 {
260     return request_key(type, "_pag", NULL);
261 }
262 # endif /* STRUCT_TASK_STRUCT_HAS_CRED */
263
264 static_inline struct key *
265 afs_set_session_keyring(struct key *keyring)
266 {
267     struct key *old;
268 #if defined(STRUCT_CRED_HAS_SESSION_KEYRING)
269     struct cred *new_creds;
270     old = current_session_keyring();
271     new_creds = prepare_creds();
272     rcu_assign_pointer(new_creds->session_keyring, keyring);
273     commit_creds(new_creds);
274 #else
275     spin_lock_irq(&current->sighand->siglock);
276     old = task_session_keyring(current);
277     smp_wmb();
278     task_session_keyring(current) = keyring;
279     spin_unlock_irq(&current->sighand->siglock);
280 #endif
281     return old;
282 }
283 #endif /* LINUX_KEYRING_SUPPORT */
284
285 #ifdef STRUCT_TASK_STRUCT_HAS_CRED
286 static inline int
287 afs_linux_cred_is_current(afs_ucred_t *cred)
288 {
289     return (cred == current_cred());
290 }
291 #else
292 static inline int
293 afs_linux_cred_is_current(afs_ucred_t *cred)
294 {
295     return 1;
296 }
297 #endif
298
299 #ifndef HAVE_LINUX_PAGE_OFFSET
300 static inline loff_t
301 page_offset(struct page *pp)
302 {
303     return (((loff_t) pp->index) << PAGE_SHIFT);
304 }
305 #endif
306
307 #ifndef HAVE_LINUX_ZERO_USER_SEGMENTS
308 static inline void
309 zero_user_segments(struct page *pp, unsigned int from1, unsigned int to1,
310                    unsigned int from2, unsigned int to2)
311 {
312     void *base = kmap_atomic(pp, KM_USER0);
313
314     if (to1 > from1)
315         memset(base + from1, 0, to1 - from1);
316
317     if (to2 > from2)
318         memset(base + from2, 0, to2 - from2);
319
320     flush_dcache_page(pp);
321     kunmap_atomic(base, KM_USER0);
322 }
323
324 static inline void
325 zero_user_segment(struct page *pp, unsigned int from1, unsigned int to1)
326 {
327     zero_user_segments(pp, from1, to1, 0, 0);
328 }
329 #endif
330
331 #if defined(HAVE_LINUX_IP_SOCK_SET)
332 # include <net/ip.h>
333 /* ip_sock_set_* introduced in linux 5.8 */
334 static inline void
335 afs_linux_sock_set_mtu_discover(struct socket *sockp, int pmtu)
336 {
337     ip_sock_set_mtu_discover(sockp->sk, pmtu);
338 }
339 static inline void
340 afs_linux_sock_set_recverr(struct socket *sockp)
341 {
342     ip_sock_set_recverr(sockp->sk);
343 }
344 #else
345 # if !defined(HAVE_LINUX_KERNEL_SETSOCKOPT)
346 /* Available from 2.6.19 */
347 static inline int
348 kernel_setsockopt(struct socket *sockp, int level, int name, char *val,
349                   unsigned int len) {
350     mm_segment_t old_fs = get_fs();
351     int ret;
352
353     set_fs(get_ds());
354     ret = sockp->ops->setsockopt(sockp, level, name, val, len);
355     set_fs(old_fs);
356
357     return ret;
358 }
359 # endif /* !HAVE_LINUX_KERNEL_SETSOCKOPT */
360
361 static inline void
362 afs_linux_sock_set_mtu_discover(struct socket *sockp, int pmtu)
363 {
364     kernel_setsockopt(sockp, SOL_IP, IP_MTU_DISCOVER, (char *)&pmtu,
365                       sizeof(pmtu));
366 }
367 static inline void
368 afs_linux_sock_set_recverr(struct socket *sockp)
369 {
370     int recverr = 1;
371     kernel_setsockopt(sockp, SOL_IP, IP_RECVERR, (char *)&recverr,
372                       sizeof(recverr));
373 }
374 #endif /* !HAVE_LINUX_IP_SOCK_SET */
375
376 #ifdef HAVE_TRY_TO_FREEZE
377 static inline int
378 afs_try_to_freeze(void) {
379 # ifdef LINUX_REFRIGERATOR_TAKES_PF_FREEZE
380     return try_to_freeze(PF_FREEZE);
381 # else
382     return try_to_freeze();
383 # endif
384 }
385 #else
386 static inline int
387 afs_try_to_freeze(void) {
388 # ifdef CONFIG_PM
389     if (current->flags & PF_FREEZE) {
390         refrigerator(PF_FREEZE);
391         return 1;
392     }
393 # endif
394     return 0;
395 }
396 #endif
397
398 /* The commit which changed refrigerator so that it takes no arguments
399  * also added freezing(), so if LINUX_REFRIGERATOR_TAKES_PF_FREEZE is
400  * true, the kernel doesn't have a freezing() function.
401  */
402 #ifdef LINUX_REFRIGERATOR_TAKES_PF_FREEZE
403 static inline int
404 freezing(struct task_struct *p)
405 {
406 # ifdef CONFIG_PM
407     return p->flags & PF_FREEZE;
408 # else
409     return 0;
410 # endif
411 }
412 #endif
413
414 #if !defined(HAVE_LINUX_PAGECHECKED)
415 # if defined(HAVE_LINUX_PAGEFSMISC)
416 #  include <linux/page-flags.h>
417
418 #  define PageChecked(p)            PageFsMisc((p))
419 #  define SetPageChecked(p)         SetPageFsMisc((p))
420 #  define ClearPageChecked(p)       ClearPageFsMisc((p))
421
422 # endif
423 #endif
424
425 #if !defined(NEW_EXPORT_OPS)
426 extern struct export_operations export_op_default;
427 #endif
428
429 static inline struct dentry *
430 afs_get_dentry_from_fh(struct super_block *afs_cacheSBp, afs_dcache_id_t *ainode,
431                 int cache_fh_len, int cache_fh_type,
432                 int (*afs_fh_acceptable)(void *, struct dentry *)) {
433 #if defined(NEW_EXPORT_OPS)
434     return afs_cacheSBp->s_export_op->fh_to_dentry(afs_cacheSBp, &ainode->ufs.fh,
435                 cache_fh_len, cache_fh_type);
436 #else
437     if (afs_cacheSBp->s_export_op && afs_cacheSBp->s_export_op->decode_fh)
438         return afs_cacheSBp->s_export_op->decode_fh(afs_cacheSBp, ainode->ufs.raw,
439                         cache_fh_len, cache_fh_type, afs_fh_acceptable, NULL);
440     else
441         return export_op_default.decode_fh(afs_cacheSBp, ainode->ufs.raw,
442                         cache_fh_len, cache_fh_type, afs_fh_acceptable, NULL);
443 #endif
444 }
445
446 static inline int
447 afs_get_fh_from_dentry(struct dentry *dp, afs_ufs_dcache_id_t *ainode, int *max_lenp) {
448     if (dp->d_sb->s_export_op->encode_fh)
449 #if defined(EXPORT_OP_ENCODE_FH_TAKES_INODES)
450         return dp->d_sb->s_export_op->encode_fh(dp->d_inode, &ainode->raw[0], max_lenp, NULL);
451 #else
452         return dp->d_sb->s_export_op->encode_fh(dp, &ainode->raw[0], max_lenp, 0);
453 #endif
454 #if defined(NEW_EXPORT_OPS)
455     /* If fs doesn't provide an encode_fh method, assume the default INO32 type */
456     *max_lenp = sizeof(struct fid)/4;
457     ainode->fh.i32.ino = dp->d_inode->i_ino;
458     ainode->fh.i32.gen = dp->d_inode->i_generation;
459     return FILEID_INO32_GEN;
460 #else
461     /* or call the default encoding function for the old API */
462     return export_op_default.encode_fh(dp, &ainode->raw[0], max_lenp, 0);
463 #endif
464 }
465
466 static inline void
467 afs_init_sb_export_ops(struct super_block *sb) {
468 #if !defined(NEW_EXPORT_OPS)
469     /*
470      * decode_fh will call this function.  If not defined for this FS, make
471      * sure it points to the default
472      */
473     if (!sb->s_export_op->find_exported_dentry) {
474         /* Some kernels (at least 2.6.9) do not prototype find_exported_dentry,
475          * even though it is exported, so prototype it ourselves. Newer
476          * kernels do prototype it, but as long as our protoype matches the
477          * real one (the signature never changed before NEW_EXPORT_OPS came
478          * into play), there should be no problems. */
479         extern struct dentry * find_exported_dentry(struct super_block *sb, void *obj, void *parent,
480                                                     int (*acceptable)(void *context, struct dentry *de),
481                                                     void *context);
482         sb->s_export_op->find_exported_dentry = find_exported_dentry;
483     }
484 #endif
485 }
486
487 static inline void
488 afs_linux_lock_inode(struct inode *ip) {
489 #if defined(HAVE_LINUX_INODE_LOCK)
490     inode_lock(ip);
491 #elif defined(STRUCT_INODE_HAS_I_MUTEX)
492     mutex_lock(&ip->i_mutex);
493 #else
494     down(&ip->i_sem);
495 #endif
496 }
497
498 static inline void
499 afs_linux_unlock_inode(struct inode *ip) {
500 #if defined(HAVE_LINUX_INODE_LOCK)
501     inode_unlock(ip);
502 #elif defined(STRUCT_INODE_HAS_I_MUTEX)
503     mutex_unlock(&ip->i_mutex);
504 #else
505     up(&ip->i_sem);
506 #endif
507 }
508
509 /* Use these to lock or unlock an inode for processing
510  * its dentry aliases en masse.
511  */
512 #if defined(HAVE_DCACHE_LOCK)
513 #define afs_d_alias_lock(ip)        spin_lock(&dcache_lock)
514 #define afs_d_alias_unlock(ip)      spin_unlock(&dcache_lock)
515 #else
516 #define afs_d_alias_lock(ip)        spin_lock(&(ip)->i_lock)
517 #define afs_d_alias_unlock(ip)      spin_unlock(&(ip)->i_lock)
518 #endif
519
520
521 /* Use this instead of dget for dentry operations
522  * that occur under a higher lock (e.g. alias processing).
523  * Requires that the higher lock (e.g. dcache_lock or
524  * inode->i_lock) is already held.
525  */
526 static inline void
527 afs_linux_dget(struct dentry *dp) {
528 #if defined(HAVE_DCACHE_LOCK)
529     dget_locked(dp);
530 #else
531     dget(dp);
532 #endif
533 }
534
535
536 static inline int
537 afs_inode_setattr(struct osi_file *afile, struct iattr *newattrs) {
538
539     int code = 0;
540     struct inode *inode = OSIFILE_INODE(afile);
541 #if defined(IOP_TAKES_MNT_IDMAP)
542     code = inode->i_op->setattr(afs_mnt_idmap, afile->filp->f_dentry, newattrs);
543 #elif defined(IOP_TAKES_USER_NAMESPACE)
544     code = inode->i_op->setattr(afs_ns, afile->filp->f_dentry, newattrs);
545 #elif !defined(HAVE_LINUX_INODE_SETATTR)
546     code = inode->i_op->setattr(afile->filp->f_dentry, newattrs);
547 #elif defined(INODE_SETATTR_NOT_VOID)
548     if (inode->i_op && inode->i_op->setattr)
549         code = inode->i_op->setattr(afile->filp->f_dentry, newattrs);
550     else
551         code = inode_setattr(inode, newattrs);
552 #else
553     inode_setattr(inode, newattrs);
554 #endif
555     return code;
556 }
557
558 #if defined(HAVE_LINUX_PATH_LOOKUP)
559 static inline int
560 afs_kern_path(char *aname, int flags, struct nameidata *nd) {
561     return path_lookup(aname, flags, nd);
562 }
563 #else
564 static inline int
565 afs_kern_path(char *aname, int flags, afs_linux_path_t *path) {
566     return kern_path(aname, flags, path);
567 }
568 #endif
569
570 static inline void
571 #if defined(HAVE_LINUX_PATH_LOOKUP)
572 afs_get_dentry_ref(struct nameidata *nd, struct vfsmount **mnt, struct dentry **dpp) {
573 #else
574 afs_get_dentry_ref(afs_linux_path_t *path, struct vfsmount **mnt, struct dentry **dpp) {
575 #endif
576 #if defined(HAVE_LINUX_PATH_LOOKUP)
577 # if defined(STRUCT_NAMEIDATA_HAS_PATH)
578     *dpp = dget(nd->path.dentry);
579     if (mnt)
580         *mnt = mntget(nd->path.mnt);
581     path_put(&nd->path);
582 # else
583     *dpp = dget(nd->dentry);
584     if (mnt)
585         *mnt = mntget(nd->mnt);
586     path_release(nd);
587 # endif
588 #else
589     *dpp = dget(path->dentry);
590     if (mnt)
591         *mnt = mntget(path->mnt);
592     path_put(path);
593 #endif
594 }
595
596 /* wait_event_freezable appeared with 2.6.24 */
597
598 /* These implement the original AFS wait behaviour, with respect to the
599  * refrigerator, rather than the behaviour of the current wait_event_freezable
600  * implementation.
601  */
602
603 #ifndef wait_event_freezable
604 # define wait_event_freezable(waitqueue, condition)                             \
605 ({                                                                              \
606     int _ret;                                                                   \
607     do {                                                                        \
608         _ret = wait_event_interruptible(waitqueue,                              \
609                                         (condition) || freezing(current));      \
610         if (_ret && !freezing(current))                                 \
611             break;                                                              \
612         else if (!(condition))                                                  \
613             _ret = -EINTR;                                                      \
614     } while (afs_try_to_freeze());                                              \
615     _ret;                                                                       \
616 })
617
618 # define wait_event_freezable_timeout(waitqueue, condition, timeout)            \
619 ({                                                                              \
620      int _ret;                                                                  \
621      do {                                                                       \
622         _ret = wait_event_interruptible_timeout(waitqueue,                      \
623                                                 (condition ||                   \
624                                                  freezing(current)),            \
625                                                 timeout);                       \
626      } while (afs_try_to_freeze());                                             \
627      _ret;                                                                      \
628 })
629 #endif
630
631 #if defined(STRUCT_TASK_STRUCT_HAS_CRED)
632 static inline struct file *
633 afs_dentry_open(struct dentry *dp, struct vfsmount *mnt, int flags, const struct cred *creds) {
634 #if defined(DENTRY_OPEN_TAKES_PATH)
635     afs_linux_path_t path;
636     struct file *filp;
637     path.mnt = mnt;
638     path.dentry = dp;
639     /* note that dentry_open will path_get for us */
640     filp = dentry_open(&path, flags, creds);
641     return filp;
642 #else
643     return dentry_open(dget(dp), mntget(mnt), flags, creds);
644 #endif
645 }
646 #endif
647
648 static inline int
649 afs_truncate(struct inode *inode, int len)
650 {
651     int code;
652 #if defined(STRUCT_INODE_OPERATIONS_HAS_TRUNCATE)
653     code = vmtruncate(inode, len);
654 #else
655     code = inode_newsize_ok(inode, len);
656     if (!code)
657         truncate_setsize(inode, len);
658 #endif
659     return code;
660 }
661
662 static inline struct proc_dir_entry *
663 #if defined(HAVE_LINUX_STRUCT_PROC_OPS)
664 afs_proc_create(char *name, umode_t mode, struct proc_dir_entry *parent, struct proc_ops *ops) {
665 #else
666 afs_proc_create(char *name, umode_t mode, struct proc_dir_entry *parent, struct file_operations *ops) {
667 #endif
668 #if defined(HAVE_LINUX_PROC_CREATE)
669     return proc_create(name, mode, parent, ops);
670 #else
671     struct proc_dir_entry *entry;
672     entry = create_proc_entry(name, mode, parent);
673     if (entry)
674         entry->proc_fops = ops;
675     return entry;
676 #endif
677 }
678
679 static inline int
680 afs_dentry_count(struct dentry *dp)
681 {
682 #if defined(HAVE_LINUX_D_COUNT)
683     return d_count(dp);
684 #elif defined(D_COUNT_INT)
685     return dp->d_count;
686 #else
687     return atomic_read(&dp->d_count);
688 #endif
689 }
690
691 static inline void
692 afs_maybe_shrink_dcache(struct dentry *dp)
693 {
694 #if defined(HAVE_LINUX_D_COUNT) || defined(D_COUNT_INT)
695     spin_lock(&dp->d_lock);
696     if (afs_dentry_count(dp) > 1) {
697         spin_unlock(&dp->d_lock);
698         shrink_dcache_parent(dp);
699     } else
700         spin_unlock(&dp->d_lock);
701 #else
702     if (afs_dentry_count(dp) > 1)
703         shrink_dcache_parent(dp);
704 #endif
705 }
706
707 static inline int
708 afs_d_invalidate(struct dentry *dp)
709 {
710 #if defined(D_INVALIDATE_IS_VOID)
711     d_invalidate(dp);
712     return 0;
713 #else
714     return d_invalidate(dp);
715 #endif
716 }
717
718 #if defined(HAVE_LINUX___VFS_WRITE)
719 # define AFS_FILE_NEEDS_SET_FS 1
720 #elif defined(HAVE_LINUX_KERNEL_WRITE)
721 /* #undef AFS_FILE_NEEDS_SET_FS */
722 #else
723 # define AFS_FILE_NEEDS_SET_FS 1
724 #endif
725
726 static inline int
727 afs_file_read(struct file *filp, char __user *buf, size_t len, loff_t *pos)
728 {
729 #if defined(HAVE_LINUX___VFS_WRITE)
730     return __vfs_read(filp, buf, len, pos);
731 #elif defined(HAVE_LINUX_KERNEL_WRITE)
732 # if defined(KERNEL_READ_OFFSET_IS_LAST)
733     return kernel_read(filp, buf, len, pos);
734 # else
735     return kernel_read(filp, *pos, buf, len);
736 # endif
737 #else
738     return filp->f_op->read(filp, buf, len, pos);
739 #endif
740 }
741
742 static inline int
743 afs_file_write(struct file *filp, char __user *buf, size_t len, loff_t *pos)
744 {
745 #if defined(HAVE_LINUX___VFS_WRITE)
746     return __vfs_write(filp, buf, len, pos);
747 #elif defined(HAVE_LINUX_KERNEL_WRITE)
748 # if defined(KERNEL_READ_OFFSET_IS_LAST)
749     return kernel_write(filp, buf, len, pos);
750 # else
751     return kernel_write(filp, buf, len, *pos);
752 # endif
753 #else
754     return filp->f_op->write(filp, buf, len, pos);
755 #endif
756 }
757
758 static inline char*
759 afs_d_path(struct dentry *dp, struct vfsmount *mnt, char *buf, int buflen)
760 {
761 #ifdef D_PATH_TAKES_STRUCT_PATH
762     afs_linux_path_t p = { .mnt = mnt, .dentry = dp };
763     return d_path(&p, buf, buflen);
764 #else
765     return d_path(dp, mnt, buf, buflen);
766 #endif
767 }
768
769 static inline int
770 afs_setattr_prepare(struct dentry *dp, struct iattr *newattrs)
771 {
772 #if defined(IOP_TAKES_MNT_IDMAP)
773     return setattr_prepare(afs_mnt_idmap, dp, newattrs);
774 #elif defined(IOP_TAKES_USER_NAMESPACE)
775     return setattr_prepare(afs_ns, dp, newattrs);
776 #elif defined(HAVE_LINUX_SETATTR_PREPARE)
777     return setattr_prepare(dp, newattrs);
778 #else
779     return inode_change_ok(dp->d_inode, newattrs);
780 #endif
781 }
782
783 /*
784  * afs_d_alias_foreach - Iterate over dentry aliases of an inode. Use like this:
785  *
786  * afs_d_alias_lock(inode);
787  * afs_d_alias_foreach(dentry, inode, node) {
788  *     spin_lock(&dentry->d_lock);
789  *     dentry->d_foo = bar;
790  *     spin_unlock(&dentry->d_lock);
791  * }
792  * afs_d_alias_unlock(inode);
793  *
794  * 'node' is a struct hlist_node, and is only used when D_ALIAS_IS_HLIST &&
795  * !HLIST_ITERATOR_NO_NODE.
796  *
797  * afs_d_alias_foreach_reverse is the same, but traverses the list in the
798  * reverse direction when possible (non-D_ALIAS_IS_HLIST). For
799  * D_ALIAS_IS_HLIST, Linux doesn't provide macros for going in reverse (struct
800  * hlist_head doesn't point directly to the end of the list), so just traverse
801  * forwards.
802  *
803  * @pre afs_d_alias_lock(inode) held
804  */
805 #if defined(D_ALIAS_IS_HLIST)
806
807 # if defined(HLIST_ITERATOR_NO_NODE)
808 #  define afs_d_alias_foreach(dentry, inode, node) \
809           hlist_for_each_entry((dentry), &((inode)->i_dentry), d_alias)
810 # else
811 #  define afs_d_alias_foreach(dentry, inode, node) \
812           hlist_for_each_entry((dentry), (node), &((inode)->i_dentry), d_alias)
813 # endif /* HLIST_ITERATOR_NO_NODE */
814 # define afs_d_alias_foreach_reverse afs_d_alias_foreach
815
816 #else /* D_ALIAS_IS_HLIST */
817
818 # define afs_d_alias_foreach(dentry, inode, node) \
819          list_for_each_entry((dentry), &((inode)->i_dentry), d_alias)
820 # define afs_d_alias_foreach_reverse(dentry, inode, node) \
821          list_for_each_entry_reverse((dentry), &((inode)->i_dentry), d_alias)
822
823 #endif /* D_ALIAS_IS_HLIST */
824
825 #endif /* AFS_LINUX_OSI_COMPAT_H */