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