1 /* Kernel compatibility routines
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
9 #ifndef AFS_LINUX_OSI_COMPAT_H
10 #define AFS_LINUX_OSI_COMPAT_H
12 #if defined(HAVE_LINUX_FREEZER_H)
13 # include <linux/freezer.h>
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>
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
30 #if defined(STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT) && !defined(DCACHE_NEED_AUTOMOUNT)
31 # define DCACHE_NEED_AUTOMOUNT DMANAGED_AUTOMOUNT
34 #ifdef HAVE_LINUX_STRUCT_VFS_PATH
35 typedef struct vfs_path afs_linux_path_t;
37 typedef struct path afs_linux_path_t;
40 #if defined(STRUCT_DENTRY_HAS_D_U_D_ALIAS)
41 # define d_alias d_u.d_alias
44 #if defined(STRUCT_FILE_HAS_F_PATH)
45 # if !defined(f_dentry)
46 # define f_dentry f_path.dentry
50 #if defined(HAVE_LINUX_LOCKS_LOCK_FILE_WAIT)
51 # define flock_lock_file_wait locks_lock_file_wait
54 #if !defined(HAVE_LINUX_DO_SYNC_READ) && !defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER)
56 do_sync_read(struct file *fp, char *buf, size_t count, loff_t *offp) {
57 return generic_file_read(fp, buf, count, offp);
61 do_sync_write(struct file *fp, char *buf, size_t count, loff_t *offp) {
62 return generic_file_write(fp, buf, count, offp);
65 #endif /* DO_SYNC_READ */
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);
72 flp->fl_flags &=~ FL_SLEEP;
73 return posix_lock_file(fp, flp);
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;
85 #elif defined(POSIX_TEST_LOCK_RETURNS_CONFLICT)
86 struct file_lock *conflict;
87 conflict = posix_test_lock(fp, flp);
89 locks_copy_lock(flp, conflict);
90 flp->fl_type = F_UNLCK;
93 posix_test_lock(fp, flp);
97 #ifdef DCACHE_NFSFS_RENAMED
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);
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);
113 afs_linux_nfsfs_renamed(struct dentry *dp) {
114 return dp->d_flags & DCACHE_NFSFS_RENAMED;
118 static inline void afs_linux_clear_nfsfs_renamed(void) { return; }
119 static inline void afs_linux_set_nfsfs_renamed(void) { return; }
122 #ifndef HAVE_LINUX_HLIST_UNHASHED
124 hlist_unhashed(const struct hlist_node *h) {
125 return (!h->pprev == NULL);
129 #if defined(WRITEPAGE_ACTIVATE)
130 #define AOP_WRITEPAGE_ACTIVATE WRITEPAGE_ACTIVATE
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);
141 #if defined(HAVE_KMEM_CACHE_T)
142 #define afs_kmem_cache_t kmem_cache_t
144 #define afs_kmem_cache_t struct kmem_cache
147 extern void init_once(void *);
148 #if defined(HAVE_KMEM_CACHE_T)
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)
157 #elif defined(KMEM_CACHE_INIT)
159 init_once_func(struct kmem_cache * cachep, void * foo) {
162 #elif !defined(KMEM_CACHE_CTOR_TAKES_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)
173 init_once_func(void * foo) {
178 #ifndef SLAB_RECLAIM_ACCOUNT
179 #define SLAB_RECLAIM_ACCOUNT 0
182 #if defined(SLAB_KERNEL)
183 #define KALLOC_TYPE SLAB_KERNEL
185 #define KALLOC_TYPE GFP_KERNEL
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)
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);
200 return key_alloc(type, desc, uid, gid, perm, flags);
204 # if defined(STRUCT_TASK_STRUCT_HAS_CRED)
205 static inline struct key *
206 afs_session_keyring(afs_ucred_t *cred)
208 # if defined(STRUCT_CRED_HAS_SESSION_KEYRING)
209 return cred->session_keyring;
211 return cred->tgcred->session_keyring;
215 static inline struct key*
216 afs_linux_search_keyring(afs_ucred_t *cred, struct key_type *type)
220 if (afs_session_keyring(cred)) {
221 key_ref = keyring_search(
222 make_key_ref(afs_session_keyring(cred), 1),
225 return ERR_CAST(key_ref);
227 return key_ref_to_ptr(key_ref);
230 return ERR_PTR(-ENOKEY);
233 static inline struct key*
234 afs_linux_search_keyring(afs_ucred_t *cred, struct key_type *type)
236 return request_key(type, "_pag", NULL);
238 # endif /* STRUCT_TASK_STRUCT_HAS_CRED */
240 static_inline struct key *
241 afs_set_session_keyring(struct key *keyring)
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);
251 spin_lock_irq(¤t->sighand->siglock);
252 old = task_session_keyring(current);
254 task_session_keyring(current) = keyring;
255 spin_unlock_irq(¤t->sighand->siglock);
259 #endif /* LINUX_KEYRING_SUPPORT */
261 #ifdef STRUCT_TASK_STRUCT_HAS_CRED
263 afs_linux_cred_is_current(afs_ucred_t *cred)
265 return (cred == current_cred());
269 afs_linux_cred_is_current(afs_ucred_t *cred)
275 #ifndef HAVE_LINUX_PAGE_OFFSET
277 page_offset(struct page *pp)
279 return (((loff_t) pp->index) << PAGE_SHIFT);
283 #ifndef HAVE_LINUX_ZERO_USER_SEGMENTS
285 zero_user_segments(struct page *pp, unsigned int from1, unsigned int to1,
286 unsigned int from2, unsigned int to2)
288 void *base = kmap_atomic(pp, KM_USER0);
291 memset(base + from1, 0, to1 - from1);
294 memset(base + from2, 0, to2 - from2);
296 flush_dcache_page(pp);
297 kunmap_atomic(base, KM_USER0);
301 zero_user_segment(struct page *pp, unsigned int from1, unsigned int to1)
303 zero_user_segments(pp, from1, to1, 0, 0);
307 #ifndef HAVE_LINUX_KERNEL_SETSOCKOPT
308 /* Available from 2.6.19 */
311 kernel_setsockopt(struct socket *sockp, int level, int name, char *val,
313 mm_segment_t old_fs = get_fs();
317 ret = sockp->ops->setsockopt(sockp, level, name, val, len);
324 kernel_getsockopt(struct socket *sockp, int level, int name, char *val,
326 mm_segment_t old_fs = get_fs();
330 ret = sockp->ops->getsockopt(sockp, level, name, val, len);
337 #ifdef HAVE_TRY_TO_FREEZE
339 afs_try_to_freeze(void) {
340 # ifdef LINUX_REFRIGERATOR_TAKES_PF_FREEZE
341 return try_to_freeze(PF_FREEZE);
343 return try_to_freeze();
348 afs_try_to_freeze(void) {
350 if (current->flags & PF_FREEZE) {
351 refrigerator(PF_FREEZE);
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.
363 #ifdef LINUX_REFRIGERATOR_TAKES_PF_FREEZE
365 freezing(struct task_struct *p)
368 return p->flags & PF_FREEZE;
375 #if !defined(HAVE_LINUX_PAGECHECKED)
376 # if defined(HAVE_LINUX_PAGEFSMISC)
377 # include <linux/page-flags.h>
379 # define PageChecked(p) PageFsMisc((p))
380 # define SetPageChecked(p) SetPageFsMisc((p))
381 # define ClearPageChecked(p) ClearPageFsMisc((p))
386 #if !defined(NEW_EXPORT_OPS)
387 extern struct export_operations export_op_default;
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);
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);
402 return export_op_default.decode_fh(afs_cacheSBp, ainode->ufs.raw,
403 cache_fh_len, cache_fh_type, afs_fh_acceptable, NULL);
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);
413 return dp->d_sb->s_export_op->encode_fh(dp, &ainode->raw[0], max_lenp, 0);
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;
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);
428 afs_init_sb_export_ops(struct super_block *sb) {
429 #if !defined(NEW_EXPORT_OPS)
431 * decode_fh will call this function. If not defined for this FS, make
432 * sure it points to the default
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),
443 sb->s_export_op->find_exported_dentry = find_exported_dentry;
449 afs_linux_lock_inode(struct inode *ip) {
450 #if defined(HAVE_LINUX_INODE_LOCK)
452 #elif defined(STRUCT_INODE_HAS_I_MUTEX)
453 mutex_lock(&ip->i_mutex);
460 afs_linux_unlock_inode(struct inode *ip) {
461 #if defined(HAVE_LINUX_INODE_LOCK)
463 #elif defined(STRUCT_INODE_HAS_I_MUTEX)
464 mutex_unlock(&ip->i_mutex);
471 afs_inode_setattr(struct osi_file *afile, struct iattr *newattrs) {
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);
481 code = inode_setattr(inode, newattrs);
483 inode_setattr(inode, newattrs);
488 #if defined(HAVE_LINUX_PATH_LOOKUP)
490 afs_kern_path(char *aname, int flags, struct nameidata *nd) {
491 return path_lookup(aname, flags, nd);
495 afs_kern_path(char *aname, int flags, afs_linux_path_t *path) {
496 return kern_path(aname, flags, path);
501 #if defined(HAVE_LINUX_PATH_LOOKUP)
502 afs_get_dentry_ref(struct nameidata *nd, struct vfsmount **mnt, struct dentry **dpp) {
504 afs_get_dentry_ref(afs_linux_path_t *path, struct vfsmount **mnt, struct dentry **dpp) {
506 #if defined(HAVE_LINUX_PATH_LOOKUP)
507 # if defined(STRUCT_NAMEIDATA_HAS_PATH)
508 *dpp = dget(nd->path.dentry);
510 *mnt = mntget(nd->path.mnt);
513 *dpp = dget(nd->dentry);
515 *mnt = mntget(nd->mnt);
519 *dpp = dget(path->dentry);
521 *mnt = mntget(path->mnt);
526 /* wait_event_freezable appeared with 2.6.24 */
528 /* These implement the original AFS wait behaviour, with respect to the
529 * refrigerator, rather than the behaviour of the current wait_event_freezable
533 #ifndef wait_event_freezable
534 # define wait_event_freezable(waitqueue, condition) \
538 _ret = wait_event_interruptible(waitqueue, \
539 (condition) || freezing(current)); \
540 if (_ret && !freezing(current)) \
542 else if (!(condition)) \
544 } while (afs_try_to_freeze()); \
548 # define wait_event_freezable_timeout(waitqueue, condition, timeout) \
552 _ret = wait_event_interruptible_timeout(waitqueue, \
554 freezing(current)), \
556 } while (afs_try_to_freeze()); \
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;
569 /* note that dentry_open will path_get for us */
570 filp = dentry_open(&path, flags, creds);
573 return dentry_open(dget(dp), mntget(mnt), flags, creds);
579 afs_truncate(struct inode *inode, int len)
582 #if defined(STRUCT_INODE_OPERATIONS_HAS_TRUNCATE)
583 code = vmtruncate(inode, len);
585 code = inode_newsize_ok(inode, len);
587 truncate_setsize(inode, len);
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);
597 struct proc_dir_entry *entry;
598 entry = create_proc_entry(name, mode, parent);
600 entry->proc_fops = fops;
606 afs_dentry_count(struct dentry *dp)
608 #if defined(HAVE_LINUX_D_COUNT)
610 #elif defined(D_COUNT_INT)
613 return atomic_read(&dp->d_count);
618 afs_maybe_shrink_dcache(struct dentry *dp)
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);
626 spin_unlock(&dp->d_lock);
628 if (afs_dentry_count(dp) > 1)
629 shrink_dcache_parent(dp);
634 afs_d_invalidate(struct dentry *dp)
636 #if defined(D_INVALIDATE_IS_VOID)
640 return d_invalidate(dp);
645 afs_file_read(struct file *filp, char __user *buf, size_t len, loff_t *pos)
647 #if defined(HAVE_LINUX___VFS_READ)
648 return __vfs_read(filp, buf, len, pos);
650 return filp->f_op->read(filp, buf, len, pos);
655 afs_file_write(struct file *filp, char __user *buf, size_t len, loff_t *pos)
657 #if defined(HAVE_LINUX___VFS_READ)
658 return __vfs_write(filp, buf, len, pos);
660 return filp->f_op->write(filp, buf, len, pos);
664 #endif /* AFS_LINUX_OSI_COMPAT_H */