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 #ifndef HAVE_LINUX_DO_SYNC_READ
46 do_sync_read(struct file *fp, char *buf, size_t count, loff_t *offp) {
47 return generic_file_read(fp, buf, count, offp);
51 do_sync_write(struct file *fp, char *buf, size_t count, loff_t *offp) {
52 return generic_file_write(fp, buf, count, offp);
55 #endif /* DO_SYNC_READ */
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);
62 flp->fl_flags &=~ FL_SLEEP;
63 return posix_lock_file(fp, flp);
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;
75 #elif defined(POSIX_TEST_LOCK_RETURNS_CONFLICT)
76 struct file_lock *conflict;
77 conflict = posix_test_lock(fp, flp);
79 locks_copy_lock(flp, conflict);
80 flp->fl_type = F_UNLCK;
83 posix_test_lock(fp, flp);
87 #ifdef DCACHE_NFSFS_RENAMED
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);
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);
103 afs_linux_nfsfs_renamed(struct dentry *dp) {
104 return dp->d_flags & DCACHE_NFSFS_RENAMED;
108 static inline void afs_linux_clear_nfsfs_renamed(void) { return; }
109 static inline void afs_linux_set_nfsfs_renamed(void) { return; }
112 #ifndef HAVE_LINUX_HLIST_UNHASHED
114 hlist_unhashed(const struct hlist_node *h) {
115 return (!h->pprev == NULL);
119 #if defined(WRITEPAGE_ACTIVATE)
120 #define AOP_WRITEPAGE_ACTIVATE WRITEPAGE_ACTIVATE
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);
131 #if defined(HAVE_KMEM_CACHE_T)
132 #define afs_kmem_cache_t kmem_cache_t
134 #define afs_kmem_cache_t struct kmem_cache
137 extern void init_once(void *);
138 #if defined(HAVE_KMEM_CACHE_T)
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)
147 #elif defined(KMEM_CACHE_INIT)
149 init_once_func(struct kmem_cache * cachep, void * foo) {
152 #elif !defined(KMEM_CACHE_CTOR_TAKES_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)
163 init_once_func(void * foo) {
168 #ifndef SLAB_RECLAIM_ACCOUNT
169 #define SLAB_RECLAIM_ACCOUNT 0
172 #if defined(SLAB_KERNEL)
173 #define KALLOC_TYPE SLAB_KERNEL
175 #define KALLOC_TYPE GFP_KERNEL
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)
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);
188 return key_alloc(type, desc, uid, gid, perm, flags);
192 # if defined(STRUCT_TASK_STRUCT_HAS_CRED)
193 static inline struct key *
194 afs_session_keyring(afs_ucred_t *cred)
196 # if defined(STRUCT_CRED_HAS_SESSION_KEYRING)
197 return cred->session_keyring;
199 return cred->tgcred->session_keyring;
203 static inline struct key*
204 afs_linux_search_keyring(afs_ucred_t *cred, struct key_type *type)
208 if (afs_session_keyring(cred)) {
209 key_ref = keyring_search(
210 make_key_ref(afs_session_keyring(cred), 1),
213 return ERR_CAST(key_ref);
215 return key_ref_to_ptr(key_ref);
218 return ERR_PTR(-ENOKEY);
221 static inline struct key*
222 afs_linux_search_keyring(afs_ucred_t *cred, struct key_type *type)
224 return request_key(type, "_pag", NULL);
226 # endif /* STRUCT_TASK_STRUCT_HAS_CRED */
228 static_inline struct key *
229 afs_set_session_keyring(struct key *keyring)
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);
239 spin_lock_irq(¤t->sighand->siglock);
240 old = task_session_keyring(current);
242 task_session_keyring(current) = keyring;
243 spin_unlock_irq(¤t->sighand->siglock);
247 #endif /* LINUX_KEYRING_SUPPORT */
249 #ifdef STRUCT_TASK_STRUCT_HAS_CRED
251 afs_linux_cred_is_current(afs_ucred_t *cred)
253 return (cred == current_cred());
257 afs_linux_cred_is_current(afs_ucred_t *cred)
263 #ifndef HAVE_LINUX_PAGE_OFFSET
265 page_offset(struct page *pp)
267 return (((loff_t) pp->index) << PAGE_CACHE_SHIFT);
271 #ifndef HAVE_LINUX_ZERO_USER_SEGMENTS
273 zero_user_segments(struct page *pp, unsigned int from1, unsigned int to1,
274 unsigned int from2, unsigned int to2)
276 void *base = kmap_atomic(pp, KM_USER0);
279 memset(base + from1, 0, to1 - from1);
282 memset(base + from2, 0, to2 - from2);
284 flush_dcache_page(pp);
285 kunmap_atomic(base, KM_USER0);
289 zero_user_segment(struct page *pp, unsigned int from1, unsigned int to1)
291 zero_user_segments(pp, from1, to1, 0, 0);
295 #ifndef HAVE_LINUX_KERNEL_SETSOCKOPT
296 /* Available from 2.6.19 */
299 kernel_setsockopt(struct socket *sockp, int level, int name, char *val,
301 mm_segment_t old_fs = get_fs();
305 ret = sockp->ops->setsockopt(sockp, level, name, val, len);
312 kernel_getsockopt(struct socket *sockp, int level, int name, char *val,
314 mm_segment_t old_fs = get_fs();
318 ret = sockp->ops->getsockopt(sockp, level, name, val, len);
325 #ifdef HAVE_TRY_TO_FREEZE
327 afs_try_to_freeze(void) {
328 # ifdef LINUX_REFRIGERATOR_TAKES_PF_FREEZE
329 return try_to_freeze(PF_FREEZE);
331 return try_to_freeze();
336 afs_try_to_freeze(void) {
338 if (current->flags & PF_FREEZE) {
339 refrigerator(PF_FREEZE);
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.
351 #ifdef LINUX_REFRIGERATOR_TAKES_PF_FREEZE
353 freezing(struct task_struct *p)
356 return p->flags & PF_FREEZE;
363 #if !defined(HAVE_LINUX_PAGECHECKED)
364 # if defined(HAVE_LINUX_PAGEFSMISC)
365 # include <linux/page-flags.h>
367 # define PageChecked(p) PageFsMisc((p))
368 # define SetPageChecked(p) SetPageFsMisc((p))
369 # define ClearPageChecked(p) ClearPageFsMisc((p))
374 #if !defined(NEW_EXPORT_OPS)
375 extern struct export_operations export_op_default;
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);
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);
390 return export_op_default.decode_fh(afs_cacheSBp, ainode->ufs.raw,
391 cache_fh_len, cache_fh_type, afs_fh_acceptable, NULL);
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);
401 return dp->d_sb->s_export_op->encode_fh(dp, &ainode->raw[0], max_lenp, 0);
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;
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);
416 afs_init_sb_export_ops(struct super_block *sb) {
417 #if !defined(NEW_EXPORT_OPS)
419 * decode_fh will call this function. If not defined for this FS, make
420 * sure it points to the default
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),
431 sb->s_export_op->find_exported_dentry = find_exported_dentry;
437 afs_linux_lock_inode(struct inode *ip) {
438 #ifdef STRUCT_INODE_HAS_I_MUTEX
439 mutex_lock(&ip->i_mutex);
446 afs_linux_unlock_inode(struct inode *ip) {
447 #ifdef STRUCT_INODE_HAS_I_MUTEX
448 mutex_unlock(&ip->i_mutex);
455 afs_inode_setattr(struct osi_file *afile, struct iattr *newattrs) {
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);
465 code = inode_setattr(inode, newattrs);
467 inode_setattr(inode, newattrs);
472 #if defined(HAVE_LINUX_PATH_LOOKUP)
474 afs_kern_path(char *aname, int flags, struct nameidata *nd) {
475 return path_lookup(aname, flags, nd);
479 afs_kern_path(char *aname, int flags, afs_linux_path_t *path) {
480 return kern_path(aname, flags, path);
485 #if defined(HAVE_LINUX_PATH_LOOKUP)
486 afs_get_dentry_ref(struct nameidata *nd, struct vfsmount **mnt, struct dentry **dpp) {
488 afs_get_dentry_ref(afs_linux_path_t *path, struct vfsmount **mnt, struct dentry **dpp) {
490 #if defined(STRUCT_NAMEIDATA_HAS_PATH)
491 # if defined(HAVE_LINUX_PATH_LOOKUP)
492 *dpp = dget(nd->path.dentry);
494 *mnt = mntget(nd->path.mnt);
497 *dpp = dget(path->dentry);
499 *mnt = mntget(path->mnt);
503 *dpp = dget(nd->dentry);
505 *mnt = mntget(nd->mnt);
510 /* wait_event_freezable appeared with 2.6.24 */
512 /* These implement the original AFS wait behaviour, with respect to the
513 * refrigerator, rather than the behaviour of the current wait_event_freezable
517 #ifndef wait_event_freezable
518 # define wait_event_freezable(waitqueue, condition) \
522 _ret = wait_event_interruptible(waitqueue, \
523 (condition) || freezing(current)); \
524 if (_ret && !freezing(current)) \
526 else if (!(condition)) \
528 } while (afs_try_to_freeze()); \
532 # define wait_event_freezable_timeout(waitqueue, condition, timeout) \
536 _ret = wait_event_interruptible_timeout(waitqueue, \
538 freezing(current)), \
540 } while (afs_try_to_freeze()); \
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;
553 /* note that dentry_open will path_get for us */
554 filp = dentry_open(&path, flags, creds);
557 return dentry_open(dget(dp), mntget(mnt), flags, creds);
563 afs_truncate(struct inode *inode, int len)
566 #if defined(STRUCT_INODE_OPERATIONS_HAS_TRUNCATE)
567 code = vmtruncate(inode, len);
569 code = inode_newsize_ok(inode, len);
571 truncate_setsize(inode, len);
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);
581 struct proc_dir_entry *entry;
582 entry = create_proc_entry(name, mode, parent);
584 entry->proc_fops = fops;
590 afs_dentry_count(struct dentry *dp)
592 #if defined(HAVE_LINUX_D_COUNT)
594 #elif defined(D_COUNT_INT)
597 return atomic_read(&dp->d_count);
602 afs_maybe_shrink_dcache(struct dentry *dp)
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);
610 spin_unlock(&dp->d_lock);
612 if (afs_dentry_count(dp) > 1)
613 shrink_dcache_parent(dp);
618 afs_d_invalidate(struct dentry *dp)
620 #if defined(D_INVALIDATE_IS_VOID)
624 return d_invalidate(dp);
628 #endif /* AFS_LINUX_OSI_COMPAT_H */