Whamcloud - gitweb
d13ea3195e02f06f11ca624ce403c981d606b689
[fs/lustre-release.git] / lustre / include / linux / lustre_compat25.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * Copyright (c) 2011 Whamcloud, Inc.
34  */
35 /*
36  * This file is part of Lustre, http://www.lustre.org/
37  * Lustre is a trademark of Sun Microsystems, Inc.
38  */
39
40 #ifndef _LINUX_COMPAT25_H
41 #define _LINUX_COMPAT25_H
42
43 #ifdef __KERNEL__
44
45 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,9)
46 #error sorry, lustre requires at least linux kernel 2.6.9 or later
47 #endif
48
49 #include <linux/fs_struct.h>
50 #include <linux/namei.h>
51 #include <libcfs/linux/portals_compat25.h>
52
53 #include <linux/lustre_patchless_compat.h>
54
55 /* Some old kernels (like 2.6.9) may not define such SEEK_XXX. So the
56  * definition allows to compile lustre client on more OS platforms. */
57 #ifndef SEEK_SET
58  #define SEEK_SET 0
59  #define SEEK_CUR 1
60  #define SEEK_END 2
61 #endif
62
63 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
64 struct ll_iattr {
65         struct iattr    iattr;
66         unsigned int    ia_attr_flags;
67 };
68 #else
69 #define ll_iattr iattr
70 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14) */
71
72 #ifdef HAVE_FS_STRUCT_RWLOCK
73 # define LOCK_FS_STRUCT(fs)   cfs_write_lock(&(fs)->lock)
74 # define UNLOCK_FS_STRUCT(fs) cfs_write_unlock(&(fs)->lock)
75 #else
76 # define LOCK_FS_STRUCT(fs)   cfs_spin_lock(&(fs)->lock)
77 # define UNLOCK_FS_STRUCT(fs) cfs_spin_unlock(&(fs)->lock)
78 #endif
79
80 #ifdef HAVE_FS_STRUCT_USE_PATH
81 static inline void ll_set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
82                                  struct dentry *dentry)
83 {
84         struct path path;
85         struct path old_pwd;
86
87         path.mnt = mnt;
88         path.dentry = dentry;
89         LOCK_FS_STRUCT(fs);
90         old_pwd = fs->pwd;
91         path_get(&path);
92         fs->pwd = path;
93         UNLOCK_FS_STRUCT(fs);
94
95         if (old_pwd.dentry)
96                 path_put(&old_pwd);
97 }
98
99 #else
100
101 static inline void ll_set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
102                 struct dentry *dentry)
103 {
104         struct dentry *old_pwd;
105         struct vfsmount *old_pwdmnt;
106
107         LOCK_FS_STRUCT(fs);
108         old_pwd = fs->pwd;
109         old_pwdmnt = fs->pwdmnt;
110         fs->pwdmnt = mntget(mnt);
111         fs->pwd = dget(dentry);
112         UNLOCK_FS_STRUCT(fs);
113
114         if (old_pwd) {
115                 dput(old_pwd);
116                 mntput(old_pwdmnt);
117         }
118 }
119 #endif
120
121 /*
122  * set ATTR_BLOCKS to a high value to avoid any risk of collision with other
123  * ATTR_* attributes (see bug 13828)
124  */
125 #define ATTR_BLOCKS    (1 << 27)
126
127 #if HAVE_INODE_I_MUTEX
128 #define UNLOCK_INODE_MUTEX(inode) \
129 do {cfs_mutex_unlock(&(inode)->i_mutex); } while(0)
130 #define LOCK_INODE_MUTEX(inode) \
131 do {cfs_mutex_lock(&(inode)->i_mutex); } while(0)
132 #define LOCK_INODE_MUTEX_PARENT(inode) \
133 do {cfs_mutex_lock_nested(&(inode)->i_mutex, I_MUTEX_PARENT); } while(0)
134 #define TRYLOCK_INODE_MUTEX(inode) cfs_mutex_trylock(&(inode)->i_mutex)
135 #else
136 #define UNLOCK_INODE_MUTEX(inode) do  cfs_up(&(inode)->i_sem); } while(0)
137 #define LOCK_INODE_MUTEX(inode) do  cfs_down(&(inode)->i_sem); } while(0)
138 #define TRYLOCK_INODE_MUTEX(inode) (!down_trylock(&(inode)->i_sem))
139 #define LOCK_INODE_MUTEX_PARENT(inode) LOCK_INODE_MUTEX(inode)
140 #endif /* HAVE_INODE_I_MUTEX */
141
142 #ifdef HAVE_SEQ_LOCK
143 #define LL_SEQ_LOCK(seq) cfs_mutex_lock(&(seq)->lock)
144 #define LL_SEQ_UNLOCK(seq) cfs_mutex_unlock(&(seq)->lock)
145 #else
146 #define LL_SEQ_LOCK(seq) cfs_down(&(seq)->sem)
147 #define LL_SEQ_UNLOCK(seq) cfs_up(&(seq)->sem)
148 #endif
149
150 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,15)
151 #define d_child d_u.d_child
152 #define d_rcu d_u.d_rcu
153 #endif
154
155 #ifdef HAVE_DQUOTOFF_MUTEX
156 #define UNLOCK_DQONOFF_MUTEX(dqopt) cfs_mutex_unlock(&(dqopt)->dqonoff_mutex)
157 #define LOCK_DQONOFF_MUTEX(dqopt) cfs_mutex_lock(&(dqopt)->dqonoff_mutex)
158 #else
159 #define UNLOCK_DQONOFF_MUTEX(dqopt) cfs_up(&(dqopt)->dqonoff_sem)
160 #define LOCK_DQONOFF_MUTEX(dqopt) cfs_down(&(dqopt)->dqonoff_sem)
161 #endif /* HAVE_DQUOTOFF_MUTEX */
162
163 #define current_ngroups current_cred()->group_info->ngroups
164 #define current_groups current_cred()->group_info->small_block
165
166 #ifndef page_private
167 #define page_private(page) ((page)->private)
168 #define set_page_private(page, v) ((page)->private = (v))
169 #endif
170
171 #ifndef HAVE_GFP_T
172 #define gfp_t int
173 #endif
174
175 #define lock_dentry(___dentry)          cfs_spin_lock(&(___dentry)->d_lock)
176 #define unlock_dentry(___dentry)        cfs_spin_unlock(&(___dentry)->d_lock)
177
178 #define ll_kernel_locked()      kernel_locked()
179
180 /*
181  * OBD need working random driver, thus all our
182  * initialization routines must be called after device
183  * driver initialization
184  */
185 #ifndef MODULE
186 #undef module_init
187 #define module_init(a)     late_initcall(a)
188 #endif
189
190 /* XXX our code should be using the 2.6 calls, not the other way around */
191 #ifdef HAVE_TRYLOCK_PAGE
192 #define TestSetPageLocked(page)         (!trylock_page(page))
193 #endif
194
195 #define Page_Uptodate(page)             PageUptodate(page)
196 #define ll_redirty_page(page)           set_page_dirty(page)
197
198 #define KDEVT_INIT(val)                 (val)
199
200 #define LTIME_S(time)                   (time.tv_sec)
201 #define ll_path_lookup                  path_lookup
202
203 #ifdef HAVE_EXPORT_INODE_PERMISSION
204 #define ll_permission(inode,mask,nd)    inode_permission(inode,mask)
205 #else
206 #define ll_permission(inode,mask,nd)    permission(inode,mask,nd)
207 #endif
208
209 #ifdef HAVE_GENERIC_PERMISSION_4ARGS
210 #define ll_generic_permission(inode, mask, flags, check_acl) \
211         generic_permission(inode, mask, flags, check_acl)
212 #else
213 #define ll_generic_permission(inode, mask, flags, check_acl) \
214         generic_permission(inode, mask, check_acl)
215 #endif
216
217 #define ll_pgcache_lock(mapping)          cfs_spin_lock(&mapping->page_lock)
218 #define ll_pgcache_unlock(mapping)        cfs_spin_unlock(&mapping->page_lock)
219 #define ll_call_writepage(inode, page)  \
220                                 (inode)->i_mapping->a_ops->writepage(page, NULL)
221 #define ll_invalidate_inode_pages(inode) \
222                                 invalidate_inode_pages((inode)->i_mapping)
223
224 #define ll_vfs_create(a,b,c,d)          vfs_create(a,b,c,d)
225 #define ll_dev_t                        dev_t
226 #define kdev_t                          dev_t
227 #define to_kdev_t(dev)                  (dev)
228 #define kdev_t_to_nr(dev)               (dev)
229 #define val_to_kdev(dev)                (dev)
230
231 #ifdef HAVE_BLKDEV_PUT_2ARGS
232 #define ll_blkdev_put(a, b) blkdev_put(a, b)
233 #else
234 #define ll_blkdev_put(a, b) blkdev_put(a)
235 #endif
236
237 #ifdef HAVE_DENTRY_OPEN_4ARGS
238 #define ll_dentry_open(a, b, c, d) dentry_open(a, b, c, d)
239 #else
240 #define ll_dentry_open(a, b, c, d) dentry_open(a, b, c)
241 #endif
242
243 #include <linux/writeback.h>
244
245 static inline int cfs_cleanup_group_info(void)
246 {
247         struct group_info *ginfo;
248
249         ginfo = groups_alloc(0);
250         if (!ginfo)
251                 return -ENOMEM;
252
253         set_current_groups(ginfo);
254         put_group_info(ginfo);
255
256         return 0;
257 }
258
259 #define __set_page_ll_data(page, llap) \
260         do {       \
261                 page_cache_get(page); \
262                 SetPagePrivate(page); \
263                 set_page_private(page, (unsigned long)llap); \
264         } while (0)
265 #define __clear_page_ll_data(page) \
266         do {       \
267                 ClearPagePrivate(page); \
268                 set_page_private(page, 0); \
269                 page_cache_release(page); \
270         } while(0)
271
272 #define kiobuf bio
273
274 #include <linux/proc_fs.h>
275
276 #if !defined(HAVE_D_REHASH_COND) && defined(HAVE___D_REHASH)
277 #define d_rehash_cond(dentry, lock) __d_rehash(dentry, lock)
278 extern void __d_rehash(struct dentry *dentry, int lock);
279 #endif
280
281 #ifdef HAVE_CAN_SLEEP_ARG
282 #define ll_flock_lock_file_wait(file, lock, can_sleep) \
283         flock_lock_file_wait(file, lock, can_sleep)
284 #else
285 #define ll_flock_lock_file_wait(file, lock, can_sleep) \
286         flock_lock_file_wait(file, lock)
287 #endif
288
289 #define CheckWriteback(page, cmd) \
290         ((!PageWriteback(page) && (cmd & OBD_BRW_READ)) || \
291          (PageWriteback(page) && (cmd & OBD_BRW_WRITE)))
292
293 #ifdef HAVE_PAGE_LIST
294 static inline int mapping_has_pages(struct address_space *mapping)
295 {
296         int rc = 1;
297
298         ll_pgcache_lock(mapping);
299         if (cfs_list_empty(&mapping->dirty_pages) &&
300             cfs_list_empty(&mapping->clean_pages) &&
301             cfs_list_empty(&mapping->locked_pages)) {
302                 rc = 0;
303         }
304         ll_pgcache_unlock(mapping);
305
306         return rc;
307 }
308 #else
309 static inline int mapping_has_pages(struct address_space *mapping)
310 {
311         return mapping->nrpages > 0;
312 }
313 #endif
314
315 #ifdef HAVE_KIOBUF_KIO_BLOCKS
316 #define KIOBUF_GET_BLOCKS(k) ((k)->kio_blocks)
317 #else
318 #define KIOBUF_GET_BLOCKS(k) ((k)->blocks)
319 #endif
320
321 #ifdef HAVE_SECURITY_PLUG
322 #define ll_vfs_symlink(dir, dentry, mnt, path, mode) \
323                 vfs_symlink(dir, dentry, mnt, path, mode)
324 #else
325 #ifdef HAVE_4ARGS_VFS_SYMLINK
326 #define ll_vfs_symlink(dir, dentry, mnt, path, mode) \
327                 vfs_symlink(dir, dentry, path, mode)
328 #else
329 #define ll_vfs_symlink(dir, dentry, mnt, path, mode) \
330                        vfs_symlink(dir, dentry, path)
331 #endif
332
333 #define ll_set_dflags(dentry, flags) do { \
334                 cfs_spin_lock(&dentry->d_lock); \
335                 dentry->d_flags |= flags; \
336                 cfs_spin_unlock(&dentry->d_lock); \
337         } while(0)
338 #endif
339
340 #ifndef container_of
341 #define container_of(ptr, type, member) ({                      \
342                 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
343                 (type *)( (char *)__mptr - offsetof(type,member) );})
344 #endif
345
346 #define UP_WRITE_I_ALLOC_SEM(i)   up_write(&(i)->i_alloc_sem)
347 #define DOWN_WRITE_I_ALLOC_SEM(i) down_write(&(i)->i_alloc_sem)
348 #define LASSERT_I_ALLOC_SEM_WRITE_LOCKED(i) LASSERT(down_read_trylock(&(i)->i_alloc_sem) == 0)
349
350 #define UP_READ_I_ALLOC_SEM(i)    up_read(&(i)->i_alloc_sem)
351 #define DOWN_READ_I_ALLOC_SEM(i)  down_read(&(i)->i_alloc_sem)
352 #define LASSERT_I_ALLOC_SEM_READ_LOCKED(i) LASSERT(down_write_trylock(&(i)->i_alloc_sem) == 0)
353
354 #ifndef HAVE_GRAB_CACHE_PAGE_NOWAIT_GFP
355 #define grab_cache_page_nowait_gfp(x, y, z) grab_cache_page_nowait((x), (y))
356 #endif
357
358 #include <linux/mpage.h>        /* for generic_writepages */
359 #ifndef HAVE_FILEMAP_FDATAWRITE_RANGE
360 #include <linux/backing-dev.h>  /* for mapping->backing_dev_info */
361 static inline int filemap_fdatawrite_range(struct address_space *mapping,
362                                            loff_t start, loff_t end)
363 {
364         int rc;
365         struct writeback_control wbc = {
366                 .sync_mode = WB_SYNC_ALL,
367                 .nr_to_write = (end - start + PAGE_SIZE - 1) >> PAGE_SHIFT,
368         };
369
370 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,18)
371         wbc.range_start = start;
372         wbc.range_end = end;
373 #else
374         wbc.start = start;
375         wbc.end = end;
376 #endif
377
378 #ifdef HAVE_MAPPING_CAP_WRITEBACK_DIRTY
379         if (!mapping_cap_writeback_dirty(mapping))
380                 rc = 0;
381 #else
382         if (mapping->backing_dev_info->memory_backed)
383                 rc = 0;
384 #endif
385         /* do_writepages() */
386         else if (mapping->a_ops->writepages)
387                 rc = mapping->a_ops->writepages(mapping, &wbc);
388         else
389                 rc = generic_writepages(mapping, &wbc);
390         return rc;
391 }
392 #else
393 int filemap_fdatawrite_range(struct address_space *mapping,
394                              loff_t start, loff_t end);
395 #endif /* HAVE_FILEMAP_FDATAWRITE_RANGE */
396
397 #ifdef HAVE_VFS_KERN_MOUNT
398 static inline struct vfsmount *
399 ll_kern_mount(const char *fstype, int flags, const char *name, void *data)
400 {
401         struct file_system_type *type = get_fs_type(fstype);
402         struct vfsmount *mnt;
403         if (!type)
404                 return ERR_PTR(-ENODEV);
405         mnt = vfs_kern_mount(type, flags, name, data);
406         cfs_module_put(type->owner);
407         return mnt;
408 }
409 #else
410 #define ll_kern_mount(fstype, flags, name, data) do_kern_mount((fstype), (flags), (name), (data))
411 #endif
412
413 #ifndef HAVE_ATOMIC_MNT_COUNT
414 static inline unsigned int mnt_get_count(struct vfsmount *mnt)
415 {
416 #ifdef CONFIG_SMP
417         unsigned int count = 0;
418         int cpu;
419
420         for_each_possible_cpu(cpu) {
421                 count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
422         }
423
424         return count;
425 #else
426         return mnt->mnt_count;
427 #endif
428 }
429 #else
430 # define mnt_get_count(mnt)      cfs_atomic_read(&mnt->mnt_count)
431 #endif
432
433 #ifdef HAVE_STATFS_DENTRY_PARAM
434 #define ll_do_statfs(sb, sfs) (sb)->s_op->statfs((sb)->s_root, (sfs))
435 #else
436 #define ll_do_statfs(sb, sfs) (sb)->s_op->statfs((sb), (sfs))
437 #endif
438
439 #ifndef HAVE_SB_TIME_GRAN
440 #ifndef HAVE_S_TIME_GRAN
441 #error Need s_time_gran patch!
442 #endif
443 static inline u32 get_sb_time_gran(struct super_block *sb)
444 {
445         return sb->s_time_gran;
446 }
447 #endif
448
449 #ifdef HAVE_RW_TREE_LOCK
450 #define TREE_READ_LOCK_IRQ(mapping)     read_lock_irq(&(mapping)->tree_lock)
451 #define TREE_READ_UNLOCK_IRQ(mapping) read_unlock_irq(&(mapping)->tree_lock)
452 #else
453 #define TREE_READ_LOCK_IRQ(mapping) cfs_spin_lock_irq(&(mapping)->tree_lock)
454 #define TREE_READ_UNLOCK_IRQ(mapping) cfs_spin_unlock_irq(&(mapping)->tree_lock)
455 #endif
456
457 #ifdef HAVE_UNREGISTER_BLKDEV_RETURN_INT
458 #define ll_unregister_blkdev(a,b)       unregister_blkdev((a),(b))
459 #else
460 static inline
461 int ll_unregister_blkdev(unsigned int dev, const char *name)
462 {
463         unregister_blkdev(dev, name);
464         return 0;
465 }
466 #endif
467
468 #ifdef HAVE_INVALIDATE_BDEV_2ARG
469 #define ll_invalidate_bdev(a,b)         invalidate_bdev((a),(b))
470 #else
471 #define ll_invalidate_bdev(a,b)         invalidate_bdev((a))
472 #endif
473
474 #ifdef HAVE_INODE_BLKSIZE
475 #define ll_inode_blksize(a)     (a)->i_blksize
476 #else
477 #define ll_inode_blksize(a)     (1<<(a)->i_blkbits)
478 #endif
479
480 #ifdef HAVE_FS_RENAME_DOES_D_MOVE
481 #define LL_RENAME_DOES_D_MOVE   FS_RENAME_DOES_D_MOVE
482 #else
483 #define LL_RENAME_DOES_D_MOVE   FS_ODD_RENAME
484 #endif
485
486 #ifndef HAVE_D_OBTAIN_ALIAS
487 /* The old d_alloc_anon() didn't free the inode reference on error
488  * like d_obtain_alias().  Hide that difference/inconvenience here. */
489 static inline struct dentry *d_obtain_alias(struct inode *inode)
490 {
491         struct dentry *anon = d_alloc_anon(inode);
492
493         if (anon == NULL) {
494                 iput(inode);
495                 anon = ERR_PTR(-ENOMEM);
496         }
497
498         return anon;
499 }
500 #endif
501
502 /* add a lustre compatible layer for crypto API */
503 #include <linux/crypto.h>
504 #ifdef HAVE_ASYNC_BLOCK_CIPHER
505 #define ll_crypto_hash          crypto_hash
506 #define ll_crypto_cipher        crypto_blkcipher
507 #define ll_crypto_alloc_hash(name, type, mask)  crypto_alloc_hash(name, type, mask)
508 #define ll_crypto_hash_setkey(tfm, key, keylen) crypto_hash_setkey(tfm, key, keylen)
509 #define ll_crypto_hash_init(desc)               crypto_hash_init(desc)
510 #define ll_crypto_hash_update(desc, sl, bytes)  crypto_hash_update(desc, sl, bytes)
511 #define ll_crypto_hash_final(desc, out)         crypto_hash_final(desc, out)
512 #define ll_crypto_alloc_blkcipher(name, type, mask) \
513                 crypto_alloc_blkcipher(name ,type, mask)
514 #define ll_crypto_blkcipher_setkey(tfm, key, keylen) \
515                 crypto_blkcipher_setkey(tfm, key, keylen)
516 #define ll_crypto_blkcipher_set_iv(tfm, src, len) \
517                 crypto_blkcipher_set_iv(tfm, src, len)
518 #define ll_crypto_blkcipher_get_iv(tfm, dst, len) \
519                 crypto_blkcipher_get_iv(tfm, dst, len)
520 #define ll_crypto_blkcipher_encrypt(desc, dst, src, bytes) \
521                 crypto_blkcipher_encrypt(desc, dst, src, bytes)
522 #define ll_crypto_blkcipher_decrypt(desc, dst, src, bytes) \
523                 crypto_blkcipher_decrypt(desc, dst, src, bytes)
524 #define ll_crypto_blkcipher_encrypt_iv(desc, dst, src, bytes) \
525                 crypto_blkcipher_encrypt_iv(desc, dst, src, bytes)
526 #define ll_crypto_blkcipher_decrypt_iv(desc, dst, src, bytes) \
527                 crypto_blkcipher_decrypt_iv(desc, dst, src, bytes)
528
529 static inline int ll_crypto_hmac(struct ll_crypto_hash *tfm,
530                                  u8 *key, unsigned int *keylen,
531                                  struct scatterlist *sg,
532                                  unsigned int size, u8 *result)
533 {
534         struct hash_desc desc;
535         int              rv;
536         desc.tfm   = tfm;
537         desc.flags = 0;
538         rv = crypto_hash_setkey(desc.tfm, key, *keylen);
539         if (rv) {
540                 CERROR("failed to hash setkey: %d\n", rv);
541                 return rv;
542         }
543         return crypto_hash_digest(&desc, sg, size, result);
544 }
545 static inline
546 unsigned int ll_crypto_tfm_alg_max_keysize(struct crypto_blkcipher *tfm)
547 {
548         return crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher.max_keysize;
549 }
550 static inline
551 unsigned int ll_crypto_tfm_alg_min_keysize(struct crypto_blkcipher *tfm)
552 {
553         return crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher.min_keysize;
554 }
555
556 #define ll_crypto_hash_blocksize(tfm)       crypto_hash_blocksize(tfm)
557 #define ll_crypto_hash_digestsize(tfm)      crypto_hash_digestsize(tfm)
558 #define ll_crypto_blkcipher_ivsize(tfm)     crypto_blkcipher_ivsize(tfm)
559 #define ll_crypto_blkcipher_blocksize(tfm)  crypto_blkcipher_blocksize(tfm)
560 #define ll_crypto_free_hash(tfm)            crypto_free_hash(tfm)
561 #define ll_crypto_free_blkcipher(tfm)       crypto_free_blkcipher(tfm)
562 #else /* HAVE_ASYNC_BLOCK_CIPHER */
563 #include <linux/scatterlist.h>
564 #define ll_crypto_hash          crypto_tfm
565 #define ll_crypto_cipher        crypto_tfm
566 #ifndef HAVE_STRUCT_HASH_DESC
567 struct hash_desc {
568         struct ll_crypto_hash *tfm;
569         u32                    flags;
570 };
571 #endif
572 #ifndef HAVE_STRUCT_BLKCIPHER_DESC
573 struct blkcipher_desc {
574         struct ll_crypto_cipher *tfm;
575         void                    *info;
576         u32                      flags;
577 };
578 #endif
579 #define ll_crypto_blkcipher_setkey(tfm, key, keylen) \
580         crypto_cipher_setkey(tfm, key, keylen)
581 #define ll_crypto_blkcipher_set_iv(tfm, src, len) \
582         crypto_cipher_set_iv(tfm, src, len)
583 #define ll_crypto_blkcipher_get_iv(tfm, dst, len) \
584         crypto_cipher_get_iv(tfm, dst, len)
585 #define ll_crypto_blkcipher_encrypt(desc, dst, src, bytes) \
586         crypto_cipher_encrypt((desc)->tfm, dst, src, bytes)
587 #define ll_crypto_blkcipher_decrypt(desc, dst, src, bytes) \
588         crypto_cipher_decrypt((desc)->tfm, dst, src, bytes)
589 #define ll_crypto_blkcipher_decrypt_iv(desc, dst, src, bytes) \
590         crypto_cipher_decrypt_iv((desc)->tfm, dst, src, bytes, (desc)->info)
591 #define ll_crypto_blkcipher_encrypt_iv(desc, dst, src, bytes) \
592         crypto_cipher_encrypt_iv((desc)->tfm, dst, src, bytes, (desc)->info)
593
594 static inline
595 struct ll_crypto_cipher *ll_crypto_alloc_blkcipher(const char * algname,
596                                                    u32 type, u32 mask)
597 {
598         char        buf[CRYPTO_MAX_ALG_NAME + 1];
599         const char *pan = algname;
600         u32         flag = 0;
601
602         if (strncmp("cbc(", algname, 4) == 0)
603                 flag |= CRYPTO_TFM_MODE_CBC;
604         else if (strncmp("ecb(", algname, 4) == 0)
605                 flag |= CRYPTO_TFM_MODE_ECB;
606         if (flag) {
607                 char *vp = strnchr(algname, CRYPTO_MAX_ALG_NAME, ')');
608                 if (vp) {
609                         memcpy(buf, algname + 4, vp - algname - 4);
610                         buf[vp - algname - 4] = '\0';
611                         pan = buf;
612                 } else {
613                         flag = 0;
614                 }
615         }
616         return crypto_alloc_tfm(pan, flag);
617 }
618
619 static inline
620 struct ll_crypto_hash *ll_crypto_alloc_hash(const char *alg, u32 type, u32 mask)
621 {
622         char        buf[CRYPTO_MAX_ALG_NAME + 1];
623         const char *pan = alg;
624
625         if (strncmp("hmac(", alg, 5) == 0) {
626                 char *vp = strnchr(alg, CRYPTO_MAX_ALG_NAME, ')');
627                 if (vp) {
628                         memcpy(buf, alg+ 5, vp - alg- 5);
629                         buf[vp - alg - 5] = 0x00;
630                         pan = buf;
631                 }
632         }
633         return crypto_alloc_tfm(pan, 0);
634 }
635 static inline int ll_crypto_hash_init(struct hash_desc *desc)
636 {
637        crypto_digest_init(desc->tfm); return 0;
638 }
639 static inline int ll_crypto_hash_update(struct hash_desc *desc,
640                                         struct scatterlist *sg,
641                                         unsigned int nbytes)
642 {
643         struct scatterlist *sl = sg;
644         unsigned int        count;
645                 /*
646                  * This way is very weakness. We must ensure that
647                  * the sum of sg[0..i]->length isn't greater than nbytes.
648                  * In the upstream kernel the crypto_hash_update() also
649                  * via the nbytes computed the count of sg[...].
650                  * The old style is more safely. but it gone.
651                  */
652         for (count = 0; nbytes > 0; count ++, sl ++) {
653                 nbytes -= sl->length;
654         }
655         crypto_digest_update(desc->tfm, sg, count); return 0;
656 }
657 static inline int ll_crypto_hash_final(struct hash_desc *desc, u8 *out)
658 {
659         crypto_digest_final(desc->tfm, out); return 0;
660 }
661 static inline int ll_crypto_hmac(struct crypto_tfm *tfm,
662                                  u8 *key, unsigned int *keylen,
663                                  struct scatterlist *sg,
664                                  unsigned int nbytes,
665                                  u8 *out)
666 {
667         struct scatterlist *sl = sg;
668         int                 count;
669         for (count = 0; nbytes > 0; count ++, sl ++) {
670                 nbytes -= sl->length;
671         }
672         crypto_hmac(tfm, key, keylen, sg, count, out);
673         return 0;
674 }
675
676 #define ll_crypto_hash_setkey(tfm, key, keylen) crypto_digest_setkey(tfm, key, keylen)
677 #define ll_crypto_blkcipher_blocksize(tfm)      crypto_tfm_alg_blocksize(tfm)
678 #define ll_crypto_blkcipher_ivsize(tfm) crypto_tfm_alg_ivsize(tfm)
679 #define ll_crypto_hash_digestsize(tfm)  crypto_tfm_alg_digestsize(tfm)
680 #define ll_crypto_hash_blocksize(tfm)   crypto_tfm_alg_blocksize(tfm)
681 #define ll_crypto_free_hash(tfm)        crypto_free_tfm(tfm)
682 #define ll_crypto_free_blkcipher(tfm)   crypto_free_tfm(tfm)
683 #define ll_crypto_tfm_alg_min_keysize   crypto_tfm_alg_min_keysize
684 #define ll_crypto_tfm_alg_max_keysize   crypto_tfm_alg_max_keysize
685 #endif /* HAVE_ASYNC_BLOCK_CIPHER */
686
687 #ifndef HAVE_SYNCHRONIZE_RCU
688 /* Linux 2.6.32 provides define when !CONFIG_TREE_PREEMPT_RCU */
689 #ifndef synchronize_rcu
690 #define synchronize_rcu() synchronize_kernel()
691 #endif
692 #endif
693
694 #ifdef HAVE_FILE_REMOVE_SUID
695 # define ll_remove_suid(file, mnt)       file_remove_suid(file)
696 #else
697 # ifdef HAVE_SECURITY_PLUG
698 #  define ll_remove_suid(file,mnt)      remove_suid(file->f_dentry,mnt)
699 # else
700 #  define ll_remove_suid(file,mnt)      remove_suid(file->f_dentry)
701 # endif
702 #endif
703
704 #ifdef HAVE_SECURITY_PLUG
705 #define ll_vfs_rmdir(dir,entry,mnt)             vfs_rmdir(dir,entry,mnt)
706 #define ll_vfs_mkdir(inode,dir,mnt,mode)        vfs_mkdir(inode,dir,mnt,mode)
707 #define ll_vfs_link(old,mnt,dir,new,mnt1)       vfs_link(old,mnt,dir,new,mnt1)
708 #define ll_vfs_unlink(inode,entry,mnt)          vfs_unlink(inode,entry,mnt)
709 #define ll_vfs_mknod(dir,entry,mnt,mode,dev)            \
710                 vfs_mknod(dir,entry,mnt,mode,dev)
711 #define ll_security_inode_unlink(dir,entry,mnt)         \
712                 security_inode_unlink(dir,entry,mnt)
713 #define ll_vfs_rename(old,old_dir,mnt,new,new_dir,mnt1) \
714                 vfs_rename(old,old_dir,mnt,new,new_dir,mnt1)
715 #else
716 #define ll_vfs_rmdir(dir,entry,mnt)             vfs_rmdir(dir,entry)
717 #define ll_vfs_mkdir(inode,dir,mnt,mode)        vfs_mkdir(inode,dir,mode)
718 #define ll_vfs_link(old,mnt,dir,new,mnt1)       vfs_link(old,dir,new)
719 #define ll_vfs_unlink(inode,entry,mnt)          vfs_unlink(inode,entry)
720 #define ll_vfs_mknod(dir,entry,mnt,mode,dev)    vfs_mknod(dir,entry,mode,dev)
721 #define ll_security_inode_unlink(dir,entry,mnt) security_inode_unlink(dir,entry)
722 #define ll_vfs_rename(old,old_dir,mnt,new,new_dir,mnt1) \
723                 vfs_rename(old,old_dir,new,new_dir)
724 #endif /* HAVE_SECURITY_PLUG */
725
726 #ifdef for_each_possible_cpu
727 #define cfs_for_each_possible_cpu(cpu) for_each_possible_cpu(cpu)
728 #elif defined(for_each_cpu)
729 #define cfs_for_each_possible_cpu(cpu) for_each_cpu(cpu)
730 #endif
731
732 #ifdef HAVE_BIO_ENDIO_2ARG
733 #define cfs_bio_io_error(a,b)   bio_io_error((a))
734 #define cfs_bio_endio(a,b,c)    bio_endio((a),(c))
735 #else
736 #define cfs_bio_io_error(a,b)   bio_io_error((a),(b))
737 #define cfs_bio_endio(a,b,c)    bio_endio((a),(b),(c))
738 #endif
739
740 #ifdef HAVE_FS_STRUCT_USE_PATH
741 #define cfs_fs_pwd(fs)       ((fs)->pwd.dentry)
742 #define cfs_fs_mnt(fs)       ((fs)->pwd.mnt)
743 #define cfs_path_put(nd)     path_put(&(nd)->path)
744 #else
745 #define cfs_fs_pwd(fs)       ((fs)->pwd)
746 #define cfs_fs_mnt(fs)       ((fs)->pwdmnt)
747 #define cfs_path_put(nd)     path_release(nd)
748 #endif
749
750 #ifndef abs
751 static inline int abs(int x)
752 {
753         return (x < 0) ? -x : x;
754 }
755 #endif
756
757 #ifndef labs
758 static inline long labs(long x)
759 {
760         return (x < 0) ? -x : x;
761 }
762 #endif /* HAVE_REGISTER_SHRINKER */
763
764 #ifdef HAVE_INVALIDATE_INODE_PAGES
765 #define invalidate_mapping_pages(mapping,s,e) invalidate_inode_pages(mapping)
766 #endif
767
768 #ifdef HAVE_INODE_IPRIVATE
769 #define INODE_PRIVATE_DATA(inode)       ((inode)->i_private)
770 #else
771 #define INODE_PRIVATE_DATA(inode)       ((inode)->u.generic_ip)
772 #endif
773
774 #ifndef SLAB_DESTROY_BY_RCU
775 #define CFS_SLAB_DESTROY_BY_RCU 0
776 #else
777 #define CFS_SLAB_DESTROY_BY_RCU SLAB_DESTROY_BY_RCU
778 #endif
779
780 #ifdef HAVE_SB_HAS_QUOTA_ACTIVE
781 #define ll_sb_has_quota_active(sb, type) sb_has_quota_active(sb, type)
782 #else
783 #define ll_sb_has_quota_active(sb, type) sb_has_quota_enabled(sb, type)
784 #endif
785
786 #ifdef DQUOT_USAGE_ENABLED
787 #define ll_sb_any_quota_active(sb) sb_any_quota_loaded(sb)
788 #elif defined(HAVE_SB_ANY_QUOTA_ACTIVE)
789 #define ll_sb_any_quota_active(sb) sb_any_quota_active(sb)
790 #else
791 #define ll_sb_any_quota_active(sb) sb_any_quota_enabled(sb)
792 #endif
793
794 static inline int
795 ll_quota_on(struct super_block *sb, int off, int ver, char *name, int remount)
796 {
797         int rc;
798
799         if (sb->s_qcop->quota_on) {
800 #ifdef HAVE_QUOTA_ON_USE_PATH
801                 struct path path;
802
803                 rc = kern_path(name, LOOKUP_FOLLOW, &path);
804                 if (!rc)
805                         return rc;
806 #endif
807                 rc = sb->s_qcop->quota_on(sb, off, ver
808 #ifdef HAVE_QUOTA_ON_USE_PATH
809                                             , &path
810 #else
811                                             , name
812 #endif
813 #ifdef HAVE_QUOTA_ON_5ARGS
814                                             , remount
815 #endif
816                                            );
817 #ifdef HAVE_QUOTA_ON_USE_PATH
818                 path_put(&path);
819 #endif
820                 return rc;
821         }
822         else
823                 return -ENOSYS;
824 }
825
826 static inline int ll_quota_off(struct super_block *sb, int off, int remount)
827 {
828         if (sb->s_qcop->quota_off) {
829                 return sb->s_qcop->quota_off(sb, off
830 #ifdef HAVE_QUOTA_OFF_3ARGS
831                                              , remount
832 #endif
833                                             );
834         }
835         else
836                 return -ENOSYS;
837 }
838
839 #ifndef HAVE_BLK_QUEUE_LOG_BLK_SIZE /* added in 2.6.31 */
840 #define blk_queue_logical_block_size(q, sz) blk_queue_hardsect_size(q, sz)
841 #endif
842
843 #ifndef HAVE_VFS_DQ_OFF
844 # define ll_vfs_dq_init             DQUOT_INIT
845 # define ll_vfs_dq_drop             DQUOT_DROP
846 # define ll_vfs_dq_transfer         DQUOT_TRANSFER
847 # define ll_vfs_dq_off(sb, remount) DQUOT_OFF(sb)
848 #else
849 # define ll_vfs_dq_init             vfs_dq_init
850 # define ll_vfs_dq_drop             vfs_dq_drop
851 # define ll_vfs_dq_transfer         vfs_dq_transfer
852 # define ll_vfs_dq_off(sb, remount) vfs_dq_off(sb, remount)
853 #endif
854
855 #ifdef HAVE_BDI_INIT
856 #define ll_bdi_init(bdi)    bdi_init(bdi)
857 #define ll_bdi_destroy(bdi) bdi_destroy(bdi)
858 #else
859 #define ll_bdi_init(bdi)    0
860 #define ll_bdi_destroy(bdi) do { } while(0)
861 #endif
862
863 #ifdef HAVE_NEW_BACKING_DEV_INFO
864 # define ll_bdi_wb_cnt(bdi) ((bdi).wb_cnt)
865 #else
866 # define ll_bdi_wb_cnt(bdi) 1
867 #endif
868
869 #ifdef HAVE_BLK_QUEUE_MAX_SECTORS /* removed in rhel6 */
870 #define blk_queue_max_hw_sectors(q, sect) blk_queue_max_sectors(q, sect)
871 #endif
872
873 #ifndef HAVE_BLKDEV_GET_BY_DEV
874 # define blkdev_get_by_dev(dev, mode, holder) open_by_devnum(dev, mode)
875 #endif
876
877 #ifndef HAVE_REQUEST_QUEUE_LIMITS
878 #define queue_max_sectors(rq)             ((rq)->max_sectors)
879 #define queue_max_hw_sectors(rq)          ((rq)->max_hw_sectors)
880 #define queue_max_phys_segments(rq)       ((rq)->max_phys_segments)
881 #define queue_max_hw_segments(rq)         ((rq)->max_hw_segments)
882 #endif
883
884 #ifndef HAVE_BLK_QUEUE_MAX_SEGMENTS
885 #define blk_queue_max_segments(rq, seg)                      \
886         do { blk_queue_max_phys_segments(rq, seg);           \
887              blk_queue_max_hw_segments(rq, seg); } while (0)
888 #else
889 #define queue_max_phys_segments(rq)       queue_max_segments(rq)
890 #define queue_max_hw_segments(rq)         queue_max_segments(rq)
891 #endif
892
893
894 #ifndef HAVE_BI_HW_SEGMENTS
895 #define bio_hw_segments(q, bio) 0
896 #endif
897
898 #ifndef HAVE_PAGEVEC_LRU_ADD_FILE
899 #define pagevec_lru_add_file pagevec_lru_add
900 #endif
901
902 #ifdef HAVE_ADD_TO_PAGE_CACHE_LRU
903 #define ll_add_to_page_cache_lru(pg, mapping, off, gfp) \
904         add_to_page_cache_lru(pg, mapping, off, gfp)
905 #define ll_pagevec_init(pv, cold)       do {} while (0)
906 #define ll_pagevec_add(pv, pg)          (0)
907 #define ll_pagevec_lru_add_file(pv)     do {} while (0)
908 #else
909 #define ll_add_to_page_cache_lru(pg, mapping, off, gfp) \
910         add_to_page_cache(pg, mapping, off, gfp)
911 #define ll_pagevec_init(pv, cold)       pagevec_init(&lru_pvec, cold);
912 #define ll_pagevec_add(pv, pg)          pagevec_add(pv, pg)
913 #define ll_pagevec_lru_add_file(pv)     pagevec_lru_add_file(pv)
914 #endif
915
916 #if !defined(HAVE_NODE_TO_CPUMASK) && defined(HAVE_CPUMASK_OF_NODE)
917 #define node_to_cpumask(i)         (*(cpumask_of_node(i)))
918 #define HAVE_NODE_TO_CPUMASK
919 #endif
920
921 #ifndef QUOTA_OK
922 # define QUOTA_OK 0
923 #endif
924 #ifndef NO_QUOTA
925 # define NO_QUOTA (-EDQUOT)
926 #endif
927
928 #if !defined(_ASM_GENERIC_BITOPS_EXT2_NON_ATOMIC_H_) && !defined(ext2_set_bit)
929 # define ext2_set_bit             __test_and_set_bit_le
930 # define ext2_clear_bit           __test_and_clear_bit_le
931 # define ext2_test_bit            test_bit_le
932 # define ext2_find_first_zero_bit find_first_zero_bit_le
933 # define ext2_find_next_zero_bit  find_next_zero_bit_le
934 #endif
935
936 #endif /* __KERNEL__ */
937 #endif /* _COMPAT25_H */