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 #ifndef HAVE_LINUX_FILE_DENTRY
51 #define file_dentry(file) ((file)->f_dentry)
54 #if defined(HAVE_LINUX_LOCKS_LOCK_FILE_WAIT)
55 # define flock_lock_file_wait locks_lock_file_wait
58 #if !defined(HAVE_LINUX_DO_SYNC_READ) && !defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER)
60 do_sync_read(struct file *fp, char *buf, size_t count, loff_t *offp) {
61 return generic_file_read(fp, buf, count, offp);
65 do_sync_write(struct file *fp, char *buf, size_t count, loff_t *offp) {
66 return generic_file_write(fp, buf, count, offp);
69 #endif /* DO_SYNC_READ */
72 afs_posix_lock_file(struct file *fp, struct file_lock *flp) {
73 #ifdef POSIX_LOCK_FILE_WAIT_ARG
74 return posix_lock_file(fp, flp, NULL);
76 flp->fl_flags &=~ FL_SLEEP;
77 return posix_lock_file(fp, flp);
82 afs_posix_test_lock(struct file *fp, struct file_lock *flp) {
83 #if defined(POSIX_TEST_LOCK_CONFLICT_ARG)
84 struct file_lock conflict;
85 if (posix_test_lock(fp, flp, &conflict)) {
86 locks_copy_lock(flp, &conflict);
87 flp->fl_type = F_UNLCK;
89 #elif defined(POSIX_TEST_LOCK_RETURNS_CONFLICT)
90 struct file_lock *conflict;
91 conflict = posix_test_lock(fp, flp);
93 locks_copy_lock(flp, conflict);
94 flp->fl_type = F_UNLCK;
97 posix_test_lock(fp, flp);
101 #ifdef DCACHE_NFSFS_RENAMED
103 afs_linux_clear_nfsfs_renamed(struct dentry *dp) {
104 spin_lock(&dp->d_lock);
105 dp->d_flags &= ~DCACHE_NFSFS_RENAMED;
106 spin_unlock(&dp->d_lock);
110 afs_linux_set_nfsfs_renamed(struct dentry *dp) {
111 spin_lock(&dp->d_lock);
112 dp->d_flags |= DCACHE_NFSFS_RENAMED;
113 spin_unlock(&dp->d_lock);
117 afs_linux_nfsfs_renamed(struct dentry *dp) {
118 return dp->d_flags & DCACHE_NFSFS_RENAMED;
122 static inline void afs_linux_clear_nfsfs_renamed(void) { return; }
123 static inline void afs_linux_set_nfsfs_renamed(void) { return; }
126 #ifndef HAVE_LINUX_HLIST_UNHASHED
128 hlist_unhashed(const struct hlist_node *h) {
129 return (!h->pprev == NULL);
133 #if defined(WRITEPAGE_ACTIVATE)
134 #define AOP_WRITEPAGE_ACTIVATE WRITEPAGE_ACTIVATE
137 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN) && !defined(HAVE_LINUX_GRAB_CACHE_PAGE_WRITE_BEGIN)
138 static inline struct page *
139 grab_cache_page_write_begin(struct address_space *mapping, pgoff_t index,
140 unsigned int flags) {
141 return __grab_cache_page(mapping, index);
145 #if defined(HAVE_KMEM_CACHE_T)
146 #define afs_kmem_cache_t kmem_cache_t
148 #define afs_kmem_cache_t struct kmem_cache
151 extern void init_once(void *);
152 #if defined(HAVE_KMEM_CACHE_T)
154 init_once_func(void * foo, kmem_cache_t * cachep, unsigned long flags) {
155 #if defined(SLAB_CTOR_VERIFY)
156 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
157 SLAB_CTOR_CONSTRUCTOR)
161 #elif defined(KMEM_CACHE_INIT)
163 init_once_func(struct kmem_cache * cachep, void * foo) {
166 #elif !defined(KMEM_CACHE_CTOR_TAKES_VOID)
168 init_once_func(void * foo, struct kmem_cache * cachep, unsigned long flags) {
169 #if defined(SLAB_CTOR_VERIFY)
170 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
171 SLAB_CTOR_CONSTRUCTOR)
177 init_once_func(void * foo) {
182 #ifndef SLAB_RECLAIM_ACCOUNT
183 #define SLAB_RECLAIM_ACCOUNT 0
186 #if defined(SLAB_KERNEL)
187 #define KALLOC_TYPE SLAB_KERNEL
189 #define KALLOC_TYPE GFP_KERNEL
192 #ifdef LINUX_KEYRING_SUPPORT
193 static inline struct key *
194 afs_linux_key_alloc(struct key_type *type, const char *desc, afs_kuid_t uid,
195 afs_kgid_t gid, key_perm_t perm, unsigned long flags)
197 # if defined(KEY_ALLOC_BYPASS_RESTRICTION)
198 return key_alloc(type, desc, uid, gid, current_cred(), perm, flags, NULL);
199 # elif defined(KEY_ALLOC_NEEDS_STRUCT_TASK)
200 return key_alloc(type, desc, uid, gid, current, perm, flags);
201 # elif defined(KEY_ALLOC_NEEDS_CRED)
202 return key_alloc(type, desc, uid, gid, current_cred(), perm, flags);
204 return key_alloc(type, desc, uid, gid, perm, flags);
208 # if defined(STRUCT_TASK_STRUCT_HAS_CRED)
209 static inline struct key *
210 afs_session_keyring(afs_ucred_t *cred)
212 # if defined(STRUCT_CRED_HAS_SESSION_KEYRING)
213 return cred->session_keyring;
215 return cred->tgcred->session_keyring;
219 static inline struct key*
220 afs_linux_search_keyring(afs_ucred_t *cred, struct key_type *type)
224 if (afs_session_keyring(cred)) {
225 key_ref = keyring_search(
226 make_key_ref(afs_session_keyring(cred), 1),
229 return ERR_CAST(key_ref);
231 return key_ref_to_ptr(key_ref);
234 return ERR_PTR(-ENOKEY);
237 static inline struct key*
238 afs_linux_search_keyring(afs_ucred_t *cred, struct key_type *type)
240 return request_key(type, "_pag", NULL);
242 # endif /* STRUCT_TASK_STRUCT_HAS_CRED */
244 static_inline struct key *
245 afs_set_session_keyring(struct key *keyring)
248 #if defined(STRUCT_CRED_HAS_SESSION_KEYRING)
249 struct cred *new_creds;
250 old = current_session_keyring();
251 new_creds = prepare_creds();
252 rcu_assign_pointer(new_creds->session_keyring, keyring);
253 commit_creds(new_creds);
255 spin_lock_irq(¤t->sighand->siglock);
256 old = task_session_keyring(current);
258 task_session_keyring(current) = keyring;
259 spin_unlock_irq(¤t->sighand->siglock);
263 #endif /* LINUX_KEYRING_SUPPORT */
265 #ifdef STRUCT_TASK_STRUCT_HAS_CRED
267 afs_linux_cred_is_current(afs_ucred_t *cred)
269 return (cred == current_cred());
273 afs_linux_cred_is_current(afs_ucred_t *cred)
279 #ifndef HAVE_LINUX_PAGE_OFFSET
281 page_offset(struct page *pp)
283 return (((loff_t) pp->index) << PAGE_SHIFT);
287 #ifndef HAVE_LINUX_ZERO_USER_SEGMENTS
289 zero_user_segments(struct page *pp, unsigned int from1, unsigned int to1,
290 unsigned int from2, unsigned int to2)
292 void *base = kmap_atomic(pp, KM_USER0);
295 memset(base + from1, 0, to1 - from1);
298 memset(base + from2, 0, to2 - from2);
300 flush_dcache_page(pp);
301 kunmap_atomic(base, KM_USER0);
305 zero_user_segment(struct page *pp, unsigned int from1, unsigned int to1)
307 zero_user_segments(pp, from1, to1, 0, 0);
311 #ifndef HAVE_LINUX_KERNEL_SETSOCKOPT
312 /* Available from 2.6.19 */
315 kernel_setsockopt(struct socket *sockp, int level, int name, char *val,
317 mm_segment_t old_fs = get_fs();
321 ret = sockp->ops->setsockopt(sockp, level, name, val, len);
328 kernel_getsockopt(struct socket *sockp, int level, int name, char *val,
330 mm_segment_t old_fs = get_fs();
334 ret = sockp->ops->getsockopt(sockp, level, name, val, len);
341 #ifdef HAVE_TRY_TO_FREEZE
343 afs_try_to_freeze(void) {
344 # ifdef LINUX_REFRIGERATOR_TAKES_PF_FREEZE
345 return try_to_freeze(PF_FREEZE);
347 return try_to_freeze();
352 afs_try_to_freeze(void) {
354 if (current->flags & PF_FREEZE) {
355 refrigerator(PF_FREEZE);
363 /* The commit which changed refrigerator so that it takes no arguments
364 * also added freezing(), so if LINUX_REFRIGERATOR_TAKES_PF_FREEZE is
365 * true, the kernel doesn't have a freezing() function.
367 #ifdef LINUX_REFRIGERATOR_TAKES_PF_FREEZE
369 freezing(struct task_struct *p)
372 return p->flags & PF_FREEZE;
379 #if !defined(HAVE_LINUX_PAGECHECKED)
380 # if defined(HAVE_LINUX_PAGEFSMISC)
381 # include <linux/page-flags.h>
383 # define PageChecked(p) PageFsMisc((p))
384 # define SetPageChecked(p) SetPageFsMisc((p))
385 # define ClearPageChecked(p) ClearPageFsMisc((p))
390 #if !defined(NEW_EXPORT_OPS)
391 extern struct export_operations export_op_default;
394 static inline struct dentry *
395 afs_get_dentry_from_fh(struct super_block *afs_cacheSBp, afs_dcache_id_t *ainode,
396 int cache_fh_len, int cache_fh_type,
397 int (*afs_fh_acceptable)(void *, struct dentry *)) {
398 #if defined(NEW_EXPORT_OPS)
399 return afs_cacheSBp->s_export_op->fh_to_dentry(afs_cacheSBp, &ainode->ufs.fh,
400 cache_fh_len, cache_fh_type);
402 if (afs_cacheSBp->s_export_op && afs_cacheSBp->s_export_op->decode_fh)
403 return afs_cacheSBp->s_export_op->decode_fh(afs_cacheSBp, ainode->ufs.raw,
404 cache_fh_len, cache_fh_type, afs_fh_acceptable, NULL);
406 return export_op_default.decode_fh(afs_cacheSBp, ainode->ufs.raw,
407 cache_fh_len, cache_fh_type, afs_fh_acceptable, NULL);
412 afs_get_fh_from_dentry(struct dentry *dp, afs_ufs_dcache_id_t *ainode, int *max_lenp) {
413 if (dp->d_sb->s_export_op->encode_fh)
414 #if defined(EXPORT_OP_ENCODE_FH_TAKES_INODES)
415 return dp->d_sb->s_export_op->encode_fh(dp->d_inode, &ainode->raw[0], max_lenp, NULL);
417 return dp->d_sb->s_export_op->encode_fh(dp, &ainode->raw[0], max_lenp, 0);
419 #if defined(NEW_EXPORT_OPS)
420 /* If fs doesn't provide an encode_fh method, assume the default INO32 type */
421 *max_lenp = sizeof(struct fid)/4;
422 ainode->fh.i32.ino = dp->d_inode->i_ino;
423 ainode->fh.i32.gen = dp->d_inode->i_generation;
424 return FILEID_INO32_GEN;
426 /* or call the default encoding function for the old API */
427 return export_op_default.encode_fh(dp, &ainode->raw[0], max_lenp, 0);
432 afs_init_sb_export_ops(struct super_block *sb) {
433 #if !defined(NEW_EXPORT_OPS)
435 * decode_fh will call this function. If not defined for this FS, make
436 * sure it points to the default
438 if (!sb->s_export_op->find_exported_dentry) {
439 /* Some kernels (at least 2.6.9) do not prototype find_exported_dentry,
440 * even though it is exported, so prototype it ourselves. Newer
441 * kernels do prototype it, but as long as our protoype matches the
442 * real one (the signature never changed before NEW_EXPORT_OPS came
443 * into play), there should be no problems. */
444 extern struct dentry * find_exported_dentry(struct super_block *sb, void *obj, void *parent,
445 int (*acceptable)(void *context, struct dentry *de),
447 sb->s_export_op->find_exported_dentry = find_exported_dentry;
453 afs_linux_lock_inode(struct inode *ip) {
454 #if defined(HAVE_LINUX_INODE_LOCK)
456 #elif defined(STRUCT_INODE_HAS_I_MUTEX)
457 mutex_lock(&ip->i_mutex);
464 afs_linux_unlock_inode(struct inode *ip) {
465 #if defined(HAVE_LINUX_INODE_LOCK)
467 #elif defined(STRUCT_INODE_HAS_I_MUTEX)
468 mutex_unlock(&ip->i_mutex);
474 /* Use this instead of dget for dentry operations
475 * that occur under a higher lock (e.g. alias processing).
476 * Requires that the higher lock (e.g. dcache_lock or
477 * inode->i_lock) is already held.
480 afs_linux_dget(struct dentry *dp) {
481 #if defined(HAVE_DCACHE_LOCK)
490 afs_inode_setattr(struct osi_file *afile, struct iattr *newattrs) {
493 struct inode *inode = OSIFILE_INODE(afile);
494 #if !defined(HAVE_LINUX_INODE_SETATTR)
495 code = inode->i_op->setattr(afile->filp->f_dentry, newattrs);
496 #elif defined(INODE_SETATTR_NOT_VOID)
497 if (inode->i_op && inode->i_op->setattr)
498 code = inode->i_op->setattr(afile->filp->f_dentry, newattrs);
500 code = inode_setattr(inode, newattrs);
502 inode_setattr(inode, newattrs);
507 #if defined(HAVE_LINUX_PATH_LOOKUP)
509 afs_kern_path(char *aname, int flags, struct nameidata *nd) {
510 return path_lookup(aname, flags, nd);
514 afs_kern_path(char *aname, int flags, afs_linux_path_t *path) {
515 return kern_path(aname, flags, path);
520 #if defined(HAVE_LINUX_PATH_LOOKUP)
521 afs_get_dentry_ref(struct nameidata *nd, struct vfsmount **mnt, struct dentry **dpp) {
523 afs_get_dentry_ref(afs_linux_path_t *path, struct vfsmount **mnt, struct dentry **dpp) {
525 #if defined(HAVE_LINUX_PATH_LOOKUP)
526 # if defined(STRUCT_NAMEIDATA_HAS_PATH)
527 *dpp = dget(nd->path.dentry);
529 *mnt = mntget(nd->path.mnt);
532 *dpp = dget(nd->dentry);
534 *mnt = mntget(nd->mnt);
538 *dpp = dget(path->dentry);
540 *mnt = mntget(path->mnt);
545 /* wait_event_freezable appeared with 2.6.24 */
547 /* These implement the original AFS wait behaviour, with respect to the
548 * refrigerator, rather than the behaviour of the current wait_event_freezable
552 #ifndef wait_event_freezable
553 # define wait_event_freezable(waitqueue, condition) \
557 _ret = wait_event_interruptible(waitqueue, \
558 (condition) || freezing(current)); \
559 if (_ret && !freezing(current)) \
561 else if (!(condition)) \
563 } while (afs_try_to_freeze()); \
567 # define wait_event_freezable_timeout(waitqueue, condition, timeout) \
571 _ret = wait_event_interruptible_timeout(waitqueue, \
573 freezing(current)), \
575 } while (afs_try_to_freeze()); \
580 #if defined(STRUCT_TASK_STRUCT_HAS_CRED)
581 static inline struct file *
582 afs_dentry_open(struct dentry *dp, struct vfsmount *mnt, int flags, const struct cred *creds) {
583 #if defined(DENTRY_OPEN_TAKES_PATH)
584 afs_linux_path_t path;
588 /* note that dentry_open will path_get for us */
589 filp = dentry_open(&path, flags, creds);
592 return dentry_open(dget(dp), mntget(mnt), flags, creds);
598 afs_truncate(struct inode *inode, int len)
601 #if defined(STRUCT_INODE_OPERATIONS_HAS_TRUNCATE)
602 code = vmtruncate(inode, len);
604 code = inode_newsize_ok(inode, len);
606 truncate_setsize(inode, len);
611 static inline struct proc_dir_entry *
612 afs_proc_create(char *name, umode_t mode, struct proc_dir_entry *parent, struct file_operations *fops) {
613 #if defined(HAVE_LINUX_PROC_CREATE)
614 return proc_create(name, mode, parent, fops);
616 struct proc_dir_entry *entry;
617 entry = create_proc_entry(name, mode, parent);
619 entry->proc_fops = fops;
625 afs_dentry_count(struct dentry *dp)
627 #if defined(HAVE_LINUX_D_COUNT)
629 #elif defined(D_COUNT_INT)
632 return atomic_read(&dp->d_count);
637 afs_maybe_shrink_dcache(struct dentry *dp)
639 #if defined(HAVE_LINUX_D_COUNT) || defined(D_COUNT_INT)
640 spin_lock(&dp->d_lock);
641 if (afs_dentry_count(dp) > 1) {
642 spin_unlock(&dp->d_lock);
643 shrink_dcache_parent(dp);
645 spin_unlock(&dp->d_lock);
647 if (afs_dentry_count(dp) > 1)
648 shrink_dcache_parent(dp);
653 afs_d_invalidate(struct dentry *dp)
655 #if defined(D_INVALIDATE_IS_VOID)
659 return d_invalidate(dp);
663 #if defined(HAVE_LINUX___VFS_WRITE)
664 # define AFS_FILE_NEEDS_SET_FS 1
665 #elif defined(HAVE_LINUX_KERNEL_WRITE)
666 /* #undef AFS_FILE_NEEDS_SET_FS */
668 # define AFS_FILE_NEEDS_SET_FS 1
672 afs_file_read(struct file *filp, char __user *buf, size_t len, loff_t *pos)
674 #if defined(HAVE_LINUX___VFS_WRITE)
675 return __vfs_read(filp, buf, len, pos);
676 #elif defined(HAVE_LINUX_KERNEL_WRITE)
677 # if defined(KERNEL_READ_OFFSET_IS_LAST)
678 return kernel_read(filp, buf, len, pos);
680 return kernel_read(filp, *pos, buf, len);
683 return filp->f_op->read(filp, buf, len, pos);
688 afs_file_write(struct file *filp, char __user *buf, size_t len, loff_t *pos)
690 #if defined(HAVE_LINUX___VFS_WRITE)
691 return __vfs_write(filp, buf, len, pos);
692 #elif defined(HAVE_LINUX_KERNEL_WRITE)
693 # if defined(KERNEL_READ_OFFSET_IS_LAST)
694 return kernel_write(filp, buf, len, pos);
696 return kernel_write(filp, buf, len, *pos);
699 return filp->f_op->write(filp, buf, len, pos);
703 #endif /* AFS_LINUX_OSI_COMPAT_H */