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 #ifndef HAVE_LINUX_DO_SYNC_READ
32 do_sync_read(struct file *fp, char *buf, size_t count, loff_t *offp) {
33 return generic_file_read(fp, buf, count, offp);
37 do_sync_write(struct file *fp, char *buf, size_t count, loff_t *offp) {
38 return generic_file_write(fp, buf, count, offp);
41 #endif /* DO_SYNC_READ */
44 afs_posix_lock_file(struct file *fp, struct file_lock *flp) {
45 #ifdef POSIX_LOCK_FILE_WAIT_ARG
46 return posix_lock_file(fp, flp, NULL);
48 flp->fl_flags &=~ FL_SLEEP;
49 return posix_lock_file(fp, flp);
54 afs_posix_test_lock(struct file *fp, struct file_lock *flp) {
55 #if defined(POSIX_TEST_LOCK_CONFLICT_ARG)
56 struct file_lock conflict;
57 if (posix_test_lock(fp, flp, &conflict)) {
58 locks_copy_lock(flp, &conflict);
59 flp->fl_type = F_UNLCK;
61 #elif defined(POSIX_TEST_LOCK_RETURNS_CONFLICT)
62 struct file_lock *conflict;
63 conflict = posix_test_lock(fp, flp);
65 locks_copy_lock(flp, conflict);
66 flp->fl_type = F_UNLCK;
69 posix_test_lock(fp, flp);
73 #ifdef DCACHE_NFSFS_RENAMED
75 afs_linux_clear_nfsfs_renamed(struct dentry *dp) {
76 spin_lock(&dp->d_lock);
77 dp->d_flags &= ~DCACHE_NFSFS_RENAMED;
78 spin_unlock(&dp->d_lock);
82 afs_linux_set_nfsfs_renamed(struct dentry *dp) {
83 spin_lock(&dp->d_lock);
84 dp->d_flags |= DCACHE_NFSFS_RENAMED;
85 spin_unlock(&dp->d_lock);
89 afs_linux_nfsfs_renamed(struct dentry *dp) {
90 return dp->d_flags & DCACHE_NFSFS_RENAMED;
94 static inline void afs_linux_clear_nfsfs_renamed(void) { return; }
95 static inline void afs_linux_set_nfsfs_renamed(void) { return; }
98 #ifndef HAVE_LINUX_HLIST_UNHASHED
100 hlist_unhashed(const struct hlist_node *h) {
101 return (!h->pprev == NULL);
105 #if defined(WRITEPAGE_ACTIVATE)
106 #define AOP_WRITEPAGE_ACTIVATE WRITEPAGE_ACTIVATE
109 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN) && !defined(HAVE_LINUX_GRAB_CACHE_PAGE_WRITE_BEGIN)
110 static inline struct page *
111 grab_cache_page_write_begin(struct address_space *mapping, pgoff_t index,
112 unsigned int flags) {
113 return __grab_cache_page(mapping, index);
117 #if defined(HAVE_KMEM_CACHE_T)
118 #define afs_kmem_cache_t kmem_cache_t
120 #define afs_kmem_cache_t struct kmem_cache
123 extern void init_once(void *);
124 #if defined(HAVE_KMEM_CACHE_T)
126 init_once_func(void * foo, kmem_cache_t * cachep, unsigned long flags) {
127 #if defined(SLAB_CTOR_VERIFY)
128 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
129 SLAB_CTOR_CONSTRUCTOR)
133 #elif defined(KMEM_CACHE_INIT)
135 init_once_func(struct kmem_cache * cachep, void * foo) {
138 #elif !defined(KMEM_CACHE_CTOR_TAKES_VOID)
140 init_once_func(void * foo, struct kmem_cache * cachep, unsigned long flags) {
141 #if defined(SLAB_CTOR_VERIFY)
142 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
143 SLAB_CTOR_CONSTRUCTOR)
149 init_once_func(void * foo) {
154 #ifndef SLAB_RECLAIM_ACCOUNT
155 #define SLAB_RECLAIM_ACCOUNT 0
158 #if defined(SLAB_KERNEL)
159 #define KALLOC_TYPE SLAB_KERNEL
161 #define KALLOC_TYPE GFP_KERNEL
164 #ifdef LINUX_KEYRING_SUPPORT
165 static inline struct key *
166 afs_linux_key_alloc(struct key_type *type, const char *desc, uid_t uid,
167 gid_t gid, key_perm_t perm, unsigned long flags)
169 # if defined(KEY_ALLOC_NEEDS_STRUCT_TASK)
170 return key_alloc(type, desc, uid, gid, current, perm, flags);
171 # elif defined(KEY_ALLOC_NEEDS_CRED)
172 return key_alloc(type, desc, uid, gid, current_cred(), perm, flags);
174 return key_alloc(type, desc, uid, gid, perm, flags);
178 # if defined(STRUCT_TASK_STRUCT_HAS_CRED)
179 static inline struct key*
180 afs_linux_search_keyring(afs_ucred_t *cred, struct key_type *type)
184 if (cred->tgcred->session_keyring) {
185 key_ref = keyring_search(
186 make_key_ref(cred->tgcred->session_keyring, 1),
189 return ERR_CAST(key_ref);
191 return key_ref_to_ptr(key_ref);
194 return ERR_PTR(-ENOKEY);
197 static inline struct key*
198 afs_linux_search_keyring(afs_ucred_t *cred, struct key_type *type)
200 return request_key(type, "_pag", NULL);
202 # endif /* STRUCT_TASK_STRUCT_HAS_CRED */
203 #endif /* LINUX_KEYRING_SUPPORT */
205 #ifdef STRUCT_TASK_STRUCT_HAS_CRED
207 afs_linux_cred_is_current(afs_ucred_t *cred)
209 return (cred == current_cred());
213 afs_linux_cred_is_current(afs_ucred_t *cred)
219 #ifndef HAVE_LINUX_PAGE_OFFSET
221 page_offset(struct page *pp)
223 return (((loff_t) pp->index) << PAGE_CACHE_SHIFT);
227 #ifndef HAVE_LINUX_ZERO_USER_SEGMENTS
229 zero_user_segments(struct page *pp, unsigned int from1, unsigned int to1,
230 unsigned int from2, unsigned int to2)
232 void *base = kmap_atomic(pp, KM_USER0);
235 memset(base + from1, 0, to1 - from1);
238 memset(base + from2, 0, to2 - from2);
240 flush_dcache_page(pp);
241 kunmap_atomic(base, KM_USER0);
245 zero_user_segment(struct page *pp, unsigned int from1, unsigned int to1)
247 zero_user_segments(pp, from1, to1, 0, 0);
251 #ifndef HAVE_LINUX_KERNEL_SETSOCKOPT
252 /* Available from 2.6.19 */
255 kernel_setsockopt(struct socket *sockp, int level, int name, char *val,
257 mm_segment_t old_fs = get_fs();
261 ret = sockp->ops->setsockopt(sockp, level, name, val, len);
268 kernel_getsockopt(struct socket *sockp, int level, int name, char *val,
270 mm_segment_t old_fs = get_fs();
274 ret = sockp->ops->getsockopt(sockp, level, name, val, len);
281 #ifdef HAVE_TRY_TO_FREEZE
283 afs_try_to_freeze(void) {
284 # ifdef LINUX_REFRIGERATOR_TAKES_PF_FREEZE
285 try_to_freeze(PF_FREEZE);
292 afs_try_to_freeze(void) {
294 if (current->flags & PF_FREEZE) {
295 refrigerator(PF_FREEZE);
301 #if !defined(HAVE_LINUX_PAGECHECKED)
302 # if defined(HAVE_LINUX_PAGEFSMISC)
303 # include <linux/page-flags.h>
305 # define PageChecked(p) PageFsMisc((p))
306 # define SetPageChecked(p) SetPageFsMisc((p))
307 # define ClearPageChecked(p) ClearPageFsMisc((p))
312 #if !defined(NEW_EXPORT_OPS)
313 extern struct export_operations export_op_default;
316 static inline struct dentry *
317 afs_get_dentry_from_fh(struct super_block *afs_cacheSBp, afs_dcache_id_t *ainode,
318 int cache_fh_len, int cache_fh_type,
319 int (*afs_fh_acceptable)(void *, struct dentry *)) {
320 #if defined(NEW_EXPORT_OPS)
321 return afs_cacheSBp->s_export_op->fh_to_dentry(afs_cacheSBp, &ainode->ufs.fh,
322 cache_fh_len, cache_fh_type);
324 if (afs_cacheSBp->s_export_op && afs_cacheSBp->s_export_op->decode_fh)
325 return afs_cacheSBp->s_export_op->decode_fh(afs_cacheSBp, ainode->ufs.raw,
326 cache_fh_len, cache_fh_type, afs_fh_acceptable, NULL);
328 return export_op_default.decode_fh(afs_cacheSBp, ainode->ufs.raw,
329 cache_fh_len, cache_fh_type, afs_fh_acceptable, NULL);
334 afs_get_fh_from_dentry(struct dentry *dp, afs_ufs_dcache_id_t *ainode, int *max_lenp) {
335 if (dp->d_sb->s_export_op->encode_fh)
336 return dp->d_sb->s_export_op->encode_fh(dp, &ainode->raw[0], max_lenp, 0);
337 #if defined(NEW_EXPORT_OPS)
338 /* If fs doesn't provide an encode_fh method, assume the default INO32 type */
339 *max_lenp = sizeof(struct fid)/4;
340 ainode->fh.i32.ino = dp->d_inode->i_ino;
341 ainode->fh.i32.gen = dp->d_inode->i_generation;
342 return FILEID_INO32_GEN;
344 /* or call the default encoding function for the old API */
345 return export_op_default.encode_fh(dp, &ainode->raw[0], max_lenp, 0);
350 afs_init_sb_export_ops(struct super_block *sb) {
351 #if !defined(NEW_EXPORT_OPS)
353 * decode_fh will call this function. If not defined for this FS, make
354 * sure it points to the default
356 if (!sb->s_export_op->find_exported_dentry) {
357 /* Some kernels (at least 2.6.9) do not prototype find_exported_dentry,
358 * even though it is exported, so prototype it ourselves. Newer
359 * kernels do prototype it, but as long as our protoype matches the
360 * real one (the signature never changed before NEW_EXPORT_OPS came
361 * into play), there should be no problems. */
362 extern struct dentry * find_exported_dentry(struct super_block *sb, void *obj, void *parent,
363 int (*acceptable)(void *context, struct dentry *de),
365 sb->s_export_op->find_exported_dentry = find_exported_dentry;
371 afs_linux_lock_inode(struct inode *ip) {
372 #ifdef STRUCT_INODE_HAS_I_MUTEX
373 mutex_lock(&ip->i_mutex);
380 afs_linux_unlock_inode(struct inode *ip) {
381 #ifdef STRUCT_INODE_HAS_I_MUTEX
382 mutex_unlock(&ip->i_mutex);
389 afs_inode_setattr(struct osi_file *afile, struct iattr *newattrs) {
392 struct inode *inode = OSIFILE_INODE(afile);
393 #if !defined(HAVE_LINUX_INODE_SETATTR)
394 code = inode->i_op->setattr(afile->filp->f_dentry, newattrs);
395 #elif defined(INODE_SETATTR_NOT_VOID)
396 if (inode->i_op && inode->i_op->setattr)
397 code = inode->i_op->setattr(afile->filp->f_dentry, newattrs);
399 code = inode_setattr(inode, newattrs);
401 inode_setattr(inode, newattrs);
407 afs_kern_path(char *aname, int flags, struct nameidata *nd, struct path *path) {
408 #if defined(HAVE_LINUX_PATH_LOOKUP)
409 return path_lookup(aname, flags, nd);
411 return kern_path(aname, flags, path);
416 afs_get_dentry_ref(struct nameidata *nd, struct path *path, struct vfsmount **mnt, struct dentry **dpp) {
417 #if defined(STRUCT_NAMEIDATA_HAS_PATH)
418 # if defined(HAVE_LINUX_PATH_LOOKUP)
419 *dpp = dget(nd->path.dentry);
421 *mnt = mntget(nd->path.mnt);
424 *dpp = dget(path->dentry);
426 *mnt = mntget(path->mnt);
430 *dpp = dget(nd->dentry);
432 *mnt = mntget(nd->mnt);
437 #endif /* AFS_LINUX_OSI_COMPAT_H */