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 #ifndef HAVE_LINUX_DO_SYNC_READ
42 do_sync_read(struct file *fp, char *buf, size_t count, loff_t *offp) {
43 return generic_file_read(fp, buf, count, offp);
47 do_sync_write(struct file *fp, char *buf, size_t count, loff_t *offp) {
48 return generic_file_write(fp, buf, count, offp);
51 #endif /* DO_SYNC_READ */
54 afs_posix_lock_file(struct file *fp, struct file_lock *flp) {
55 #ifdef POSIX_LOCK_FILE_WAIT_ARG
56 return posix_lock_file(fp, flp, NULL);
58 flp->fl_flags &=~ FL_SLEEP;
59 return posix_lock_file(fp, flp);
64 afs_posix_test_lock(struct file *fp, struct file_lock *flp) {
65 #if defined(POSIX_TEST_LOCK_CONFLICT_ARG)
66 struct file_lock conflict;
67 if (posix_test_lock(fp, flp, &conflict)) {
68 locks_copy_lock(flp, &conflict);
69 flp->fl_type = F_UNLCK;
71 #elif defined(POSIX_TEST_LOCK_RETURNS_CONFLICT)
72 struct file_lock *conflict;
73 conflict = posix_test_lock(fp, flp);
75 locks_copy_lock(flp, conflict);
76 flp->fl_type = F_UNLCK;
79 posix_test_lock(fp, flp);
83 #ifdef DCACHE_NFSFS_RENAMED
85 afs_linux_clear_nfsfs_renamed(struct dentry *dp) {
86 spin_lock(&dp->d_lock);
87 dp->d_flags &= ~DCACHE_NFSFS_RENAMED;
88 spin_unlock(&dp->d_lock);
92 afs_linux_set_nfsfs_renamed(struct dentry *dp) {
93 spin_lock(&dp->d_lock);
94 dp->d_flags |= DCACHE_NFSFS_RENAMED;
95 spin_unlock(&dp->d_lock);
99 afs_linux_nfsfs_renamed(struct dentry *dp) {
100 return dp->d_flags & DCACHE_NFSFS_RENAMED;
104 static inline void afs_linux_clear_nfsfs_renamed(void) { return; }
105 static inline void afs_linux_set_nfsfs_renamed(void) { return; }
108 #ifndef HAVE_LINUX_HLIST_UNHASHED
110 hlist_unhashed(const struct hlist_node *h) {
111 return (!h->pprev == NULL);
115 #if defined(WRITEPAGE_ACTIVATE)
116 #define AOP_WRITEPAGE_ACTIVATE WRITEPAGE_ACTIVATE
119 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN) && !defined(HAVE_LINUX_GRAB_CACHE_PAGE_WRITE_BEGIN)
120 static inline struct page *
121 grab_cache_page_write_begin(struct address_space *mapping, pgoff_t index,
122 unsigned int flags) {
123 return __grab_cache_page(mapping, index);
127 #if defined(HAVE_KMEM_CACHE_T)
128 #define afs_kmem_cache_t kmem_cache_t
130 #define afs_kmem_cache_t struct kmem_cache
133 extern void init_once(void *);
134 #if defined(HAVE_KMEM_CACHE_T)
136 init_once_func(void * foo, kmem_cache_t * cachep, unsigned long flags) {
137 #if defined(SLAB_CTOR_VERIFY)
138 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
139 SLAB_CTOR_CONSTRUCTOR)
143 #elif defined(KMEM_CACHE_INIT)
145 init_once_func(struct kmem_cache * cachep, void * foo) {
148 #elif !defined(KMEM_CACHE_CTOR_TAKES_VOID)
150 init_once_func(void * foo, struct kmem_cache * cachep, unsigned long flags) {
151 #if defined(SLAB_CTOR_VERIFY)
152 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
153 SLAB_CTOR_CONSTRUCTOR)
159 init_once_func(void * foo) {
164 #ifndef SLAB_RECLAIM_ACCOUNT
165 #define SLAB_RECLAIM_ACCOUNT 0
168 #if defined(SLAB_KERNEL)
169 #define KALLOC_TYPE SLAB_KERNEL
171 #define KALLOC_TYPE GFP_KERNEL
174 #ifdef LINUX_KEYRING_SUPPORT
175 static inline struct key *
176 afs_linux_key_alloc(struct key_type *type, const char *desc, afs_kuid_t uid,
177 afs_kgid_t gid, key_perm_t perm, unsigned long flags)
179 # if defined(KEY_ALLOC_NEEDS_STRUCT_TASK)
180 return key_alloc(type, desc, uid, gid, current, perm, flags);
181 # elif defined(KEY_ALLOC_NEEDS_CRED)
182 return key_alloc(type, desc, uid, gid, current_cred(), perm, flags);
184 return key_alloc(type, desc, uid, gid, perm, flags);
188 # if defined(STRUCT_TASK_STRUCT_HAS_CRED)
189 static inline struct key *
190 afs_session_keyring(afs_ucred_t *cred)
192 # if defined(STRUCT_CRED_HAS_SESSION_KEYRING)
193 return cred->session_keyring;
195 return cred->tgcred->session_keyring;
199 static inline struct key*
200 afs_linux_search_keyring(afs_ucred_t *cred, struct key_type *type)
204 if (afs_session_keyring(cred)) {
205 key_ref = keyring_search(
206 make_key_ref(afs_session_keyring(cred), 1),
209 return ERR_CAST(key_ref);
211 return key_ref_to_ptr(key_ref);
214 return ERR_PTR(-ENOKEY);
217 static inline struct key*
218 afs_linux_search_keyring(afs_ucred_t *cred, struct key_type *type)
220 return request_key(type, "_pag", NULL);
222 # endif /* STRUCT_TASK_STRUCT_HAS_CRED */
224 static_inline struct key *
225 afs_set_session_keyring(struct key *keyring)
228 #if defined(STRUCT_CRED_HAS_SESSION_KEYRING)
229 struct cred *new_creds;
230 old = current_session_keyring();
231 new_creds = prepare_creds();
232 rcu_assign_pointer(new_creds->session_keyring, keyring);
233 commit_creds(new_creds);
235 spin_lock_irq(¤t->sighand->siglock);
236 old = task_session_keyring(current);
238 task_session_keyring(current) = keyring;
239 spin_unlock_irq(¤t->sighand->siglock);
243 #endif /* LINUX_KEYRING_SUPPORT */
245 #ifdef STRUCT_TASK_STRUCT_HAS_CRED
247 afs_linux_cred_is_current(afs_ucred_t *cred)
249 return (cred == current_cred());
253 afs_linux_cred_is_current(afs_ucred_t *cred)
259 #ifndef HAVE_LINUX_PAGE_OFFSET
261 page_offset(struct page *pp)
263 return (((loff_t) pp->index) << PAGE_CACHE_SHIFT);
267 #ifndef HAVE_LINUX_ZERO_USER_SEGMENTS
269 zero_user_segments(struct page *pp, unsigned int from1, unsigned int to1,
270 unsigned int from2, unsigned int to2)
272 void *base = kmap_atomic(pp, KM_USER0);
275 memset(base + from1, 0, to1 - from1);
278 memset(base + from2, 0, to2 - from2);
280 flush_dcache_page(pp);
281 kunmap_atomic(base, KM_USER0);
285 zero_user_segment(struct page *pp, unsigned int from1, unsigned int to1)
287 zero_user_segments(pp, from1, to1, 0, 0);
291 #ifndef HAVE_LINUX_KERNEL_SETSOCKOPT
292 /* Available from 2.6.19 */
295 kernel_setsockopt(struct socket *sockp, int level, int name, char *val,
297 mm_segment_t old_fs = get_fs();
301 ret = sockp->ops->setsockopt(sockp, level, name, val, len);
308 kernel_getsockopt(struct socket *sockp, int level, int name, char *val,
310 mm_segment_t old_fs = get_fs();
314 ret = sockp->ops->getsockopt(sockp, level, name, val, len);
321 #ifdef HAVE_TRY_TO_FREEZE
323 afs_try_to_freeze(void) {
324 # ifdef LINUX_REFRIGERATOR_TAKES_PF_FREEZE
325 return try_to_freeze(PF_FREEZE);
327 return try_to_freeze();
332 afs_try_to_freeze(void) {
334 if (current->flags & PF_FREEZE) {
335 refrigerator(PF_FREEZE);
343 /* The commit which changed refrigerator so that it takes no arguments
344 * also added freezing(), so if LINUX_REFRIGERATOR_TAKES_PF_FREEZE is
345 * true, the kernel doesn't have a freezing() function.
347 #ifdef LINUX_REFRIGERATOR_TAKES_PF_FREEZE
349 freezing(struct task_struct *p)
352 return p->flags & PF_FREEZE;
359 #if !defined(HAVE_LINUX_PAGECHECKED)
360 # if defined(HAVE_LINUX_PAGEFSMISC)
361 # include <linux/page-flags.h>
363 # define PageChecked(p) PageFsMisc((p))
364 # define SetPageChecked(p) SetPageFsMisc((p))
365 # define ClearPageChecked(p) ClearPageFsMisc((p))
370 #if !defined(NEW_EXPORT_OPS)
371 extern struct export_operations export_op_default;
374 static inline struct dentry *
375 afs_get_dentry_from_fh(struct super_block *afs_cacheSBp, afs_dcache_id_t *ainode,
376 int cache_fh_len, int cache_fh_type,
377 int (*afs_fh_acceptable)(void *, struct dentry *)) {
378 #if defined(NEW_EXPORT_OPS)
379 return afs_cacheSBp->s_export_op->fh_to_dentry(afs_cacheSBp, &ainode->ufs.fh,
380 cache_fh_len, cache_fh_type);
382 if (afs_cacheSBp->s_export_op && afs_cacheSBp->s_export_op->decode_fh)
383 return afs_cacheSBp->s_export_op->decode_fh(afs_cacheSBp, ainode->ufs.raw,
384 cache_fh_len, cache_fh_type, afs_fh_acceptable, NULL);
386 return export_op_default.decode_fh(afs_cacheSBp, ainode->ufs.raw,
387 cache_fh_len, cache_fh_type, afs_fh_acceptable, NULL);
392 afs_get_fh_from_dentry(struct dentry *dp, afs_ufs_dcache_id_t *ainode, int *max_lenp) {
393 if (dp->d_sb->s_export_op->encode_fh)
394 #if defined(EXPORT_OP_ENCODE_FH_TAKES_INODES)
395 return dp->d_sb->s_export_op->encode_fh(dp->d_inode, &ainode->raw[0], max_lenp, NULL);
397 return dp->d_sb->s_export_op->encode_fh(dp, &ainode->raw[0], max_lenp, 0);
399 #if defined(NEW_EXPORT_OPS)
400 /* If fs doesn't provide an encode_fh method, assume the default INO32 type */
401 *max_lenp = sizeof(struct fid)/4;
402 ainode->fh.i32.ino = dp->d_inode->i_ino;
403 ainode->fh.i32.gen = dp->d_inode->i_generation;
404 return FILEID_INO32_GEN;
406 /* or call the default encoding function for the old API */
407 return export_op_default.encode_fh(dp, &ainode->raw[0], max_lenp, 0);
412 afs_init_sb_export_ops(struct super_block *sb) {
413 #if !defined(NEW_EXPORT_OPS)
415 * decode_fh will call this function. If not defined for this FS, make
416 * sure it points to the default
418 if (!sb->s_export_op->find_exported_dentry) {
419 /* Some kernels (at least 2.6.9) do not prototype find_exported_dentry,
420 * even though it is exported, so prototype it ourselves. Newer
421 * kernels do prototype it, but as long as our protoype matches the
422 * real one (the signature never changed before NEW_EXPORT_OPS came
423 * into play), there should be no problems. */
424 extern struct dentry * find_exported_dentry(struct super_block *sb, void *obj, void *parent,
425 int (*acceptable)(void *context, struct dentry *de),
427 sb->s_export_op->find_exported_dentry = find_exported_dentry;
433 afs_linux_lock_inode(struct inode *ip) {
434 #ifdef STRUCT_INODE_HAS_I_MUTEX
435 mutex_lock(&ip->i_mutex);
442 afs_linux_unlock_inode(struct inode *ip) {
443 #ifdef STRUCT_INODE_HAS_I_MUTEX
444 mutex_unlock(&ip->i_mutex);
451 afs_inode_setattr(struct osi_file *afile, struct iattr *newattrs) {
454 struct inode *inode = OSIFILE_INODE(afile);
455 #if !defined(HAVE_LINUX_INODE_SETATTR)
456 code = inode->i_op->setattr(afile->filp->f_dentry, newattrs);
457 #elif defined(INODE_SETATTR_NOT_VOID)
458 if (inode->i_op && inode->i_op->setattr)
459 code = inode->i_op->setattr(afile->filp->f_dentry, newattrs);
461 code = inode_setattr(inode, newattrs);
463 inode_setattr(inode, newattrs);
468 #if defined(HAVE_LINUX_PATH_LOOKUP)
470 afs_kern_path(char *aname, int flags, struct nameidata *nd) {
471 return path_lookup(aname, flags, nd);
475 afs_kern_path(char *aname, int flags, afs_linux_path_t *path) {
476 return kern_path(aname, flags, path);
481 #if defined(HAVE_LINUX_PATH_LOOKUP)
482 afs_get_dentry_ref(struct nameidata *nd, struct vfsmount **mnt, struct dentry **dpp) {
484 afs_get_dentry_ref(afs_linux_path_t *path, struct vfsmount **mnt, struct dentry **dpp) {
486 #if defined(STRUCT_NAMEIDATA_HAS_PATH)
487 # if defined(HAVE_LINUX_PATH_LOOKUP)
488 *dpp = dget(nd->path.dentry);
490 *mnt = mntget(nd->path.mnt);
493 *dpp = dget(path->dentry);
495 *mnt = mntget(path->mnt);
499 *dpp = dget(nd->dentry);
501 *mnt = mntget(nd->mnt);
506 /* wait_event_freezable appeared with 2.6.24 */
508 /* These implement the original AFS wait behaviour, with respect to the
509 * refrigerator, rather than the behaviour of the current wait_event_freezable
513 #ifndef wait_event_freezable
514 # define wait_event_freezable(waitqueue, condition) \
518 _ret = wait_event_interruptible(waitqueue, \
519 (condition) || freezing(current)); \
520 if (_ret && !freezing(current)) \
522 else if (!(condition)) \
524 } while (afs_try_to_freeze()); \
528 # define wait_event_freezable_timeout(waitqueue, condition, timeout) \
532 _ret = wait_event_interruptible_timeout(waitqueue, \
534 freezing(current)), \
536 } while (afs_try_to_freeze()); \
541 #if defined(STRUCT_TASK_STRUCT_HAS_CRED)
542 static inline struct file *
543 afs_dentry_open(struct dentry *dp, struct vfsmount *mnt, int flags, const struct cred *creds) {
544 #if defined(DENTRY_OPEN_TAKES_PATH)
545 afs_linux_path_t path;
549 /* note that dentry_open will path_get for us */
550 filp = dentry_open(&path, flags, creds);
553 return dentry_open(dget(dp), mntget(mnt), flags, creds);
559 afs_truncate(struct inode *inode, int len)
562 #if defined(STRUCT_INODE_OPERATIONS_HAS_TRUNCATE)
563 code = vmtruncate(inode, len);
565 code = inode_newsize_ok(inode, len);
567 truncate_setsize(inode, len);
572 static inline struct proc_dir_entry *
573 afs_proc_create(char *name, umode_t mode, struct proc_dir_entry *parent, struct file_operations *fops) {
574 #if defined(HAVE_LINUX_PROC_CREATE)
575 return proc_create(name, mode, parent, fops);
577 struct proc_dir_entry *entry;
578 entry = create_proc_entry(name, mode, parent);
580 entry->proc_fops = fops;
586 afs_dentry_count(struct dentry *dp)
588 #if defined(HAVE_LINUX_D_COUNT)
590 #elif defined(D_COUNT_INT)
593 return atomic_read(&dp->d_count);
598 afs_maybe_shrink_dcache(struct dentry *dp)
600 #if defined(HAVE_LINUX_D_COUNT) || defined(D_COUNT_INT)
601 spin_lock(&dp->d_lock);
602 if (afs_dentry_count(dp) > 1) {
603 spin_unlock(&dp->d_lock);
604 shrink_dcache_parent(dp);
606 spin_unlock(&dp->d_lock);
608 if (afs_dentry_count(dp) > 1)
609 shrink_dcache_parent(dp);
614 afs_d_invalidate(struct dentry *dp)
616 #if defined(D_INVALIDATE_IS_VOID)
620 return d_invalidate(dp);
624 #endif /* AFS_LINUX_OSI_COMPAT_H */