Linux: adapt to truncate sequence changes
[openafs.git] / src / afs / LINUX / osi_compat.h
1 /* Kernel compatibility routines
2  *
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
6  * little cleaner...
7  */
8
9 #ifndef AFS_LINUX_OSI_COMPAT_H
10 #define AFS_LINUX_OSI_COMPAT_H
11
12 #if defined(HAVE_LINUX_FREEZER_H)
13 # include <linux/freezer.h>
14 #endif
15
16 #ifndef HAVE_LINUX_DO_SYNC_READ
17 static inline int
18 do_sync_read(struct file *fp, char *buf, size_t count, loff_t *offp) {
19     return generic_file_read(fp, buf, count, offp);
20 }
21
22 static inline int
23 do_sync_write(struct file *fp, char *buf, size_t count, loff_t *offp) {
24     return generic_file_write(fp, buf, count, offp);
25 }
26
27 #endif /* DO_SYNC_READ */
28
29 static inline int
30 afs_posix_lock_file(struct file *fp, struct file_lock *flp) {
31 #ifdef POSIX_LOCK_FILE_WAIT_ARG
32     return posix_lock_file(fp, flp, NULL);
33 #else
34     flp->fl_flags &=~ FL_SLEEP;
35     return posix_lock_file(fp, flp);
36 #endif
37 }
38
39 static inline void
40 afs_posix_test_lock(struct file *fp, struct file_lock *flp) {
41 #if defined(POSIX_TEST_LOCK_CONFLICT_ARG)
42     struct file_lock conflict;
43     if (posix_test_lock(fp, flp, &conflict)) {
44         locks_copy_lock(flp, &conflict);
45         flp->fl_type = F_UNLCK;
46     }
47 #elif defined(POSIX_TEST_LOCK_RETURNS_CONFLICT)
48     struct file_lock *conflict;
49     if (conflict = posix_test_lock(fp, flp)) {
50         locks_copy_lock(flp, conflict);
51         flp->fl_type = F_UNLCK;
52     }
53 #else
54     posix_test_lock(fp, flp);
55 #endif
56 }
57
58 #ifdef DCACHE_NFSFS_RENAMED
59 static inline void
60 afs_linux_clear_nfsfs_renamed(struct dentry *dp) {
61     spin_lock(&dp->d_lock);
62     dp->d_flags &= ~DCACHE_NFSFS_RENAMED;
63     spin_unlock(&dp->d_lock);
64 }
65
66 static inline void
67 afs_linux_set_nfsfs_renamed(struct dentry *dp) {
68     spin_lock(&dp->d_lock);
69     dp->d_flags |= DCACHE_NFSFS_RENAMED;
70     spin_unlock(&dp->d_lock);
71 }
72
73 static inline int
74 afs_linux_nfsfs_renamed(struct dentry *dp) {
75     return dp->d_flags & DCACHE_NFSFS_RENAMED;
76 }
77
78 #else
79 static inline void afs_linux_clear_nfsfs_renamed(void) { return; }
80 static inline void afs_linux_set_nfsfs_renamed(void) { return; }
81 #endif
82
83 #ifndef HAVE_LINUX_HLIST_UNHASHED
84 static void
85 hlist_unhashed(const struct hlist_node *h) {
86     return (!h->pprev == NULL);
87 }
88 #endif
89
90 #if defined(WRITEPAGE_ACTIVATE)
91 #define AOP_WRITEPAGE_ACTIVATE WRITEPAGE_ACTIVATE
92 #endif
93
94 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN) && !defined(HAVE_LINUX_GRAB_CACHE_PAGE_WRITE_BEGIN)
95 static inline struct page *
96 grab_cache_page_write_begin(struct address_space *mapping, pgoff_t index,
97                             unsigned int flags) {
98     return __grab_cache_page(mapping, index);
99 }
100 #endif
101
102 #if defined(HAVE_KMEM_CACHE_T)
103 #define afs_kmem_cache_t kmem_cache_t
104 #else
105 #define afs_kmem_cache_t struct kmem_cache
106 #endif
107
108 extern void init_once(void *);
109 #if defined(HAVE_KMEM_CACHE_T)
110 static inline void
111 init_once_func(void * foo, kmem_cache_t * cachep, unsigned long flags) {
112 #if defined(SLAB_CTOR_VERIFY)
113     if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
114         SLAB_CTOR_CONSTRUCTOR)
115 #endif
116     init_once(foo);
117 }
118 #elif defined(KMEM_CACHE_INIT)
119 static inline void
120 init_once_func(struct kmem_cache * cachep, void * foo) {
121     init_once(foo);
122 }
123 #elif !defined(KMEM_CACHE_CTOR_TAKES_VOID)
124 static inline void
125 init_once_func(void * foo, struct kmem_cache * cachep, unsigned long flags) {
126 #if defined(SLAB_CTOR_VERIFY)
127     if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
128         SLAB_CTOR_CONSTRUCTOR)
129 #endif
130     init_once(foo);
131 }
132 #else
133 static inline void
134 init_once_func(void * foo) {
135     init_once(foo);
136 }
137 #endif
138
139 #ifndef SLAB_RECLAIM_ACCOUNT
140 #define SLAB_RECLAIM_ACCOUNT 0
141 #endif
142
143 #if defined(SLAB_KERNEL)
144 #define KALLOC_TYPE SLAB_KERNEL
145 #else
146 #define KALLOC_TYPE GFP_KERNEL
147 #endif
148
149 static inline struct key *
150 afs_linux_key_alloc(struct key_type *type, const char *desc, uid_t uid,
151                     gid_t gid, key_perm_t perm, unsigned long flags)
152 {
153 #if defined(KEY_ALLOC_NEEDS_STRUCT_TASK)
154     return key_alloc(type, desc, uid, gid, current, perm, flags);
155 #elif defined(KEY_ALLOC_NEEDS_CRED)
156     return key_alloc(type, desc, uid, gid, current_cred(), perm, flags);
157 #else
158     return key_alloc(type, desc, uid, gid, perm, flags);
159 #endif
160 }
161
162 #if defined(STRUCT_TASK_STRUCT_HAS_CRED)
163 static inline struct key*
164 afs_linux_search_keyring(afs_ucred_t *cred, struct key_type *type)
165 {
166     key_ref_t key_ref;
167
168     if (cred->tgcred->session_keyring) {
169         key_ref = keyring_search(
170                       make_key_ref(cred->tgcred->session_keyring, 1),
171                       type, "_pag");
172         if (IS_ERR(key_ref))
173             return ERR_CAST(key_ref);
174
175         return key_ref_to_ptr(key_ref);
176     }
177
178     return ERR_PTR(-ENOKEY);
179 }
180
181 static inline int
182 afs_linux_cred_is_current(afs_ucred_t *cred)
183 {
184     return (cred == current_cred());
185 }
186
187 #else
188 static inline struct key*
189 afs_linux_search_keyring(afs_ucred_t *cred, struct key_type *type)
190 {
191     return request_key(type, "_pag", NULL);
192 }
193
194 static inline int
195 afs_linux_cred_is_current(afs_ucred_t *cred)
196 {
197     return 1;
198 }
199 #endif
200
201 #ifdef LINUX_KEYRING_SUPPORT
202 # ifndef KEY_ALLOC_NOT_IN_QUOTA
203 #  define KEY_ALLOC_NOT_IN_QUOTA 1
204 # endif
205 # ifndef KEY_ALLOC_IN_QUOTA
206 #  define KEY_ALLOC_IN_QUOTA 0
207 # endif
208 #endif
209 #endif
210
211 #ifndef HAVE_LINUX_PAGE_OFFSET
212 static inline loff_t
213 page_offset(struct page *pp)
214 {
215     return (((loff_t) pp->index) << PAGE_CACHE_SHIFT);
216 }
217 #endif
218
219 #ifndef HAVE_LINUX_ZERO_USER_SEGMENTS
220 static inline void
221 zero_user_segments(struct page *pp, unsigned int from1, unsigned int to1,
222                    unsigned int from2, unsigned int to2)
223 {
224     void *base = kmap_atomic(pp, KM_USER0);
225
226     if (to1 > from1)
227         memset(base + from1, 0, to1 - from1);
228
229     if (to2 > from2)
230         memset(base + from2, 0, to2 - from2);
231
232     flush_dcache_page(pp);
233     kunmap_atomic(base, KM_USER0);
234 }
235 #endif
236
237 #ifndef HAVE_LINUX_KERNEL_SETSOCKOPT
238 /* Available from 2.6.19 */
239
240 static inline int
241 kernel_setsockopt(struct socket *sockp, int level, int name, char *val,
242                   unsigned int len) {
243     mm_segment_t old_fs = get_fs();
244     int ret;
245
246     set_fs(get_ds());
247     ret = sockp->ops->setsockopt(sockp, level, name, val, len);
248     set_fs(old_fs);
249
250     return ret;
251 }
252
253 static inline int
254 kernel_getsockopt(struct socket *sockp, int level, int name, char *val,
255                   int *len) {
256     mm_segment_t old_fs = get_fs();
257     int ret;
258
259     set_fs(get_ds());
260     ret = sockp->ops->setsockopt(sockp, level, name, val, len);
261     set_fs(old_fs);
262
263     return ret;
264 }
265 #endif
266
267 #ifdef HAVE_TRY_TO_FREEZE
268 static inline void
269 afs_try_to_freeze(void) {
270 # ifdef LINUX_REFRIGERATOR_TAKES_PF_FREEZE
271     try_to_freeze(PF_FREEZE);
272 # else
273     try_to_freeze();
274 # endif
275 }
276 #else
277 static inline void
278 afs_try_to_freeze(void) {
279 # ifdef CONFIG_PM
280     if (current->flags & PF_FREEZE) {
281         refrigerator(PF_FREEZE);
282 # endif
283 }
284 #endif
285
286 #if !defined(HAVE_LINUX_PAGECHECKED)
287 # if defined(HAVE_LINUX_PAGEFSMISC)
288 #  include <linux/page-flags.h>
289
290 #  define PageChecked(p)            PageFsMisc((p))
291 #  define SetPageChecked(p)         SetPageFsMisc((p))
292 #  define ClearPageChecked(p)       ClearPageFsMisc((p))
293
294 # endif
295 #endif
296
297 #if !defined(NEW_EXPORT_OPS)
298 extern struct export_operations export_op_default;
299 #endif
300
301 static inline struct dentry *
302 afs_get_dentry_from_fh(struct super_block *afs_cacheSBp, afs_dcache_id_t *ainode,
303                 int cache_fh_len, int cache_fh_type,
304                 int (*afs_fh_acceptable)(void *, struct dentry *)) {
305 #if defined(NEW_EXPORT_OPS)
306     return afs_cacheSBp->s_export_op->fh_to_dentry(afs_cacheSBp, &ainode->ufs.fh,
307                 cache_fh_len, cache_fh_type);
308 #else
309     if (afs_cacheSBp->s_export_op && afs_cacheSBp->s_export_op->decode_fh)
310         return afs_cacheSBp->s_export_op->decode_fh(afs_cacheSBp, ainode->ufs.raw,
311                         cache_fh_len, cache_fh_type, afs_fh_acceptable, NULL);
312     else
313         return export_op_default.decode_fh(afs_cacheSBp, ainode->ufs.raw,
314                         cache_fh_len, cache_fh_type, afs_fh_acceptable, NULL);
315 #endif
316 }
317
318 static inline int
319 afs_get_fh_from_dentry(struct dentry *dp, afs_ufs_dcache_id_t *ainode, int *max_lenp) {
320     if (dp->d_sb->s_export_op->encode_fh)
321         return dp->d_sb->s_export_op->encode_fh(dp, &ainode->raw[0], max_lenp, 0);
322 #if defined(NEW_EXPORT_OPS)
323     /* If fs doesn't provide an encode_fh method, assume the default INO32 type */
324     *max_lenp = sizeof(struct fid)/4;
325     ainode->fh.i32.ino = dp->d_inode->i_ino;
326     ainode->fh.i32.gen = dp->d_inode->i_generation;
327     return FILEID_INO32_GEN;
328 #else
329     /* or call the default encoding function for the old API */
330     return export_op_default.encode_fh(dp, &ainode->raw[0], max_lenp, 0);
331 #endif
332 }
333
334 static inline void
335 afs_init_sb_export_ops(struct super_block *sb) {
336 #if !defined(NEW_EXPORT_OPS)
337     /*
338      * decode_fh will call this function.  If not defined for this FS, make
339      * sure it points to the default
340      */
341     if (!sb->s_export_op->find_exported_dentry)
342         sb->s_export_op->find_exported_dentry = find_exported_dentry;
343 #endif
344 }
345
346 static inline void
347 afs_linux_lock_inode(struct inode *ip) {
348 #ifdef STRUCT_INODE_HAS_I_MUTEX
349     mutex_lock(&ip->i_mutex);
350 #else
351     down(&ip->i_sem);
352 #endif
353 }
354
355 static inline void
356 afs_linux_unlock_inode(struct inode *ip) {
357 #ifdef STRUCT_INODE_HAS_I_MUTEX
358     mutex_unlock(&ip->i_mutex);
359 #else
360     up(&ip->i_sem);
361 #endif
362 }
363
364 static inline int
365 afs_inode_setattr(struct osi_file *afile, struct iattr *newattrs) {
366
367     int code = 0;
368     struct inode *inode = OSIFILE_INODE(afile);
369 #if !defined(HAVE_LINUX_INODE_SETATTR)
370     code = inode->i_op->setattr(afile->filp->f_dentry, newattrs);
371 #elif defined(INODE_SETATTR_NOT_VOID)
372     if (inode->i_op && inode->i_op->setattr)
373         code = inode->i_op->setattr(afile->filp->f_dentry, newattrs);
374     else
375         code = inode_setattr(inode, newattrs);
376 #else
377     inode_setattr(inode, newattrs);
378 #endif
379     return code;
380 }