Whamcloud - gitweb
b9118c7abc77f2d16d48e2985e7d983489b80234
[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, 2013, Intel Corporation.
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 #include <linux/fs_struct.h>
41 #include <linux/namei.h>
42 #include <libcfs/linux/portals_compat25.h>
43
44 #include <linux/lustre_patchless_compat.h>
45
46 #ifdef HAVE_FS_STRUCT_RWLOCK
47 # define LOCK_FS_STRUCT(fs)     write_lock(&(fs)->lock)
48 # define UNLOCK_FS_STRUCT(fs)   write_unlock(&(fs)->lock)
49 #else
50 # define LOCK_FS_STRUCT(fs)     spin_lock(&(fs)->lock)
51 # define UNLOCK_FS_STRUCT(fs)   spin_unlock(&(fs)->lock)
52 #endif
53
54 #ifdef HAVE_FS_STRUCT_USE_PATH
55 static inline void ll_set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
56                                  struct dentry *dentry)
57 {
58         struct path path;
59         struct path old_pwd;
60
61         path.mnt = mnt;
62         path.dentry = dentry;
63         LOCK_FS_STRUCT(fs);
64         old_pwd = fs->pwd;
65         path_get(&path);
66         fs->pwd = path;
67         UNLOCK_FS_STRUCT(fs);
68
69         if (old_pwd.dentry)
70                 path_put(&old_pwd);
71 }
72
73 #else
74
75 static inline void ll_set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
76                 struct dentry *dentry)
77 {
78         struct dentry *old_pwd;
79         struct vfsmount *old_pwdmnt;
80
81         LOCK_FS_STRUCT(fs);
82         old_pwd = fs->pwd;
83         old_pwdmnt = fs->pwdmnt;
84         fs->pwdmnt = mntget(mnt);
85         fs->pwd = dget(dentry);
86         UNLOCK_FS_STRUCT(fs);
87
88         if (old_pwd) {
89                 dput(old_pwd);
90                 mntput(old_pwdmnt);
91         }
92 }
93 #endif
94
95 /*
96  * set ATTR_BLOCKS to a high value to avoid any risk of collision with other
97  * ATTR_* attributes (see bug 13828)
98  */
99 #define ATTR_BLOCKS    (1 << 27)
100
101 #define current_ngroups current_cred()->group_info->ngroups
102 #define current_groups current_cred()->group_info->small_block
103
104 /*
105  * OBD need working random driver, thus all our
106  * initialization routines must be called after device
107  * driver initialization
108  */
109 #ifndef MODULE
110 #undef module_init
111 #define module_init(a)     late_initcall(a)
112 #endif
113
114 #define LTIME_S(time)                   (time.tv_sec)
115
116 #ifdef HAVE_GENERIC_PERMISSION_2ARGS
117 # define ll_generic_permission(inode, mask, flags, check_acl) \
118          generic_permission(inode, mask)
119 #elif defined HAVE_GENERIC_PERMISSION_4ARGS
120 # define ll_generic_permission(inode, mask, flags, check_acl) \
121          generic_permission(inode, mask, flags, check_acl)
122 #else
123 # define ll_generic_permission(inode, mask, flags, check_acl) \
124          generic_permission(inode, mask, check_acl)
125 #endif
126
127 #ifdef HAVE_BLKDEV_PUT_2ARGS
128 #define ll_blkdev_put(a, b) blkdev_put(a, b)
129 #else
130 #define ll_blkdev_put(a, b) blkdev_put(a)
131 #endif
132
133 #ifdef HAVE_DENTRY_OPEN_USE_PATH
134 #define ll_dentry_open(a,b,c)   dentry_open(a,b,c)
135 #else
136 /*
137  * dentry_open handles its own reference counting since Linux v3.6
138  * (commit 765927b2). Callers should free their own references.
139  *
140  * Prior versions expected the caller to increment the references.
141  * The references are retained on success and freed on error.
142  */
143 static inline struct file *ll_dentry_open(struct path *path, int flags,
144                                           const struct cred *cred)
145 {
146         mntget(path->mnt);
147         dget(path->dentry);
148         return dentry_open(path->dentry, path->mnt, flags, cred);
149 }
150 #endif
151
152 #ifdef HAVE_SECURITY_PLUG
153 #define ll_vfs_symlink(dir, dentry, mnt, path, mode) \
154                 vfs_symlink(dir, dentry, mnt, path, mode)
155 #else
156 #ifdef HAVE_4ARGS_VFS_SYMLINK
157 #define ll_vfs_symlink(dir, dentry, mnt, path, mode) \
158                 vfs_symlink(dir, dentry, path, mode)
159 #else
160 #define ll_vfs_symlink(dir, dentry, mnt, path, mode) \
161                        vfs_symlink(dir, dentry, path)
162 #endif
163
164 #endif
165
166 #if !defined(HAVE_FILE_LLSEEK_SIZE) || defined(HAVE_FILE_LLSEEK_SIZE_5ARGS)
167 #define ll_generic_file_llseek_size(file, offset, origin, maxbytes, eof) \
168                 generic_file_llseek_size(file, offset, origin, maxbytes, eof);
169 #else
170 #define ll_generic_file_llseek_size(file, offset, origin, maxbytes, eof) \
171                 generic_file_llseek_size(file, offset, origin, maxbytes);
172 #endif
173
174 #ifdef HAVE_INODE_DIO_WAIT
175 /* inode_dio_wait(i) use as-is for write lock */
176 # define inode_dio_write_done(i)        do {} while (0) /* for write unlock */
177 # define inode_dio_read(i)              atomic_inc(&(i)->i_dio_count)
178 /* inode_dio_done(i) use as-is for read unlock */
179 #else
180 # define inode_dio_wait(i)              down_write(&(i)->i_alloc_sem)
181 # define inode_dio_write_done(i)        up_write(&(i)->i_alloc_sem)
182 # define inode_dio_read(i)              down_read(&(i)->i_alloc_sem)
183 # define inode_dio_done(i)              up_read(&(i)->i_alloc_sem)
184 #endif
185
186 #ifdef HAVE_RW_TREE_LOCK
187 #define TREE_READ_LOCK_IRQ(mapping)     read_lock_irq(&(mapping)->tree_lock)
188 #define TREE_READ_UNLOCK_IRQ(mapping)   read_unlock_irq(&(mapping)->tree_lock)
189 #else
190 #define TREE_READ_LOCK_IRQ(mapping)     spin_lock_irq(&(mapping)->tree_lock)
191 #define TREE_READ_UNLOCK_IRQ(mapping)   spin_unlock_irq(&(mapping)->tree_lock)
192 #endif
193
194 #ifdef HAVE_UNREGISTER_BLKDEV_RETURN_INT
195 #define ll_unregister_blkdev(a,b)       unregister_blkdev((a),(b))
196 #else
197 static inline
198 int ll_unregister_blkdev(unsigned int dev, const char *name)
199 {
200         unregister_blkdev(dev, name);
201         return 0;
202 }
203 #endif
204
205 #ifndef FS_HAS_FIEMAP
206 #define FS_HAS_FIEMAP                   (0)
207 #endif
208
209 #ifndef HAVE_FS_RENAME_DOES_D_MOVE
210 #define FS_RENAME_DOES_D_MOVE           FS_ODD_RENAME
211 #endif
212
213 /* add a lustre compatible layer for crypto API */
214 #include <linux/crypto.h>
215 #define ll_crypto_hash          crypto_hash
216 #define ll_crypto_cipher        crypto_blkcipher
217 #define ll_crypto_alloc_hash(name, type, mask)  crypto_alloc_hash(name, type, mask)
218 #define ll_crypto_hash_setkey(tfm, key, keylen) crypto_hash_setkey(tfm, key, keylen)
219 #define ll_crypto_hash_init(desc)               crypto_hash_init(desc)
220 #define ll_crypto_hash_update(desc, sl, bytes)  crypto_hash_update(desc, sl, bytes)
221 #define ll_crypto_hash_final(desc, out)         crypto_hash_final(desc, out)
222 #define ll_crypto_blkcipher_setkey(tfm, key, keylen) \
223                 crypto_blkcipher_setkey(tfm, key, keylen)
224 #define ll_crypto_blkcipher_set_iv(tfm, src, len) \
225                 crypto_blkcipher_set_iv(tfm, src, len)
226 #define ll_crypto_blkcipher_get_iv(tfm, dst, len) \
227                 crypto_blkcipher_get_iv(tfm, dst, len)
228 #define ll_crypto_blkcipher_encrypt(desc, dst, src, bytes) \
229                 crypto_blkcipher_encrypt(desc, dst, src, bytes)
230 #define ll_crypto_blkcipher_decrypt(desc, dst, src, bytes) \
231                 crypto_blkcipher_decrypt(desc, dst, src, bytes)
232 #define ll_crypto_blkcipher_encrypt_iv(desc, dst, src, bytes) \
233                 crypto_blkcipher_encrypt_iv(desc, dst, src, bytes)
234 #define ll_crypto_blkcipher_decrypt_iv(desc, dst, src, bytes) \
235                 crypto_blkcipher_decrypt_iv(desc, dst, src, bytes)
236
237 static inline
238 struct ll_crypto_cipher *ll_crypto_alloc_blkcipher(const char *name,
239                                                    u32 type, u32 mask)
240 {
241         struct ll_crypto_cipher *rtn = crypto_alloc_blkcipher(name, type, mask);
242
243         return (rtn == NULL ? ERR_PTR(-ENOMEM) : rtn);
244 }
245
246 static inline int ll_crypto_hmac(struct ll_crypto_hash *tfm,
247                                  u8 *key, unsigned int *keylen,
248                                  struct scatterlist *sg,
249                                  unsigned int size, u8 *result)
250 {
251         struct hash_desc desc;
252         int              rv;
253         desc.tfm   = tfm;
254         desc.flags = 0;
255         rv = crypto_hash_setkey(desc.tfm, key, *keylen);
256         if (rv) {
257                 CERROR("failed to hash setkey: %d\n", rv);
258                 return rv;
259         }
260         return crypto_hash_digest(&desc, sg, size, result);
261 }
262 static inline
263 unsigned int ll_crypto_tfm_alg_max_keysize(struct crypto_blkcipher *tfm)
264 {
265         return crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher.max_keysize;
266 }
267 static inline
268 unsigned int ll_crypto_tfm_alg_min_keysize(struct crypto_blkcipher *tfm)
269 {
270         return crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher.min_keysize;
271 }
272
273 #define ll_crypto_hash_blocksize(tfm)       crypto_hash_blocksize(tfm)
274 #define ll_crypto_hash_digestsize(tfm)      crypto_hash_digestsize(tfm)
275 #define ll_crypto_blkcipher_ivsize(tfm)     crypto_blkcipher_ivsize(tfm)
276 #define ll_crypto_blkcipher_blocksize(tfm)  crypto_blkcipher_blocksize(tfm)
277 #define ll_crypto_free_hash(tfm)            crypto_free_hash(tfm)
278 #define ll_crypto_free_blkcipher(tfm)       crypto_free_blkcipher(tfm)
279
280 #ifdef HAVE_SECURITY_PLUG
281 #define ll_vfs_rmdir(dir,entry,mnt)             vfs_rmdir(dir,entry,mnt)
282 #define ll_vfs_mkdir(inode,dir,mnt,mode)        vfs_mkdir(inode,dir,mnt,mode)
283 #define ll_vfs_link(old,mnt,dir,new,mnt1)       vfs_link(old,mnt,dir,new,mnt1)
284 #define ll_vfs_unlink(inode,entry,mnt)          vfs_unlink(inode,entry,mnt)
285 #define ll_vfs_mknod(dir,entry,mnt,mode,dev)            \
286                 vfs_mknod(dir,entry,mnt,mode,dev)
287 #define ll_vfs_rename(old,old_dir,mnt,new,new_dir,mnt1) \
288                 vfs_rename(old,old_dir,mnt,new,new_dir,mnt1)
289 #else
290 #define ll_vfs_rmdir(dir,entry,mnt)             vfs_rmdir(dir,entry)
291 #define ll_vfs_mkdir(inode,dir,mnt,mode)        vfs_mkdir(inode,dir,mode)
292 #define ll_vfs_link(old,mnt,dir,new,mnt1)       vfs_link(old,dir,new)
293 #define ll_vfs_unlink(inode,entry,mnt)          vfs_unlink(inode,entry)
294 #define ll_vfs_mknod(dir,entry,mnt,mode,dev)    vfs_mknod(dir,entry,mode,dev)
295 #define ll_vfs_rename(old,old_dir,mnt,new,new_dir,mnt1) \
296                 vfs_rename(old,old_dir,new,new_dir)
297 #endif /* HAVE_SECURITY_PLUG */
298
299 #ifdef for_each_possible_cpu
300 #define cfs_for_each_possible_cpu(cpu) for_each_possible_cpu(cpu)
301 #elif defined(for_each_cpu)
302 #define cfs_for_each_possible_cpu(cpu) for_each_cpu(cpu)
303 #endif
304
305 #ifdef HAVE_BIO_ENDIO_2ARG
306 #define cfs_bio_io_error(a,b)   bio_io_error((a))
307 #define cfs_bio_endio(a,b,c)    bio_endio((a),(c))
308 #else
309 #define cfs_bio_io_error(a,b)   bio_io_error((a),(b))
310 #define cfs_bio_endio(a,b,c)    bio_endio((a),(b),(c))
311 #endif
312
313 #ifdef HAVE_FS_STRUCT_USE_PATH
314 #define cfs_fs_pwd(fs)       ((fs)->pwd.dentry)
315 #define cfs_fs_mnt(fs)       ((fs)->pwd.mnt)
316 #define cfs_path_put(nd)     path_put(&(nd)->path)
317 #else
318 #define cfs_fs_pwd(fs)       ((fs)->pwd)
319 #define cfs_fs_mnt(fs)       ((fs)->pwdmnt)
320 #define cfs_path_put(nd)     path_release(nd)
321 #endif
322
323 #ifndef HAVE_SIMPLE_SETATTR
324 #define simple_setattr(dentry, ops) inode_setattr((dentry)->d_inode, ops)
325 #endif
326
327 #ifndef SLAB_DESTROY_BY_RCU
328 #define SLAB_DESTROY_BY_RCU 0
329 #endif
330
331 static inline int
332 ll_quota_on(struct super_block *sb, int off, int ver, char *name, int remount)
333 {
334         int rc;
335
336         if (sb->s_qcop->quota_on) {
337 #ifdef HAVE_QUOTA_ON_USE_PATH
338                 struct path path;
339
340                 rc = kern_path(name, LOOKUP_FOLLOW, &path);
341                 if (!rc)
342                         return rc;
343 #endif
344                 rc = sb->s_qcop->quota_on(sb, off, ver
345 #ifdef HAVE_QUOTA_ON_USE_PATH
346                                             , &path
347 #else
348                                             , name
349 #endif
350 #ifdef HAVE_QUOTA_ON_5ARGS
351                                             , remount
352 #endif
353                                            );
354 #ifdef HAVE_QUOTA_ON_USE_PATH
355                 path_put(&path);
356 #endif
357                 return rc;
358         }
359         else
360                 return -ENOSYS;
361 }
362
363 static inline int ll_quota_off(struct super_block *sb, int off, int remount)
364 {
365         if (sb->s_qcop->quota_off) {
366                 return sb->s_qcop->quota_off(sb, off
367 #ifdef HAVE_QUOTA_OFF_3ARGS
368                                              , remount
369 #endif
370                                             );
371         }
372         else
373                 return -ENOSYS;
374 }
375
376 #ifndef HAVE_DQUOT_SUSPEND
377 # define ll_vfs_dq_init             vfs_dq_init
378 # define ll_vfs_dq_drop             vfs_dq_drop
379 # define ll_vfs_dq_transfer         vfs_dq_transfer
380 # define ll_vfs_dq_off(sb, remount) vfs_dq_off(sb, remount)
381 #else
382 # define ll_vfs_dq_init             dquot_initialize
383 # define ll_vfs_dq_drop             dquot_drop
384 # define ll_vfs_dq_transfer         dquot_transfer
385 # define ll_vfs_dq_off(sb, remount) dquot_suspend(sb, -1)
386 #endif
387
388 #ifndef HAVE_BLKDEV_GET_BY_DEV
389 # define blkdev_get_by_dev(dev, mode, holder) open_by_devnum(dev, mode)
390 #endif
391
392 #ifndef HAVE_BLK_QUEUE_MAX_SEGMENTS
393 #define blk_queue_max_segments(rq, seg)                      \
394         do { blk_queue_max_phys_segments(rq, seg);           \
395              blk_queue_max_hw_segments(rq, seg); } while (0)
396 #else
397 #define queue_max_phys_segments(rq)       queue_max_segments(rq)
398 #define queue_max_hw_segments(rq)         queue_max_segments(rq)
399 #endif
400
401 #ifdef HAVE_KMAP_ATOMIC_HAS_1ARG
402 #define ll_kmap_atomic(a, b)    kmap_atomic(a)
403 #define ll_kunmap_atomic(a, b)  kunmap_atomic(a)
404 #else
405 #define ll_kmap_atomic(a, b)    kmap_atomic(a, b)
406 #define ll_kunmap_atomic(a, b)  kunmap_atomic(a, b)
407 #endif
408
409 #ifndef HAVE_CLEAR_INODE
410 #define clear_inode(i)          end_writeback(i)
411 #endif
412
413 #ifdef HAVE_DENTRY_D_ALIAS_HLIST
414 #define ll_d_hlist_node hlist_node
415 #define ll_d_hlist_empty(list) hlist_empty(list)
416 #define ll_d_hlist_entry(ptr, type, name) hlist_entry(ptr.first, type, name)
417 #define ll_d_hlist_for_each(tmp, i_dentry) hlist_for_each(tmp, i_dentry)
418 #ifdef HAVE_HLIST_FOR_EACH_3ARG
419 #define ll_d_hlist_for_each_entry(dentry, p, i_dentry, alias) \
420         p = NULL; hlist_for_each_entry(dentry, i_dentry, alias)
421 #else
422 #define ll_d_hlist_for_each_entry(dentry, p, i_dentry, alias) \
423         hlist_for_each_entry(dentry, p, i_dentry, alias)
424 #endif
425 #define DECLARE_LL_D_HLIST_NODE_PTR(name) struct ll_d_hlist_node *name
426 #else
427 #define ll_d_hlist_node list_head
428 #define ll_d_hlist_empty(list) list_empty(list)
429 #define ll_d_hlist_entry(ptr, type, name) list_entry(ptr.next, type, name)
430 #define ll_d_hlist_for_each(tmp, i_dentry) list_for_each(tmp, i_dentry)
431 #define ll_d_hlist_for_each_entry(dentry, p, i_dentry, alias) \
432         list_for_each_entry(dentry, i_dentry, alias)
433 #define DECLARE_LL_D_HLIST_NODE_PTR(name) /* nothing */
434 #endif
435
436
437 #ifndef HAVE_BI_HW_SEGMENTS
438 #define bio_hw_segments(q, bio) 0
439 #endif
440
441 #ifndef QUOTA_OK
442 # define QUOTA_OK 0
443 #endif
444 #ifndef NO_QUOTA
445 # define NO_QUOTA (-EDQUOT)
446 #endif
447
448 #ifndef SEEK_DATA
449 #define SEEK_DATA      3       /* seek to the next data */
450 #endif
451 #ifndef SEEK_HOLE
452 #define SEEK_HOLE      4       /* seek to the next hole */
453 #endif
454
455 #ifndef FMODE_UNSIGNED_OFFSET
456 #define FMODE_UNSIGNED_OFFSET   ((__force fmode_t)0x2000)
457 #endif
458
459 #if !defined(_ASM_GENERIC_BITOPS_EXT2_NON_ATOMIC_H_) && !defined(ext2_set_bit)
460 # define ext2_set_bit             __test_and_set_bit_le
461 # define ext2_clear_bit           __test_and_clear_bit_le
462 # define ext2_test_bit            test_bit_le
463 # define ext2_find_first_zero_bit find_first_zero_bit_le
464 # define ext2_find_next_zero_bit  find_next_zero_bit_le
465 #endif
466
467 #ifdef ATTR_TIMES_SET
468 # define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)
469 #else
470 # define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET)
471 #endif
472
473 #ifndef HAVE_LM_XXX_LOCK_MANAGER_OPS
474 # define lm_compare_owner       fl_compare_owner
475 #endif
476
477 /*
478  * After 3.1, kernel's nameidata.intent.open.flags is different
479  * with lustre's lookup_intent.it_flags, as lustre's it_flags'
480  * lower bits equal to FMODE_xxx while kernel doesn't transliterate
481  * lower bits of nameidata.intent.open.flags to FMODE_xxx.
482  * */
483 #include <linux/version.h>
484 static inline int ll_namei_to_lookup_intent_flag(int flag)
485 {
486 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 1, 0)
487         flag = (flag & ~O_ACCMODE) | OPEN_FMODE(flag);
488 #endif
489         return flag;
490 }
491
492 #ifdef HAVE_VOID_MAKE_REQUEST_FN
493 # define ll_mrf_ret void
494 # define LL_MRF_RETURN(rc)
495 #else
496 # define ll_mrf_ret int
497 # define LL_MRF_RETURN(rc) RETURN(rc)
498 #endif
499
500 #include <linux/fs.h>
501 #ifndef HAVE_PROTECT_I_NLINK
502 static inline void set_nlink(struct inode *inode, unsigned int nlink)
503 {
504         inode->i_nlink = nlink;
505 }
506 #endif
507
508 #ifdef HAVE_INODEOPS_USE_UMODE_T
509 # define ll_umode_t     umode_t
510 #else
511 # define ll_umode_t     int
512 #endif
513
514 #include <linux/dcache.h>
515 #ifndef HAVE_D_MAKE_ROOT
516 static inline struct dentry *d_make_root(struct inode *root)
517 {
518         struct dentry *res = d_alloc_root(root);
519
520         if (res == NULL && root)
521                 iput(root);
522
523         return res;
524 }
525 #endif
526
527 #ifdef HAVE_DIRTY_INODE_HAS_FLAG
528 # define ll_dirty_inode(inode, flag)    (inode)->i_sb->s_op->dirty_inode((inode), flag)
529 #else
530 # define ll_dirty_inode(inode, flag)    (inode)->i_sb->s_op->dirty_inode((inode))
531 #endif
532
533 #endif /* _COMPAT25_H */