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