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