Whamcloud - gitweb
df7927c2afc64ae6050118eaee1d3641e8ca2cef
[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  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
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 <libcfs/linux/portals_compat25.h>
48
49 #include <linux/lustre_patchless_compat.h>
50
51 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
52 struct ll_iattr {
53         struct iattr    iattr;
54         unsigned int    ia_attr_flags;
55 };
56 #else
57 #define ll_iattr iattr
58 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14) */
59
60 #ifdef HAVE_FS_STRUCT_USE_PATH
61 static inline void ll_set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
62                                  struct dentry *dentry)
63 {
64         struct path path;
65         struct path old_pwd;
66
67         path.mnt = mnt;
68         path.dentry = dentry;
69         write_lock(&fs->lock);
70         old_pwd = fs->pwd;
71         path_get(&path);
72         fs->pwd = path;
73         write_unlock(&fs->lock);
74
75         if (old_pwd.dentry)
76                 path_put(&old_pwd);
77 }
78
79 #else
80
81 static inline void ll_set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
82                 struct dentry *dentry)
83 {
84         struct dentry *old_pwd;
85         struct vfsmount *old_pwdmnt;
86
87         write_lock(&fs->lock);
88         old_pwd = fs->pwd;
89         old_pwdmnt = fs->pwdmnt;
90         fs->pwdmnt = mntget(mnt);
91         fs->pwd = dget(dentry);
92         write_unlock(&fs->lock);
93
94         if (old_pwd) {
95                 dput(old_pwd);
96                 mntput(old_pwdmnt);
97         }
98 }
99 #endif
100
101 /*
102  * set ATTR_BLOCKS to a high value to avoid any risk of collision with other
103  * ATTR_* attributes (see bug 13828)
104  */
105 #define ATTR_BLOCKS    (1 << 27)
106
107 #if HAVE_INODE_I_MUTEX
108 #define UNLOCK_INODE_MUTEX(inode) do {mutex_unlock(&(inode)->i_mutex); } while(0)
109 #define LOCK_INODE_MUTEX(inode) do {mutex_lock(&(inode)->i_mutex); } while(0)
110 #define LOCK_INODE_MUTEX_PARENT(inode) \
111 do {mutex_lock_nested(&(inode)->i_mutex, I_MUTEX_PARENT); } while(0)
112 #define TRYLOCK_INODE_MUTEX(inode) mutex_trylock(&(inode)->i_mutex)
113 #else
114 #define UNLOCK_INODE_MUTEX(inode) do {up(&(inode)->i_sem); } while(0)
115 #define LOCK_INODE_MUTEX(inode) do {down(&(inode)->i_sem); } while(0)
116 #define TRYLOCK_INODE_MUTEX(inode) (!down_trylock(&(inode)->i_sem))
117 #define LOCK_INODE_MUTEX_PARENT(inode) LOCK_INODE_MUTEX(inode)
118 #endif /* HAVE_INODE_I_MUTEX */
119
120 #ifdef HAVE_SEQ_LOCK
121 #define LL_SEQ_LOCK(seq) mutex_lock(&(seq)->lock)
122 #define LL_SEQ_UNLOCK(seq) mutex_unlock(&(seq)->lock)
123 #else
124 #define LL_SEQ_LOCK(seq) down(&(seq)->sem)
125 #define LL_SEQ_UNLOCK(seq) up(&(seq)->sem)
126 #endif
127
128 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,15)
129 #define d_child d_u.d_child
130 #define d_rcu d_u.d_rcu
131 #endif
132
133 #ifdef HAVE_DQUOTOFF_MUTEX
134 #define UNLOCK_DQONOFF_MUTEX(dqopt) do {mutex_unlock(&(dqopt)->dqonoff_mutex); } while(0)
135 #define LOCK_DQONOFF_MUTEX(dqopt) do {mutex_lock(&(dqopt)->dqonoff_mutex); } while(0)
136 #else
137 #define UNLOCK_DQONOFF_MUTEX(dqopt) do {up(&(dqopt)->dqonoff_sem); } while(0)
138 #define LOCK_DQONOFF_MUTEX(dqopt) do {down(&(dqopt)->dqonoff_sem); } while(0)
139 #endif /* HAVE_DQUOTOFF_MUTEX */
140
141 #define current_ngroups current_cred()->group_info->ngroups
142 #define current_groups current_cred()->group_info->small_block
143
144 #ifndef page_private
145 #define page_private(page) ((page)->private)
146 #define set_page_private(page, v) ((page)->private = (v))
147 #endif
148
149 #ifndef HAVE_GFP_T
150 #define gfp_t int
151 #endif
152
153 #define lock_dentry(___dentry)          spin_lock(&(___dentry)->d_lock)
154 #define unlock_dentry(___dentry)        spin_unlock(&(___dentry)->d_lock)
155
156 #define ll_kernel_locked()      kernel_locked()
157
158 /*
159  * OBD need working random driver, thus all our
160  * initialization routines must be called after device
161  * driver initialization
162  */
163 #ifndef MODULE
164 #undef module_init
165 #define module_init(a)     late_initcall(a)
166 #endif
167
168 /* XXX our code should be using the 2.6 calls, not the other way around */
169 #ifdef HAVE_TRYLOCK_PAGE
170 #define TestSetPageLocked(page)         (!trylock_page(page))
171 #endif
172
173 #define Page_Uptodate(page)             PageUptodate(page)
174 #define ll_redirty_page(page)           set_page_dirty(page)
175
176 #define KDEVT_INIT(val)                 (val)
177
178 #define LTIME_S(time)                   (time.tv_sec)
179 #define ll_path_lookup                  path_lookup
180 #define ll_permission(inode,mask,nd)    permission(inode,mask,nd)
181
182 #define ll_pgcache_lock(mapping)          spin_lock(&mapping->page_lock)
183 #define ll_pgcache_unlock(mapping)        spin_unlock(&mapping->page_lock)
184 #define ll_call_writepage(inode, page)  \
185                                 (inode)->i_mapping->a_ops->writepage(page, NULL)
186 #define ll_invalidate_inode_pages(inode) \
187                                 invalidate_inode_pages((inode)->i_mapping)
188 #define ll_truncate_complete_page(page) \
189                                 truncate_complete_page(page->mapping, page)
190
191 #define ll_vfs_create(a,b,c,d)          vfs_create(a,b,c,d)
192 #define ll_dev_t                        dev_t
193 #define kdev_t                          dev_t
194 #define to_kdev_t(dev)                  (dev)
195 #define kdev_t_to_nr(dev)               (dev)
196 #define val_to_kdev(dev)                (dev)
197 #define ILOOKUP(sb, ino, test, data)    ilookup5(sb, ino, test, (void *)(data));
198
199 #ifdef HAVE_BLKDEV_PUT_2ARGS
200 #define ll_blkdev_put(a, b) blkdev_put(a, b)
201 #else
202 #define ll_blkdev_put(a, b) blkdev_put(a)
203 #endif
204
205 #ifdef HAVE_DENTRY_OPEN_4ARGS
206 #define ll_dentry_open(a, b, c, d) dentry_open(a, b, c, d)
207 #else
208 #define ll_dentry_open(a, b, c, d) dentry_open(a, b, c)
209 #endif
210
211 #include <linux/writeback.h>
212
213 static inline int cleanup_group_info(void)
214 {
215         struct group_info *ginfo;
216
217         ginfo = groups_alloc(0);
218         if (!ginfo)
219                 return -ENOMEM;
220
221         set_current_groups(ginfo);
222         put_group_info(ginfo);
223
224         return 0;
225 }
226
227 #define __set_page_ll_data(page, llap) \
228         do {       \
229                 page_cache_get(page); \
230                 SetPagePrivate(page); \
231                 set_page_private(page, (unsigned long)llap); \
232         } while (0)
233 #define __clear_page_ll_data(page) \
234         do {       \
235                 ClearPagePrivate(page); \
236                 set_page_private(page, 0); \
237                 page_cache_release(page); \
238         } while(0)
239
240 #define kiobuf bio
241
242 #include <linux/proc_fs.h>
243
244 #if !defined(HAVE_D_REHASH_COND) && defined(HAVE___D_REHASH)
245 #define d_rehash_cond(dentry, lock) __d_rehash(dentry, lock)
246 extern void __d_rehash(struct dentry *dentry, int lock);
247 #endif
248
249 #if !defined(HAVE_D_MOVE_LOCKED) && defined(HAVE___D_MOVE)
250 #define d_move_locked(dentry, target) __d_move(dentry, target)
251 extern void __d_move(struct dentry *dentry, struct dentry *target);
252 #endif
253
254 #ifdef HAVE_CAN_SLEEP_ARG
255 #define ll_flock_lock_file_wait(file, lock, can_sleep) \
256         flock_lock_file_wait(file, lock, can_sleep)
257 #else
258 #define ll_flock_lock_file_wait(file, lock, can_sleep) \
259         flock_lock_file_wait(file, lock)
260 #endif
261
262 #define CheckWriteback(page, cmd) \
263         ((!PageWriteback(page) && (cmd & OBD_BRW_READ)) || \
264          (PageWriteback(page) && (cmd & OBD_BRW_WRITE)))
265
266 #ifdef HAVE_PAGE_LIST
267 static inline int mapping_has_pages(struct address_space *mapping)
268 {
269         int rc = 1;
270
271         ll_pgcache_lock(mapping);
272         if (list_empty(&mapping->dirty_pages) &&
273             list_empty(&mapping->clean_pages) &&
274             list_empty(&mapping->locked_pages)) {
275                 rc = 0;
276         }
277         ll_pgcache_unlock(mapping);
278
279         return rc;
280 }
281 #else
282 static inline int mapping_has_pages(struct address_space *mapping)
283 {
284         return mapping->nrpages > 0;
285 }
286 #endif
287
288 #ifdef HAVE_KIOBUF_KIO_BLOCKS
289 #define KIOBUF_GET_BLOCKS(k) ((k)->kio_blocks)
290 #else
291 #define KIOBUF_GET_BLOCKS(k) ((k)->blocks)
292 #endif
293
294 #ifdef HAVE_SECURITY_PLUG
295 #define ll_vfs_symlink(dir, dentry, mnt, path, mode) \
296                 vfs_symlink(dir, dentry, mnt, path, mode)
297 #else
298 #ifdef HAVE_4ARGS_VFS_SYMLINK
299 #define ll_vfs_symlink(dir, dentry, mnt, path, mode) \
300                 vfs_symlink(dir, dentry, path, mode)
301 #else
302 #define ll_vfs_symlink(dir, dentry, mnt, path, mode) \
303                        vfs_symlink(dir, dentry, path)
304 #endif
305 #endif
306
307 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,7))
308 #define ll_set_dflags(dentry, flags) do { dentry->d_vfs_flags |= flags; } while(0)
309 #else
310 #define ll_set_dflags(dentry, flags) do { \
311                 spin_lock(&dentry->d_lock); \
312                 dentry->d_flags |= flags; \
313                 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 #ifndef HAVE_GRAB_CACHE_PAGE_NOWAIT_GFP
332 #define grab_cache_page_nowait_gfp(x, y, z) grab_cache_page_nowait((x), (y))
333 #endif
334
335 #include <linux/mpage.h>        /* for generic_writepages */
336 #ifndef HAVE_FILEMAP_FDATAWRITE_RANGE
337 #include <linux/backing-dev.h>  /* for mapping->backing_dev_info */
338 static inline int filemap_fdatawrite_range(struct address_space *mapping,
339                                            loff_t start, loff_t end)
340 {
341         int rc;
342         struct writeback_control wbc = {
343                 .sync_mode = WB_SYNC_ALL,
344                 .nr_to_write = (end - start + PAGE_SIZE - 1) >> PAGE_SHIFT,
345         };
346
347 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,18)
348         wbc.range_start = start;
349         wbc.range_end = end;
350 #else
351         wbc.start = start;
352         wbc.end = end;
353 #endif
354
355 #ifdef HAVE_MAPPING_CAP_WRITEBACK_DIRTY
356         if (!mapping_cap_writeback_dirty(mapping))
357                 rc = 0;
358 #else
359         if (mapping->backing_dev_info->memory_backed)
360                 rc = 0;
361 #endif
362         /* do_writepages() */
363         else if (mapping->a_ops->writepages)
364                 rc = mapping->a_ops->writepages(mapping, &wbc);
365         else
366                 rc = generic_writepages(mapping, &wbc);
367         return rc;
368 }
369 #else
370 int filemap_fdatawrite_range(struct address_space *mapping,
371                              loff_t start, loff_t end);
372 #endif /* HAVE_FILEMAP_FDATAWRITE_RANGE */
373
374 #ifdef HAVE_VFS_KERN_MOUNT
375 static inline struct vfsmount *
376 ll_kern_mount(const char *fstype, int flags, const char *name, void *data)
377 {
378         struct file_system_type *type = get_fs_type(fstype);
379         struct vfsmount *mnt;
380         if (!type)
381                 return ERR_PTR(-ENODEV);
382         mnt = vfs_kern_mount(type, flags, name, data);
383         module_put(type->owner);
384         return mnt;
385 }
386 #else
387 #define ll_kern_mount(fstype, flags, name, data) do_kern_mount((fstype), (flags), (name), (data))
388 #endif
389
390 #ifdef HAVE_STATFS_DENTRY_PARAM
391 #define ll_do_statfs(sb, sfs) (sb)->s_op->statfs((sb)->s_root, (sfs))
392 #else
393 #define ll_do_statfs(sb, sfs) (sb)->s_op->statfs((sb), (sfs))
394 #endif
395
396 #ifndef HAVE_SB_TIME_GRAN
397 #ifndef HAVE_S_TIME_GRAN
398 #error Need s_time_gran patch!
399 #endif
400 static inline u32 get_sb_time_gran(struct super_block *sb)
401 {
402         return sb->s_time_gran;
403 }
404 #endif
405
406 #ifdef HAVE_RW_TREE_LOCK
407 #define TREE_READ_LOCK_IRQ(mapping)     read_lock_irq(&(mapping)->tree_lock)
408 #define TREE_READ_UNLOCK_IRQ(mapping) read_unlock_irq(&(mapping)->tree_lock)
409 #else
410 #define TREE_READ_LOCK_IRQ(mapping) spin_lock_irq(&(mapping)->tree_lock)
411 #define TREE_READ_UNLOCK_IRQ(mapping) spin_unlock_irq(&(mapping)->tree_lock)
412 #endif
413
414 #ifdef HAVE_UNREGISTER_BLKDEV_RETURN_INT
415 #define ll_unregister_blkdev(a,b)       unregister_blkdev((a),(b))
416 #else
417 static inline
418 int ll_unregister_blkdev(unsigned int dev, const char *name)
419 {
420         unregister_blkdev(dev, name);
421         return 0;
422 }
423 #endif
424
425 #ifdef HAVE_INVALIDATE_BDEV_2ARG
426 #define ll_invalidate_bdev(a,b)         invalidate_bdev((a),(b))
427 #else
428 #define ll_invalidate_bdev(a,b)         invalidate_bdev((a))
429 #endif
430
431 #ifdef HAVE_INODE_BLKSIZE
432 #define ll_inode_blksize(a)     (a)->i_blksize
433 #else
434 #define ll_inode_blksize(a)     (1<<(a)->i_blkbits)
435 #endif
436
437 #ifdef HAVE_FS_RENAME_DOES_D_MOVE
438 #define LL_RENAME_DOES_D_MOVE   FS_RENAME_DOES_D_MOVE
439 #else
440 #define LL_RENAME_DOES_D_MOVE   FS_ODD_RENAME
441 #endif
442
443 #ifndef HAVE_D_OBTAIN_ALIAS
444 #define d_obtain_alias(inode) d_alloc_anon(inode)
445 #endif
446
447 /* add a lustre compatible layer for crypto API */
448 #include <linux/crypto.h>
449 #ifdef HAVE_ASYNC_BLOCK_CIPHER
450 #define ll_crypto_hash          crypto_hash
451 #define ll_crypto_cipher        crypto_blkcipher
452 #define ll_crypto_alloc_hash(name, type, mask)  crypto_alloc_hash(name, type, mask)
453 #define ll_crypto_hash_setkey(tfm, key, keylen) crypto_hash_setkey(tfm, key, keylen)
454 #define ll_crypto_hash_init(desc)               crypto_hash_init(desc)
455 #define ll_crypto_hash_update(desc, sl, bytes)  crypto_hash_update(desc, sl, bytes)
456 #define ll_crypto_hash_final(desc, out)         crypto_hash_final(desc, out)
457 #define ll_crypto_alloc_blkcipher(name, type, mask) \
458                 crypto_alloc_blkcipher(name ,type, mask)
459 #define ll_crypto_blkcipher_setkey(tfm, key, keylen) \
460                 crypto_blkcipher_setkey(tfm, key, keylen)
461 #define ll_crypto_blkcipher_set_iv(tfm, src, len) \
462                 crypto_blkcipher_set_iv(tfm, src, len)
463 #define ll_crypto_blkcipher_get_iv(tfm, dst, len) \
464                 crypto_blkcipher_get_iv(tfm, dst, len)
465 #define ll_crypto_blkcipher_encrypt(desc, dst, src, bytes) \
466                 crypto_blkcipher_encrypt(desc, dst, src, bytes)
467 #define ll_crypto_blkcipher_decrypt(desc, dst, src, bytes) \
468                 crypto_blkcipher_decrypt(desc, dst, src, bytes)
469 #define ll_crypto_blkcipher_encrypt_iv(desc, dst, src, bytes) \
470                 crypto_blkcipher_encrypt_iv(desc, dst, src, bytes)
471 #define ll_crypto_blkcipher_decrypt_iv(desc, dst, src, bytes) \
472                 crypto_blkcipher_decrypt_iv(desc, dst, src, bytes)
473
474 static inline int ll_crypto_hmac(struct ll_crypto_hash *tfm,
475                                  u8 *key, unsigned int *keylen,
476                                  struct scatterlist *sg,
477                                  unsigned int size, u8 *result)
478 {
479         struct hash_desc desc;
480         int              rv;
481         desc.tfm   = tfm;
482         desc.flags = 0;
483         rv = crypto_hash_setkey(desc.tfm, key, *keylen);
484         if (rv) {
485                 CERROR("failed to hash setkey: %d\n", rv);
486                 return rv;
487         }
488         return crypto_hash_digest(&desc, sg, size, result);
489 }
490 static inline
491 unsigned int ll_crypto_tfm_alg_max_keysize(struct crypto_blkcipher *tfm)
492 {
493         return crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher.max_keysize;
494 }
495 static inline
496 unsigned int ll_crypto_tfm_alg_min_keysize(struct crypto_blkcipher *tfm)
497 {
498         return crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher.min_keysize;
499 }
500
501 #define ll_crypto_hash_blocksize(tfm)       crypto_hash_blocksize(tfm)
502 #define ll_crypto_hash_digestsize(tfm)      crypto_hash_digestsize(tfm)
503 #define ll_crypto_blkcipher_ivsize(tfm)     crypto_blkcipher_ivsize(tfm)
504 #define ll_crypto_blkcipher_blocksize(tfm)  crypto_blkcipher_blocksize(tfm)
505 #define ll_crypto_free_hash(tfm)            crypto_free_hash(tfm)
506 #define ll_crypto_free_blkcipher(tfm)       crypto_free_blkcipher(tfm)
507 #else /* HAVE_ASYNC_BLOCK_CIPHER */
508 #include <linux/scatterlist.h>
509 #define ll_crypto_hash          crypto_tfm
510 #define ll_crypto_cipher        crypto_tfm
511 #ifndef HAVE_STRUCT_HASH_DESC
512 struct hash_desc {
513         struct ll_crypto_hash *tfm;
514         u32                    flags;
515 };
516 #endif
517 #ifndef HAVE_STRUCT_BLKCIPHER_DESC
518 struct blkcipher_desc {
519         struct ll_crypto_cipher *tfm;
520         void                    *info;
521         u32                      flags;
522 };
523 #endif
524 #define ll_crypto_blkcipher_setkey(tfm, key, keylen) \
525         crypto_cipher_setkey(tfm, key, keylen)
526 #define ll_crypto_blkcipher_set_iv(tfm, src, len) \
527         crypto_cipher_set_iv(tfm, src, len)
528 #define ll_crypto_blkcipher_get_iv(tfm, dst, len) \
529         crypto_cipher_get_iv(tfm, dst, len)
530 #define ll_crypto_blkcipher_encrypt(desc, dst, src, bytes) \
531         crypto_cipher_encrypt((desc)->tfm, dst, src, bytes)
532 #define ll_crypto_blkcipher_decrypt(desc, dst, src, bytes) \
533         crypto_cipher_decrypt((desc)->tfm, dst, src, bytes)
534 #define ll_crypto_blkcipher_decrypt_iv(desc, dst, src, bytes) \
535         crypto_cipher_decrypt_iv((desc)->tfm, dst, src, bytes, (desc)->info)
536 #define ll_crypto_blkcipher_encrypt_iv(desc, dst, src, bytes) \
537         crypto_cipher_encrypt_iv((desc)->tfm, dst, src, bytes, (desc)->info)
538
539 static inline
540 struct ll_crypto_cipher *ll_crypto_alloc_blkcipher(const char * algname,
541                                                    u32 type, u32 mask)
542 {
543         char        buf[CRYPTO_MAX_ALG_NAME + 1];
544         const char *pan = algname;
545         u32         flag = 0;
546
547         if (strncmp("cbc(", algname, 4) == 0)
548                 flag |= CRYPTO_TFM_MODE_CBC;
549         else if (strncmp("ecb(", algname, 4) == 0)
550                 flag |= CRYPTO_TFM_MODE_ECB;
551         if (flag) {
552                 char *vp = strnchr(algname, CRYPTO_MAX_ALG_NAME, ')');
553                 if (vp) {
554                         memcpy(buf, algname + 4, vp - algname - 4);
555                         buf[vp - algname - 4] = '\0';
556                         pan = buf;
557                 } else {
558                         flag = 0;
559                 }
560         }
561         return crypto_alloc_tfm(pan, flag);
562 }
563
564 static inline
565 struct ll_crypto_hash *ll_crypto_alloc_hash(const char *alg, u32 type, u32 mask)
566 {
567         char        buf[CRYPTO_MAX_ALG_NAME + 1];
568         const char *pan = alg;
569
570         if (strncmp("hmac(", alg, 5) == 0) {
571                 char *vp = strnchr(alg, CRYPTO_MAX_ALG_NAME, ')');
572                 if (vp) {
573                         memcpy(buf, alg+ 5, vp - alg- 5);
574                         buf[vp - alg - 5] = 0x00;
575                         pan = buf;
576                 }
577         }
578         return crypto_alloc_tfm(pan, 0);
579 }
580 static inline int ll_crypto_hash_init(struct hash_desc *desc)
581 {
582        crypto_digest_init(desc->tfm); return 0;
583 }
584 static inline int ll_crypto_hash_update(struct hash_desc *desc,
585                                         struct scatterlist *sg,
586                                         unsigned int nbytes)
587 {
588         struct scatterlist *sl = sg;
589         unsigned int        count;
590                 /*
591                  * This way is very weakness. We must ensure that
592                  * the sum of sg[0..i]->length isn't greater than nbytes.
593                  * In the upstream kernel the crypto_hash_update() also
594                  * via the nbytes computed the count of sg[...].
595                  * The old style is more safely. but it gone.
596                  */
597         for (count = 0; nbytes > 0; count ++, sl ++) {
598                 nbytes -= sl->length;
599         }
600         crypto_digest_update(desc->tfm, sg, count); return 0;
601 }
602 static inline int ll_crypto_hash_final(struct hash_desc *desc, u8 *out)
603 {
604         crypto_digest_final(desc->tfm, out); return 0;
605 }
606 static inline int ll_crypto_hmac(struct crypto_tfm *tfm,
607                                  u8 *key, unsigned int *keylen,
608                                  struct scatterlist *sg,
609                                  unsigned int nbytes,
610                                  u8 *out)
611 {
612         struct scatterlist *sl = sg;
613         int                 count;
614         for (count = 0; nbytes > 0; count ++, sl ++) {
615                 nbytes -= sl->length;
616         }
617         crypto_hmac(tfm, key, keylen, sg, count, out);
618         return 0;
619 }
620
621 #define ll_crypto_hash_setkey(tfm, key, keylen) crypto_digest_setkey(tfm, key, keylen)
622 #define ll_crypto_blkcipher_blocksize(tfm)      crypto_tfm_alg_blocksize(tfm)
623 #define ll_crypto_blkcipher_ivsize(tfm) crypto_tfm_alg_ivsize(tfm)
624 #define ll_crypto_hash_digestsize(tfm)  crypto_tfm_alg_digestsize(tfm)
625 #define ll_crypto_hash_blocksize(tfm)   crypto_tfm_alg_blocksize(tfm)
626 #define ll_crypto_free_hash(tfm)        crypto_free_tfm(tfm)
627 #define ll_crypto_free_blkcipher(tfm)   crypto_free_tfm(tfm)
628 #define ll_crypto_tfm_alg_min_keysize   crypto_tfm_alg_min_keysize
629 #define ll_crypto_tfm_alg_max_keysize   crypto_tfm_alg_max_keysize
630 #endif /* HAVE_ASYNC_BLOCK_CIPHER */
631
632 #ifndef HAVE_SYNCHRONIZE_RCU
633 #define synchronize_rcu() synchronize_kernel()
634 #endif
635
636 #ifdef HAVE_FILE_REMOVE_SUID
637 # define ll_remove_suid(file, mnt)       file_remove_suid(file)
638 #else
639 # ifdef HAVE_SECURITY_PLUG
640 #  define ll_remove_suid(file,mnt)      remove_suid(file->f_dentry,mnt)
641 # else
642 #  define ll_remove_suid(file,mnt)      remove_suid(file->f_dentry)
643 # endif
644 #endif
645
646 #ifdef HAVE_SECURITY_PLUG
647 #define ll_vfs_rmdir(dir,entry,mnt)             vfs_rmdir(dir,entry,mnt)
648 #define ll_vfs_mkdir(inode,dir,mnt,mode)        vfs_mkdir(inode,dir,mnt,mode)
649 #define ll_vfs_link(old,mnt,dir,new,mnt1)       vfs_link(old,mnt,dir,new,mnt1)
650 #define ll_vfs_unlink(inode,entry,mnt)          vfs_unlink(inode,entry,mnt)
651 #define ll_vfs_mknod(dir,entry,mnt,mode,dev)            \
652                 vfs_mknod(dir,entry,mnt,mode,dev)
653 #define ll_security_inode_unlink(dir,entry,mnt)         \
654                 security_inode_unlink(dir,entry,mnt)
655 #define ll_vfs_rename(old,old_dir,mnt,new,new_dir,mnt1) \
656                 vfs_rename(old,old_dir,mnt,new,new_dir,mnt1)
657 #else
658 #define ll_vfs_rmdir(dir,entry,mnt)             vfs_rmdir(dir,entry)
659 #define ll_vfs_mkdir(inode,dir,mnt,mode)        vfs_mkdir(inode,dir,mode)
660 #define ll_vfs_link(old,mnt,dir,new,mnt1)       vfs_link(old,dir,new)
661 #define ll_vfs_unlink(inode,entry,mnt)          vfs_unlink(inode,entry)
662 #define ll_vfs_mknod(dir,entry,mnt,mode,dev)    vfs_mknod(dir,entry,mode,dev)
663 #define ll_security_inode_unlink(dir,entry,mnt) security_inode_unlink(dir,entry)
664 #define ll_vfs_rename(old,old_dir,mnt,new,new_dir,mnt1) \
665                 vfs_rename(old,old_dir,new,new_dir)
666 #endif /* HAVE_SECURITY_PLUG */
667
668 #ifndef for_each_possible_cpu
669 #define for_each_possible_cpu(i) for_each_cpu(i)
670 #endif
671
672 #ifndef cpu_to_node
673 #define cpu_to_node(cpu)         0
674 #endif
675
676 #ifdef HAVE_REGISTER_SHRINKER
677 typedef int (*shrinker_t)(int nr_to_scan, gfp_t gfp_mask);
678
679 static inline
680 struct shrinker *set_shrinker(int seek, shrinker_t func)
681 {
682         struct shrinker *s;
683
684         s = kmalloc(sizeof(*s), GFP_KERNEL);
685         if (s == NULL)
686                 return (NULL);
687
688         s->shrink = func;
689         s->seeks = seek;
690
691         register_shrinker(s);
692
693         return s;
694 }
695
696 static inline
697 void remove_shrinker(struct shrinker *shrinker) 
698 {
699         if (shrinker == NULL)
700                 return;
701
702         unregister_shrinker(shrinker);
703         kfree(shrinker);
704 }
705 #endif
706
707 #ifdef HAVE_BIO_ENDIO_2ARG
708 #define cfs_bio_io_error(a,b)   bio_io_error((a))
709 #define cfs_bio_endio(a,b,c)    bio_endio((a),(c))
710 #else
711 #define cfs_bio_io_error(a,b)   bio_io_error((a),(b))
712 #define cfs_bio_endio(a,b,c)    bio_endio((a),(b),(c))
713 #endif
714
715 #ifdef HAVE_FS_STRUCT_USE_PATH
716 #define cfs_fs_pwd(fs)       ((fs)->pwd.dentry)
717 #define cfs_fs_mnt(fs)       ((fs)->pwd.mnt)
718 #define cfs_path_put(nd)     path_put(&(nd)->path)
719 #else
720 #define cfs_fs_pwd(fs)       ((fs)->pwd)
721 #define cfs_fs_mnt(fs)       ((fs)->pwdmnt)
722 #define cfs_path_put(nd)     path_release(nd)
723 #endif
724
725 #ifndef abs
726 static inline int abs(int x)
727 {
728         return (x < 0) ? -x : x;
729 }
730 #endif
731
732 #ifndef labs
733 static inline long labs(long x)
734 {
735         return (x < 0) ? -x : x;
736 }
737 #endif /* HAVE_REGISTER_SHRINKER */
738
739 /* Using kernel fls(). Userspace will use one defined in user-bitops.h. */
740 #ifndef __fls
741 #define __fls fls
742 #endif
743
744 #ifdef HAVE_INVALIDATE_INODE_PAGES
745 #define invalidate_mapping_pages(mapping,s,e) invalidate_inode_pages(mapping)
746 #endif
747
748 #ifdef HAVE_INODE_IPRIVATE
749 #define INODE_PRIVATE_DATA(inode)       ((inode)->i_private)
750 #else
751 #define INODE_PRIVATE_DATA(inode)       ((inode)->u.generic_ip)
752 #endif
753
754 #ifndef SLAB_DESTROY_BY_RCU
755 #define SLAB_DESTROY_BY_RCU 0
756 #endif
757
758 #ifdef HAVE_SB_HAS_QUOTA_ACTIVE
759 #define ll_sb_has_quota_active(sb, type) sb_has_quota_active(sb, type)
760 #else
761 #define ll_sb_has_quota_active(sb, type) sb_has_quota_enabled(sb, type)
762 #endif
763
764 #ifdef HAVE_SB_ANY_QUOTA_ACTIVE
765 #define ll_sb_any_quota_active(sb) sb_any_quota_active(sb)
766 #else
767 #define ll_sb_any_quota_active(sb) sb_any_quota_enabled(sb)
768 #endif
769
770 static inline int
771 ll_quota_on(struct super_block *sb, int off, int ver, char *name, int remount)
772 {
773         if (sb->s_qcop->quota_on) {
774                 return sb->s_qcop->quota_on(sb, off, ver, name
775 #ifdef HAVE_QUOTA_ON_5ARGS
776                                             , remount
777 #endif
778                                            );
779         }
780         else
781                 return -ENOSYS;
782 }
783
784 static inline int ll_quota_off(struct super_block *sb, int off, int remount)
785 {
786         if (sb->s_qcop->quota_off) {
787                 return sb->s_qcop->quota_off(sb, off
788 #ifdef HAVE_QUOTA_OFF_3ARGS
789                                              , remount
790 #endif
791                                             );
792         }
793         else
794                 return -ENOSYS;
795 }
796
797 #endif /* __KERNEL__ */
798 #endif /* _COMPAT25_H */