Whamcloud - gitweb
ce704ac7b8d4d4f1df8dc059ad4a58cc5828179b
[fs/lustre-release.git] / lustre / include / lustre_compat.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, 2014, 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 _LUSTRE_COMPAT_H
38 #define _LUSTRE_COMPAT_H
39
40 #include <linux/fs_struct.h>
41 #include <linux/namei.h>
42 #include <linux/pagemap.h>
43 #include <linux/bio.h>
44
45 #include <libcfs/libcfs.h>
46 #include <lustre_patchless_compat.h>
47
48 #ifdef HAVE_FS_STRUCT_RWLOCK
49 # define LOCK_FS_STRUCT(fs)     write_lock(&(fs)->lock)
50 # define UNLOCK_FS_STRUCT(fs)   write_unlock(&(fs)->lock)
51 #else
52 # define LOCK_FS_STRUCT(fs)     spin_lock(&(fs)->lock)
53 # define UNLOCK_FS_STRUCT(fs)   spin_unlock(&(fs)->lock)
54 #endif
55
56 static inline void ll_set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
57                                  struct dentry *dentry)
58 {
59         struct path path;
60         struct path old_pwd;
61
62         path.mnt = mnt;
63         path.dentry = dentry;
64         LOCK_FS_STRUCT(fs);
65         old_pwd = fs->pwd;
66         path_get(&path);
67         fs->pwd = path;
68         UNLOCK_FS_STRUCT(fs);
69
70         if (old_pwd.dentry)
71                 path_put(&old_pwd);
72 }
73
74 /*
75  * set ATTR_BLOCKS to a high value to avoid any risk of collision with other
76  * ATTR_* attributes (see bug 13828)
77  */
78 #define ATTR_BLOCKS    (1 << 27)
79
80 #define current_ngroups current_cred()->group_info->ngroups
81 #define current_groups current_cred()->group_info->small_block
82
83 /*
84  * OBD need working random driver, thus all our
85  * initialization routines must be called after device
86  * driver initialization
87  */
88 #ifndef MODULE
89 #undef module_init
90 #define module_init(a)     late_initcall(a)
91 #endif
92
93 #ifndef MODULE_ALIAS_FS
94 #define MODULE_ALIAS_FS(name)
95 #endif
96
97 #define LTIME_S(time)                   (time.tv_sec)
98
99 #ifdef HAVE_GENERIC_PERMISSION_2ARGS
100 # define ll_generic_permission(inode, mask, flags, check_acl) \
101          generic_permission(inode, mask)
102 #elif defined HAVE_GENERIC_PERMISSION_4ARGS
103 # define ll_generic_permission(inode, mask, flags, check_acl) \
104          generic_permission(inode, mask, flags, check_acl)
105 #else
106 # define ll_generic_permission(inode, mask, flags, check_acl) \
107          generic_permission(inode, mask, check_acl)
108 #endif
109
110 #ifdef HAVE_4ARGS_VFS_SYMLINK
111 #define ll_vfs_symlink(dir, dentry, mnt, path, mode) \
112                 vfs_symlink(dir, dentry, path, mode)
113 #else
114 #define ll_vfs_symlink(dir, dentry, mnt, path, mode) \
115                        vfs_symlink(dir, dentry, path)
116 #endif
117
118 #if !defined(HAVE_FILE_LLSEEK_SIZE) || defined(HAVE_FILE_LLSEEK_SIZE_5ARGS)
119 #define ll_generic_file_llseek_size(file, offset, origin, maxbytes, eof) \
120                 generic_file_llseek_size(file, offset, origin, maxbytes, eof);
121 #else
122 #define ll_generic_file_llseek_size(file, offset, origin, maxbytes, eof) \
123                 generic_file_llseek_size(file, offset, origin, maxbytes);
124 #endif
125
126 #ifdef HAVE_INODE_DIO_WAIT
127 /* inode_dio_wait(i) use as-is for write lock */
128 # define inode_dio_write_done(i)        do {} while (0) /* for write unlock */
129 #else
130 # define inode_dio_wait(i)              down_write(&(i)->i_alloc_sem)
131 # define inode_dio_write_done(i)        up_write(&(i)->i_alloc_sem)
132 #endif
133
134 #ifndef FS_HAS_FIEMAP
135 #define FS_HAS_FIEMAP                   (0)
136 #endif
137
138 /* add a lustre compatible layer for crypto API */
139 #include <linux/crypto.h>
140 static inline int ll_crypto_hmac(struct crypto_hash *tfm,
141                                  u8 *key, unsigned int *keylen,
142                                  struct scatterlist *sg,
143                                  unsigned int size, u8 *result)
144 {
145         struct hash_desc desc;
146         int              rv;
147         desc.tfm   = tfm;
148         desc.flags = 0;
149         rv = crypto_hash_setkey(desc.tfm, key, *keylen);
150         if (rv) {
151                 CERROR("failed to hash setkey: %d\n", rv);
152                 return rv;
153         }
154         return crypto_hash_digest(&desc, sg, size, result);
155 }
156
157 static inline
158 unsigned int ll_crypto_tfm_alg_min_keysize(struct crypto_blkcipher *tfm)
159 {
160         return crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher.min_keysize;
161 }
162
163 #ifndef HAVE_SIMPLE_SETATTR
164 #define simple_setattr(dentry, ops) inode_setattr((dentry)->d_inode, ops)
165 #endif
166
167 #ifndef SLAB_DESTROY_BY_RCU
168 #define SLAB_DESTROY_BY_RCU 0
169 #endif
170
171 #ifndef HAVE_DQUOT_SUSPEND
172 # define ll_vfs_dq_init             vfs_dq_init
173 # define ll_vfs_dq_drop             vfs_dq_drop
174 # define ll_vfs_dq_transfer         vfs_dq_transfer
175 # define ll_vfs_dq_off(sb, remount) vfs_dq_off(sb, remount)
176 #else
177 # define ll_vfs_dq_init             dquot_initialize
178 # define ll_vfs_dq_drop             dquot_drop
179 # define ll_vfs_dq_transfer         dquot_transfer
180 # define ll_vfs_dq_off(sb, remount) dquot_suspend(sb, -1)
181 #endif
182
183 #ifndef HAVE_BLKDEV_GET_BY_DEV
184 # define blkdev_get_by_dev(dev, mode, holder) open_by_devnum(dev, mode)
185 #endif
186
187 #ifdef HAVE_BVEC_ITER
188 #define bio_idx(bio)                    (bio->bi_iter.bi_idx)
189 #define bio_set_sector(bio, sector)     (bio->bi_iter.bi_sector = sector)
190 #define bvl_to_page(bvl)                (bvl.bv_page)
191 #else
192 #define bio_idx(bio)                    (bio->bi_idx)
193 #define bio_set_sector(bio, sector)     (bio->bi_sector = sector)
194 #define bio_sectors(bio)                ((bio)->bi_size >> 9)
195 #ifndef HAVE_BIO_END_SECTOR
196 #define bio_end_sector(bio)             (bio->bi_sector + bio_sectors(bio))
197 #endif
198 #define bvl_to_page(bvl)                (bvl->bv_page)
199 #endif
200
201 #ifndef HAVE_BLK_QUEUE_MAX_SEGMENTS
202 #define blk_queue_max_segments(rq, seg)                      \
203         do { blk_queue_max_phys_segments(rq, seg);           \
204              blk_queue_max_hw_segments(rq, seg); } while (0)
205 #else
206 #define queue_max_phys_segments(rq)       queue_max_segments(rq)
207 #define queue_max_hw_segments(rq)         queue_max_segments(rq)
208 #endif
209
210 #ifdef HAVE_KMAP_ATOMIC_HAS_1ARG
211 #define ll_kmap_atomic(a, b)    kmap_atomic(a)
212 #define ll_kunmap_atomic(a, b)  kunmap_atomic(a)
213 #else
214 #define ll_kmap_atomic(a, b)    kmap_atomic(a, b)
215 #define ll_kunmap_atomic(a, b)  kunmap_atomic(a, b)
216 #endif
217
218 #ifndef HAVE_CLEAR_INODE
219 #define clear_inode(i)          end_writeback(i)
220 #endif
221
222 #ifndef HAVE_DENTRY_D_CHILD
223 #define d_child                 d_u.d_child
224 #endif
225
226 #ifdef HAVE_DENTRY_D_U_D_ALIAS
227 #define d_alias                 d_u.d_alias
228 #endif
229
230 #ifndef DATA_FOR_LLITE_IS_LIST
231 #define ll_d_hlist_node hlist_node
232 #define ll_d_hlist_empty(list) hlist_empty(list)
233 #define ll_d_hlist_entry(ptr, type, name) hlist_entry(ptr.first, type, name)
234 #define ll_d_hlist_for_each(tmp, i_dentry) hlist_for_each(tmp, i_dentry)
235 # ifdef HAVE_HLIST_FOR_EACH_3ARG
236 # define ll_d_hlist_for_each_entry(dentry, p, i_dentry) \
237         p = NULL; hlist_for_each_entry(dentry, i_dentry, d_alias)
238 # else
239 # define ll_d_hlist_for_each_entry(dentry, p, i_dentry) \
240         hlist_for_each_entry(dentry, p, i_dentry, d_alias)
241 # endif
242 #define DECLARE_LL_D_HLIST_NODE_PTR(name) struct ll_d_hlist_node *name
243 #else
244 #define ll_d_hlist_node list_head
245 #define ll_d_hlist_empty(list) list_empty(list)
246 #define ll_d_hlist_entry(ptr, type, name) list_entry(ptr.next, type, name)
247 #define ll_d_hlist_for_each(tmp, i_dentry) list_for_each(tmp, i_dentry)
248 #define ll_d_hlist_for_each_entry(dentry, p, i_dentry) \
249         list_for_each_entry(dentry, i_dentry, d_alias)
250 #define DECLARE_LL_D_HLIST_NODE_PTR(name) /* nothing */
251 #endif /* !DATA_FOR_LLITE_IS_LIST */
252
253 #ifndef QUOTA_OK
254 # define QUOTA_OK 0
255 #endif
256 #ifndef NO_QUOTA
257 # define NO_QUOTA (-EDQUOT)
258 #endif
259
260 #ifndef SEEK_DATA
261 #define SEEK_DATA      3       /* seek to the next data */
262 #endif
263 #ifndef SEEK_HOLE
264 #define SEEK_HOLE      4       /* seek to the next hole */
265 #endif
266
267 #ifndef FMODE_UNSIGNED_OFFSET
268 #define FMODE_UNSIGNED_OFFSET   ((__force fmode_t)0x2000)
269 #endif
270
271 #if !defined(_ASM_GENERIC_BITOPS_EXT2_NON_ATOMIC_H_) && !defined(ext2_set_bit)
272 # define ext2_set_bit             __test_and_set_bit_le
273 # define ext2_clear_bit           __test_and_clear_bit_le
274 # define ext2_test_bit            test_bit_le
275 # define ext2_find_first_zero_bit find_first_zero_bit_le
276 # define ext2_find_next_zero_bit  find_next_zero_bit_le
277 #endif
278
279 #ifdef ATTR_TIMES_SET
280 # define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)
281 #else
282 # define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET)
283 #endif
284
285 #ifndef XATTR_NAME_POSIX_ACL_ACCESS
286 # define XATTR_NAME_POSIX_ACL_ACCESS POSIX_ACL_XATTR_ACCESS
287 #endif
288
289 #ifndef XATTR_NAME_POSIX_ACL_DEFAULT
290 # define XATTR_NAME_POSIX_ACL_DEFAULT POSIX_ACL_XATTR_DEFAULT
291 #endif
292
293 #ifndef HAVE_LM_XXX_LOCK_MANAGER_OPS
294 # define lm_compare_owner       fl_compare_owner
295 #endif
296
297 /*
298  * After 3.1, kernel's nameidata.intent.open.flags is different
299  * with lustre's lookup_intent.it_flags, as lustre's it_flags'
300  * lower bits equal to FMODE_xxx while kernel doesn't transliterate
301  * lower bits of nameidata.intent.open.flags to FMODE_xxx.
302  * */
303 #include <linux/version.h>
304 static inline int ll_namei_to_lookup_intent_flag(int flag)
305 {
306 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 1, 0)
307         flag = (flag & ~O_ACCMODE) | OPEN_FMODE(flag);
308 #endif
309         return flag;
310 }
311
312 #ifdef HAVE_QC_MAKE_REQUEST_FN
313 # define ll_mrf_ret blk_qc_t
314 # define LL_MRF_RETURN(rc) RETURN(BLK_QC_T_NONE)
315 #else
316 # ifdef HAVE_VOID_MAKE_REQUEST_FN
317 #  define ll_mrf_ret void
318 #  define LL_MRF_RETURN(rc)
319 # else
320 #  define ll_mrf_ret int
321 #  define LL_MRF_RETURN(rc) RETURN(rc)
322 # endif
323 #endif
324
325 #include <linux/fs.h>
326 #ifndef HAVE_PROTECT_I_NLINK
327 static inline void set_nlink(struct inode *inode, unsigned int nlink)
328 {
329         inode->i_nlink = nlink;
330 }
331 #endif
332
333 #ifdef HAVE_INODEOPS_USE_UMODE_T
334 # define ll_umode_t     umode_t
335 #else
336 # define ll_umode_t     int
337 #endif
338
339 #include <linux/dcache.h>
340 #ifndef HAVE_D_MAKE_ROOT
341 static inline struct dentry *d_make_root(struct inode *root)
342 {
343         struct dentry *res = d_alloc_root(root);
344
345         if (res == NULL && root)
346                 iput(root);
347
348         return res;
349 }
350 #endif
351
352 #ifdef HAVE_DIRTY_INODE_HAS_FLAG
353 # define ll_dirty_inode(inode, flag)    (inode)->i_sb->s_op->dirty_inode((inode), flag)
354 #else
355 # define ll_dirty_inode(inode, flag)    (inode)->i_sb->s_op->dirty_inode((inode))
356 #endif
357
358 #ifdef HAVE_FILE_F_INODE
359 # define set_file_inode(file, inode)    (file)->f_inode = inode
360 #else
361 # define set_file_inode(file, inode)
362 #endif
363
364 #ifndef HAVE_FILE_INODE
365 static inline struct inode *file_inode(const struct file *file)
366 {
367         return file->f_path.dentry->d_inode;
368 }
369 #endif
370
371 #ifdef HAVE_OLDSIZE_TRUNCATE_PAGECACHE
372 #define ll_truncate_pagecache(inode, size) truncate_pagecache(inode, 0, size)
373 #else
374 #define ll_truncate_pagecache(inode, size) truncate_pagecache(inode, size)
375 #endif
376
377 #ifdef HAVE_VFS_RENAME_5ARGS
378 #define ll_vfs_rename(a, b, c, d) vfs_rename(a, b, c, d, NULL)
379 #elif defined HAVE_VFS_RENAME_6ARGS
380 #define ll_vfs_rename(a, b, c, d) vfs_rename(a, b, c, d, NULL, 0)
381 #else
382 #define ll_vfs_rename(a, b, c, d) vfs_rename(a, b, c, d)
383 #endif
384
385 #ifdef HAVE_VFS_UNLINK_3ARGS
386 #define ll_vfs_unlink(a, b) vfs_unlink(a, b, NULL)
387 #else
388 #define ll_vfs_unlink(a, b) vfs_unlink(a, b)
389 #endif
390
391 #ifndef HAVE_INODE_LOCK
392 # define inode_lock(inode) mutex_lock(&(inode)->i_mutex)
393 # define inode_unlock(inode) mutex_unlock(&(inode)->i_mutex)
394 # define inode_trylock(inode) mutex_trylock(&(inode)->i_mutex)
395 #endif
396
397 #ifndef HAVE_RADIX_EXCEPTION_ENTRY
398 static inline int radix_tree_exceptional_entry(void *arg)
399 {
400         return 0;
401 }
402 #endif
403
404 #ifndef HAVE_TRUNCATE_INODE_PAGES_FINAL
405 static inline void truncate_inode_pages_final(struct address_space *map)
406 {
407         truncate_inode_pages(map, 0);
408                 /* Workaround for LU-118 */
409         if (map->nrpages) {
410                 spin_lock_irq(&map->tree_lock);
411                 spin_unlock_irq(&map->tree_lock);
412         }       /* Workaround end */
413 }
414 #endif
415
416 #ifndef SIZE_MAX
417 #define SIZE_MAX        (~(size_t)0)
418 #endif
419
420 #ifdef HAVE_SECURITY_IINITSEC_CALLBACK
421 # define ll_security_inode_init_security(inode, dir, name, value, len, \
422                                          initxattrs, dentry)           \
423          security_inode_init_security(inode, dir, &((dentry)->d_name), \
424                                       initxattrs, dentry)
425 #elif defined HAVE_SECURITY_IINITSEC_QSTR
426 # define ll_security_inode_init_security(inode, dir, name, value, len, \
427                                          initxattrs, dentry)           \
428          security_inode_init_security(inode, dir, &((dentry)->d_name), \
429                                       name, value, len)
430 #else /* !HAVE_SECURITY_IINITSEC_CALLBACK && !HAVE_SECURITY_IINITSEC_QSTR */
431 # define ll_security_inode_init_security(inode, dir, name, value, len, \
432                                          initxattrs, dentry)           \
433          security_inode_init_security(inode, dir, name, value, len)
434 #endif
435
436 #ifndef bio_for_each_segment_all /* since kernel version 3.9 */
437 #ifdef HAVE_BVEC_ITER
438 #define bio_for_each_segment_all(bv, bio, it) \
439         for (it = 0, bv = (bio)->bi_io_vec; it < (bio)->bi_vcnt; it++, bv++)
440 #else
441 #define bio_for_each_segment_all(bv, bio, it) bio_for_each_segment(bv, bio, it)
442 #endif
443 #endif
444
445 #endif /* _LUSTRE_COMPAT_H */