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_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);
198 return key_alloc(type, desc, uid, gid, perm, flags);
202 # if defined(STRUCT_TASK_STRUCT_HAS_CRED)
203 static inline struct key *
204 afs_session_keyring(afs_ucred_t *cred)
206 # if defined(STRUCT_CRED_HAS_SESSION_KEYRING)
207 return cred->session_keyring;
209 return cred->tgcred->session_keyring;
213 static inline struct key*
214 afs_linux_search_keyring(afs_ucred_t *cred, struct key_type *type)
218 if (afs_session_keyring(cred)) {
219 key_ref = keyring_search(
220 make_key_ref(afs_session_keyring(cred), 1),
223 return ERR_CAST(key_ref);
225 return key_ref_to_ptr(key_ref);
228 return ERR_PTR(-ENOKEY);
231 static inline struct key*
232 afs_linux_search_keyring(afs_ucred_t *cred, struct key_type *type)
234 return request_key(type, "_pag", NULL);
236 # endif /* STRUCT_TASK_STRUCT_HAS_CRED */
238 static_inline struct key *
239 afs_set_session_keyring(struct key *keyring)
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);
249 spin_lock_irq(¤t->sighand->siglock);
250 old = task_session_keyring(current);
252 task_session_keyring(current) = keyring;
253 spin_unlock_irq(¤t->sighand->siglock);
257 #endif /* LINUX_KEYRING_SUPPORT */
259 #ifdef STRUCT_TASK_STRUCT_HAS_CRED
261 afs_linux_cred_is_current(afs_ucred_t *cred)
263 return (cred == current_cred());
267 afs_linux_cred_is_current(afs_ucred_t *cred)
273 #ifndef HAVE_LINUX_PAGE_OFFSET
275 page_offset(struct page *pp)
277 return (((loff_t) pp->index) << PAGE_SHIFT);
281 #ifndef HAVE_LINUX_ZERO_USER_SEGMENTS
283 zero_user_segments(struct page *pp, unsigned int from1, unsigned int to1,
284 unsigned int from2, unsigned int to2)
286 void *base = kmap_atomic(pp, KM_USER0);
289 memset(base + from1, 0, to1 - from1);
292 memset(base + from2, 0, to2 - from2);
294 flush_dcache_page(pp);
295 kunmap_atomic(base, KM_USER0);
299 zero_user_segment(struct page *pp, unsigned int from1, unsigned int to1)
301 zero_user_segments(pp, from1, to1, 0, 0);
305 #ifndef HAVE_LINUX_KERNEL_SETSOCKOPT
306 /* Available from 2.6.19 */
309 kernel_setsockopt(struct socket *sockp, int level, int name, char *val,
311 mm_segment_t old_fs = get_fs();
315 ret = sockp->ops->setsockopt(sockp, level, name, val, len);
322 kernel_getsockopt(struct socket *sockp, int level, int name, char *val,
324 mm_segment_t old_fs = get_fs();
328 ret = sockp->ops->getsockopt(sockp, level, name, val, len);
335 #ifdef HAVE_TRY_TO_FREEZE
337 afs_try_to_freeze(void) {
338 # ifdef LINUX_REFRIGERATOR_TAKES_PF_FREEZE
339 return try_to_freeze(PF_FREEZE);
341 return try_to_freeze();
346 afs_try_to_freeze(void) {
348 if (current->flags & PF_FREEZE) {
349 refrigerator(PF_FREEZE);
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.
361 #ifdef LINUX_REFRIGERATOR_TAKES_PF_FREEZE
363 freezing(struct task_struct *p)
366 return p->flags & PF_FREEZE;
373 #if !defined(HAVE_LINUX_PAGECHECKED)
374 # if defined(HAVE_LINUX_PAGEFSMISC)
375 # include <linux/page-flags.h>
377 # define PageChecked(p) PageFsMisc((p))
378 # define SetPageChecked(p) SetPageFsMisc((p))
379 # define ClearPageChecked(p) ClearPageFsMisc((p))
384 #if !defined(NEW_EXPORT_OPS)
385 extern struct export_operations export_op_default;
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);
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);
400 return export_op_default.decode_fh(afs_cacheSBp, ainode->ufs.raw,
401 cache_fh_len, cache_fh_type, afs_fh_acceptable, NULL);
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);
411 return dp->d_sb->s_export_op->encode_fh(dp, &ainode->raw[0], max_lenp, 0);
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;
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);
426 afs_init_sb_export_ops(struct super_block *sb) {
427 #if !defined(NEW_EXPORT_OPS)
429 * decode_fh will call this function. If not defined for this FS, make
430 * sure it points to the default
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),
441 sb->s_export_op->find_exported_dentry = find_exported_dentry;
447 afs_linux_lock_inode(struct inode *ip) {
448 #if defined(HAVE_LINUX_INODE_LOCK)
450 #elif defined(STRUCT_INODE_HAS_I_MUTEX)
451 mutex_lock(&ip->i_mutex);
458 afs_linux_unlock_inode(struct inode *ip) {
459 #if defined(HAVE_LINUX_INODE_LOCK)
461 #elif defined(STRUCT_INODE_HAS_I_MUTEX)
462 mutex_unlock(&ip->i_mutex);
469 afs_inode_setattr(struct osi_file *afile, struct iattr *newattrs) {
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);
479 code = inode_setattr(inode, newattrs);
481 inode_setattr(inode, newattrs);
486 #if defined(HAVE_LINUX_PATH_LOOKUP)
488 afs_kern_path(char *aname, int flags, struct nameidata *nd) {
489 return path_lookup(aname, flags, nd);
493 afs_kern_path(char *aname, int flags, afs_linux_path_t *path) {
494 return kern_path(aname, flags, path);
499 #if defined(HAVE_LINUX_PATH_LOOKUP)
500 afs_get_dentry_ref(struct nameidata *nd, struct vfsmount **mnt, struct dentry **dpp) {
502 afs_get_dentry_ref(afs_linux_path_t *path, struct vfsmount **mnt, struct dentry **dpp) {
504 #if defined(HAVE_LINUX_PATH_LOOKUP)
505 # if defined(STRUCT_NAMEIDATA_HAS_PATH)
506 *dpp = dget(nd->path.dentry);
508 *mnt = mntget(nd->path.mnt);
511 *dpp = dget(nd->dentry);
513 *mnt = mntget(nd->mnt);
517 *dpp = dget(path->dentry);
519 *mnt = mntget(path->mnt);
524 /* wait_event_freezable appeared with 2.6.24 */
526 /* These implement the original AFS wait behaviour, with respect to the
527 * refrigerator, rather than the behaviour of the current wait_event_freezable
531 #ifndef wait_event_freezable
532 # define wait_event_freezable(waitqueue, condition) \
536 _ret = wait_event_interruptible(waitqueue, \
537 (condition) || freezing(current)); \
538 if (_ret && !freezing(current)) \
540 else if (!(condition)) \
542 } while (afs_try_to_freeze()); \
546 # define wait_event_freezable_timeout(waitqueue, condition, timeout) \
550 _ret = wait_event_interruptible_timeout(waitqueue, \
552 freezing(current)), \
554 } while (afs_try_to_freeze()); \
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;
567 /* note that dentry_open will path_get for us */
568 filp = dentry_open(&path, flags, creds);
571 return dentry_open(dget(dp), mntget(mnt), flags, creds);
577 afs_truncate(struct inode *inode, int len)
580 #if defined(STRUCT_INODE_OPERATIONS_HAS_TRUNCATE)
581 code = vmtruncate(inode, len);
583 code = inode_newsize_ok(inode, len);
585 truncate_setsize(inode, len);
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);
595 struct proc_dir_entry *entry;
596 entry = create_proc_entry(name, mode, parent);
598 entry->proc_fops = fops;
604 afs_dentry_count(struct dentry *dp)
606 #if defined(HAVE_LINUX_D_COUNT)
608 #elif defined(D_COUNT_INT)
611 return atomic_read(&dp->d_count);
616 afs_maybe_shrink_dcache(struct dentry *dp)
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);
624 spin_unlock(&dp->d_lock);
626 if (afs_dentry_count(dp) > 1)
627 shrink_dcache_parent(dp);
632 afs_d_invalidate(struct dentry *dp)
634 #if defined(D_INVALIDATE_IS_VOID)
638 return d_invalidate(dp);
643 afs_file_read(struct file *filp, char __user *buf, size_t len, loff_t *pos)
645 #if defined(HAVE_LINUX___VFS_READ)
646 return __vfs_read(filp, buf, len, pos);
648 return filp->f_op->read(filp, buf, len, pos);
653 afs_file_write(struct file *filp, char __user *buf, size_t len, loff_t *pos)
655 #if defined(HAVE_LINUX___VFS_READ)
656 return __vfs_write(filp, buf, len, pos);
658 return filp->f_op->write(filp, buf, len, pos);
662 #endif /* AFS_LINUX_OSI_COMPAT_H */