Whamcloud - gitweb
ccd09605ba1887becc16668228abc764973fa007
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  */
31
32 #ifndef _LUSTRE_COMPAT_H
33 #define _LUSTRE_COMPAT_H
34
35 #include <linux/aio.h>
36 #include <linux/fs.h>
37 #include <linux/namei.h>
38 #include <linux/pagemap.h>
39 #include <linux/posix_acl_xattr.h>
40 #include <linux/bio.h>
41 #include <linux/xattr.h>
42 #include <linux/workqueue.h>
43 #include <linux/blkdev.h>
44 #include <linux/slab.h>
45 #include <linux/security.h>
46
47 #include <libcfs/linux/linux-fs.h>
48 #include <obd_support.h>
49
50 #ifdef HAVE_4ARGS_VFS_SYMLINK
51 #define ll_vfs_symlink(dir, dentry, mnt, path, mode) \
52                 vfs_symlink(dir, dentry, path, mode)
53 #else
54 #define ll_vfs_symlink(dir, dentry, mnt, path, mode) \
55                        vfs_symlink(dir, dentry, path)
56 #endif
57
58 #ifdef HAVE_BVEC_ITER
59 #define bio_idx(bio)                    (bio->bi_iter.bi_idx)
60 #define bio_set_sector(bio, sector)     (bio->bi_iter.bi_sector = sector)
61 #define bvl_to_page(bvl)                (bvl->bv_page)
62 #else
63 #define bio_idx(bio)                    (bio->bi_idx)
64 #define bio_set_sector(bio, sector)     (bio->bi_sector = sector)
65 #define bio_sectors(bio)                ((bio)->bi_size >> 9)
66 #define bvl_to_page(bvl)                (bvl->bv_page)
67 #endif
68
69 #ifdef HAVE_BVEC_ITER
70 #define bio_start_sector(bio) (bio->bi_iter.bi_sector)
71 #else
72 #define bio_start_sector(bio) (bio->bi_sector)
73 #endif
74
75 #ifndef HAVE_DENTRY_D_CHILD
76 #define d_child                 d_u.d_child
77 #endif
78
79 #ifdef HAVE_DENTRY_D_U_D_ALIAS
80 #define d_alias                 d_u.d_alias
81 #endif
82
83 #ifndef HAVE_D_IN_LOOKUP
84 static inline int d_in_lookup(struct dentry *dentry)
85 {
86         return false;
87 }
88 #endif
89
90 #ifndef HAVE_VM_FAULT_T
91 #define vm_fault_t int
92 #endif
93
94 #ifndef HAVE_FOP_ITERATE_SHARED
95 #define iterate_shared iterate
96 #endif
97
98 #ifdef HAVE_OLDSIZE_TRUNCATE_PAGECACHE
99 #define ll_truncate_pagecache(inode, size) truncate_pagecache(inode, 0, size)
100 #else
101 #define ll_truncate_pagecache(inode, size) truncate_pagecache(inode, size)
102 #endif
103
104 #ifdef HAVE_VFS_RENAME_5ARGS
105 #define ll_vfs_rename(a, b, c, d) vfs_rename(a, b, c, d, NULL)
106 #elif defined HAVE_VFS_RENAME_6ARGS
107 #define ll_vfs_rename(a, b, c, d) vfs_rename(a, b, c, d, NULL, 0)
108 #else
109 #define ll_vfs_rename(a, b, c, d) vfs_rename(a, b, c, d)
110 #endif
111
112 #ifdef HAVE_USER_NAMESPACE_ARG
113 #define vfs_unlink(ns, dir, de) vfs_unlink(ns, dir, de, NULL)
114 #elif defined HAVE_VFS_UNLINK_3ARGS
115 #define vfs_unlink(ns, dir, de) vfs_unlink(dir, de, NULL)
116 #else
117 #define vfs_unlink(ns, dir, de) vfs_unlink(dir, de)
118 #endif
119
120 static inline int ll_vfs_getattr(struct path *path, struct kstat *st,
121                                  u32 request_mask, unsigned int flags)
122 {
123         int rc;
124
125 #if defined(HAVE_USER_NAMESPACE_ARG) || defined(HAVE_INODEOPS_ENHANCED_GETATTR)
126         rc = vfs_getattr(path, st, request_mask, flags);
127 #else
128         rc = vfs_getattr(path, st);
129 #endif
130         return rc;
131 }
132
133 #ifndef HAVE_D_IS_POSITIVE
134 static inline bool d_is_positive(const struct dentry *dentry)
135 {
136         return dentry->d_inode != NULL;
137 }
138 #endif
139
140 #ifndef HAVE_INODE_LOCK
141 # define inode_lock(inode) mutex_lock(&(inode)->i_mutex)
142 # define inode_unlock(inode) mutex_unlock(&(inode)->i_mutex)
143 # define inode_trylock(inode) mutex_trylock(&(inode)->i_mutex)
144 #endif
145
146 /* Old kernels lacked both Xarray support and the page cache
147  * using Xarrays. Our back ported Xarray support introduces
148  * the real xa_is_value() but we need a wrapper as well for
149  * the page cache interaction. Lets keep xa_is_value() separate
150  * in old kernels for Xarray support and page cache handling.
151  */
152 #ifndef HAVE_XARRAY_SUPPORT
153 static inline bool ll_xa_is_value(void *entry)
154 {
155         return radix_tree_exceptional_entry(entry);
156 }
157 #else
158 #define ll_xa_is_value  xa_is_value
159 #endif
160
161 #ifndef HAVE_TRUNCATE_INODE_PAGES_FINAL
162 static inline void truncate_inode_pages_final(struct address_space *map)
163 {
164         truncate_inode_pages(map, 0);
165 }
166 #endif
167
168 #ifndef HAVE_PTR_ERR_OR_ZERO
169 static inline int __must_check PTR_ERR_OR_ZERO(__force const void *ptr)
170 {
171         if (IS_ERR(ptr))
172                 return PTR_ERR(ptr);
173         else
174                 return 0;
175 }
176 #endif
177
178 #ifdef HAVE_PID_NS_FOR_CHILDREN
179 # define ll_task_pid_ns(task) \
180          ((task)->nsproxy ? ((task)->nsproxy->pid_ns_for_children) : NULL)
181 #else
182 # define ll_task_pid_ns(task) \
183          ((task)->nsproxy ? ((task)->nsproxy->pid_ns) : NULL)
184 #endif
185
186 #ifdef HAVE_FULL_NAME_HASH_3ARGS
187 # define ll_full_name_hash(salt, name, len) full_name_hash(salt, name, len)
188 #else
189 # define ll_full_name_hash(salt, name, len) full_name_hash(name, len)
190 #endif
191
192 #ifdef HAVE_STRUCT_POSIX_ACL_XATTR
193 # define posix_acl_xattr_header struct posix_acl_xattr_header
194 # define posix_acl_xattr_entry  struct posix_acl_xattr_entry
195 # define GET_POSIX_ACL_XATTR_ENTRY(head) ((void *)((head) + 1))
196 #else
197 # define GET_POSIX_ACL_XATTR_ENTRY(head) ((head)->a_entries)
198 #endif
199
200 #ifdef HAVE_IOP_XATTR
201 #define ll_setxattr     generic_setxattr
202 #define ll_getxattr     generic_getxattr
203 #define ll_removexattr  generic_removexattr
204 #endif /* HAVE_IOP_XATTR */
205
206 #ifndef HAVE_POSIX_ACL_VALID_USER_NS
207 #define posix_acl_valid(a,b)            posix_acl_valid(b)
208 #endif
209
210 #ifdef HAVE_IOP_SET_ACL
211 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
212 #if !defined(HAVE_USER_NAMESPACE_ARG) && !defined(HAVE_POSIX_ACL_UPDATE_MODE)
213 static inline int posix_acl_update_mode(struct inode *inode, umode_t *mode_p,
214                           struct posix_acl **acl)
215 {
216         umode_t mode = inode->i_mode;
217         int error;
218
219         error = posix_acl_equiv_mode(*acl, &mode);
220         if (error < 0)
221                 return error;
222         if (error == 0)
223                 *acl = NULL;
224         if (!in_group_p(inode->i_gid) &&
225             !capable_wrt_inode_uidgid(inode, CAP_FSETID))
226                 mode &= ~S_ISGID;
227         *mode_p = mode;
228         return 0;
229 }
230 #endif /* HAVE_POSIX_ACL_UPDATE_MODE */
231 #endif
232 #endif
233
234 #ifndef HAVE_IOV_ITER_TRUNCATE
235 static inline void iov_iter_truncate(struct iov_iter *i, u64 count)
236 {
237         if (i->count > count)
238                 i->count = count;
239 }
240 #endif
241
242 /*
243  * mount MS_* flags split from superblock SB_* flags
244  * if the SB_* flags are not available use the MS_* flags
245  */
246 #if !defined(SB_RDONLY) && defined(MS_RDONLY)
247 # define SB_RDONLY MS_RDONLY
248 #endif
249 #if !defined(SB_ACTIVE) && defined(MS_ACTIVE)
250 # define SB_ACTIVE MS_ACTIVE
251 #endif
252 #if !defined(SB_NOSEC) && defined(MS_NOSEC)
253 # define SB_NOSEC MS_NOSEC
254 #endif
255 #if !defined(SB_POSIXACL) && defined(MS_POSIXACL)
256 # define SB_POSIXACL MS_POSIXACL
257 #endif
258 #if !defined(SB_NODIRATIME) && defined(MS_NODIRATIME)
259 # define SB_NODIRATIME MS_NODIRATIME
260 #endif
261
262 #ifndef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
263 static inline void iov_iter_reexpand(struct iov_iter *i, size_t count)
264 {
265         i->count = count;
266 }
267
268 static inline struct iovec iov_iter_iovec(const struct iov_iter *iter)
269 {
270         return (struct iovec) {
271                 .iov_base = iter->iov->iov_base + iter->iov_offset,
272                 .iov_len = min(iter->count,
273                                iter->iov->iov_len - iter->iov_offset),
274         };
275 }
276
277 #define iov_for_each(iov, iter, start)                                  \
278         for (iter = (start);                                            \
279              (iter).count && ((iov = iov_iter_iovec(&(iter))), 1);      \
280              iov_iter_advance(&(iter), (iov).iov_len))
281
282 static inline ssize_t
283 generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
284 {
285         struct iovec iov;
286         struct iov_iter i;
287         ssize_t bytes = 0;
288
289         iov_for_each(iov, i, *iter) {
290                 ssize_t res;
291
292                 res = generic_file_aio_read(iocb, &iov, 1, iocb->ki_pos);
293                 if (res <= 0) {
294                         if (bytes == 0)
295                                 bytes = res;
296                         break;
297                 }
298
299                 bytes += res;
300                 if (res < iov.iov_len)
301                         break;
302         }
303
304         if (bytes > 0)
305                 iov_iter_advance(iter, bytes);
306         return bytes;
307 }
308
309 static inline ssize_t
310 __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *iter)
311 {
312         struct iovec iov;
313         struct iov_iter i;
314         ssize_t bytes = 0;
315
316         /* Since LLITE updates file size at the end of I/O in
317          * vvp_io_commit_write(), append write has to be done in atomic when
318          * there are multiple segments because otherwise each iteration to
319          * __generic_file_aio_write() will see original file size */
320         if (unlikely(iocb->ki_filp->f_flags & O_APPEND && iter->nr_segs > 1)) {
321                 struct iovec *iov_copy;
322                 int count = 0;
323
324                 OBD_ALLOC_PTR_ARRAY(iov_copy, iter->nr_segs);
325                 if (!iov_copy)
326                         return -ENOMEM;
327
328                 iov_for_each(iov, i, *iter)
329                         iov_copy[count++] = iov;
330
331                 bytes = __generic_file_aio_write(iocb, iov_copy, count,
332                                                  &iocb->ki_pos);
333                 OBD_FREE_PTR_ARRAY(iov_copy, iter->nr_segs);
334
335                 if (bytes > 0)
336                         iov_iter_advance(iter, bytes);
337                 return bytes;
338         }
339
340         iov_for_each(iov, i, *iter) {
341                 ssize_t res;
342
343                 res = __generic_file_aio_write(iocb, &iov, 1, &iocb->ki_pos);
344                 if (res <= 0) {
345                         if (bytes == 0)
346                                 bytes = res;
347                         break;
348                 }
349
350                 bytes += res;
351                 if (res < iov.iov_len)
352                         break;
353         }
354
355         if (bytes > 0)
356                 iov_iter_advance(iter, bytes);
357         return bytes;
358 }
359 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
360
361 static inline void __user *get_vmf_address(struct vm_fault *vmf)
362 {
363 #ifdef HAVE_VM_FAULT_ADDRESS
364         return (void __user *)vmf->address;
365 #else
366         return vmf->virtual_address;
367 #endif
368 }
369
370 #ifdef HAVE_VM_OPS_USE_VM_FAULT_ONLY
371 # define ll_filemap_fault(vma, vmf) filemap_fault(vmf)
372 #else
373 # define ll_filemap_fault(vma, vmf) filemap_fault(vma, vmf)
374 #endif
375
376 #ifndef HAVE_CURRENT_TIME
377 static inline struct timespec current_time(struct inode *inode)
378 {
379         return CURRENT_TIME;
380 }
381 #endif
382
383 #ifndef time_after32
384 /**
385  * time_after32 - compare two 32-bit relative times
386  * @a: the time which may be after @b
387  * @b: the time which may be before @a
388  *
389  * Needed for kernels earlier than v4.14-rc1~134^2
390  *
391  * time_after32(a, b) returns true if the time @a is after time @b.
392  * time_before32(b, a) returns true if the time @b is before time @a.
393  *
394  * Similar to time_after(), compare two 32-bit timestamps for relative
395  * times.  This is useful for comparing 32-bit seconds values that can't
396  * be converted to 64-bit values (e.g. due to disk format or wire protocol
397  * issues) when it is known that the times are less than 68 years apart.
398  */
399 #define time_after32(a, b)     ((s32)((u32)(b) - (u32)(a)) < 0)
400 #define time_before32(b, a)    time_after32(a, b)
401
402 #endif
403
404 #ifndef smp_store_mb
405 #define smp_store_mb(var, value)        set_mb(var, value)
406 #endif
407
408 #if IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY)
409 static inline unsigned short blk_integrity_interval(struct blk_integrity *bi)
410 {
411 #ifdef HAVE_INTERVAL_EXP_BLK_INTEGRITY
412         return bi->interval_exp ? 1 << bi->interval_exp : 0;
413 #elif defined(HAVE_INTERVAL_BLK_INTEGRITY)
414         return bi->interval;
415 #else
416         return bi->sector_size;
417 #endif /* !HAVE_INTERVAL_EXP_BLK_INTEGRITY */
418 }
419
420 static inline const char *blk_integrity_name(struct blk_integrity *bi)
421 {
422 #ifdef HAVE_INTERVAL_EXP_BLK_INTEGRITY
423         return bi->profile->name;
424 #else
425         return bi->name;
426 #endif
427 }
428
429 static inline unsigned int bip_size(struct bio_integrity_payload *bip)
430 {
431 #ifdef HAVE_BIP_ITER_BIO_INTEGRITY_PAYLOAD
432         return bip->bip_iter.bi_size;
433 #else
434         return bip->bip_size;
435 #endif
436 }
437 #else /* !CONFIG_BLK_DEV_INTEGRITY */
438 static inline unsigned short blk_integrity_interval(struct blk_integrity *bi)
439 {
440         return 0;
441 }
442 static inline const char *blk_integrity_name(struct blk_integrity *bi)
443 {
444         /* gcc8 dislikes when strcmp() is called against NULL */
445         return "";
446 }
447 #endif /* !CONFIG_BLK_DEV_INTEGRITY */
448
449 #ifndef INTEGRITY_FLAG_READ
450 #define INTEGRITY_FLAG_READ BLK_INTEGRITY_VERIFY
451 #endif
452
453 #ifndef INTEGRITY_FLAG_WRITE
454 #define INTEGRITY_FLAG_WRITE BLK_INTEGRITY_GENERATE
455 #endif
456
457 static inline bool bdev_integrity_enabled(struct block_device *bdev, int rw)
458 {
459 #if IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY)
460         struct blk_integrity *bi = bdev_get_integrity(bdev);
461
462         if (bi == NULL)
463                 return false;
464
465 #ifdef HAVE_INTERVAL_EXP_BLK_INTEGRITY
466         if (rw == 0 && bi->profile->verify_fn != NULL &&
467             (bi->flags & INTEGRITY_FLAG_READ))
468                 return true;
469
470         if (rw == 1 && bi->profile->generate_fn != NULL &&
471             (bi->flags & INTEGRITY_FLAG_WRITE))
472                 return true;
473 #else
474         if (rw == 0 && bi->verify_fn != NULL &&
475             (bi->flags & INTEGRITY_FLAG_READ))
476                 return true;
477
478         if (rw == 1 && bi->generate_fn != NULL &&
479             (bi->flags & INTEGRITY_FLAG_WRITE))
480                 return true;
481 #endif /* !HAVE_INTERVAL_EXP_BLK_INTEGRITY */
482 #endif /* !CONFIG_BLK_DEV_INTEGRITY */
483
484         return false;
485 }
486
487 #ifdef HAVE_PAGEVEC_INIT_ONE_PARAM
488 #define ll_pagevec_init(pvec, n) pagevec_init(pvec)
489 #else
490 #define ll_pagevec_init(pvec, n) pagevec_init(pvec, n)
491 #endif
492
493 #ifdef HAVE_D_COUNT
494 #  define ll_d_count(d)         d_count(d)
495 #else
496 #  define ll_d_count(d)         ((d)->d_count)
497 #endif /* HAVE_D_COUNT */
498
499 #ifndef HAVE_IN_COMPAT_SYSCALL
500 #define in_compat_syscall       is_compat_task
501 #endif
502
503 #ifdef HAVE_I_PAGES
504 #define page_tree i_pages
505 #define ll_xa_lock_irqsave(lockp, flags) xa_lock_irqsave(lockp, flags)
506 #define ll_xa_unlock_irqrestore(lockp, flags) xa_unlock_irqrestore(lockp, flags)
507 #else
508 #define i_pages tree_lock
509 #define ll_xa_lock_irqsave(lockp, flags) spin_lock_irqsave(lockp, flags)
510 #define ll_xa_unlock_irqrestore(lockp, flags) spin_unlock_irqrestore(lockp, flags)
511 #endif
512
513 #ifndef HAVE_LOCK_PAGE_MEMCG
514 #define lock_page_memcg(page) do {} while (0)
515 #define unlock_page_memcg(page) do {} while (0)
516 #endif
517
518 #ifndef KMEM_CACHE_USERCOPY
519 #define kmem_cache_create_usercopy(name, size, align, flags, useroffset, \
520                                    usersize, ctor)                       \
521         kmem_cache_create(name, size, align, flags, ctor)
522 #endif
523
524 #ifndef HAVE_LINUX_SELINUX_IS_ENABLED
525 #define selinux_is_enabled() 1
526 #endif
527
528 static inline int ll_vfs_getxattr(struct dentry *dentry, struct inode *inode,
529                                   const char *name,
530                                   void *value, size_t size)
531 {
532 #ifdef HAVE_USER_NAMESPACE_ARG
533         return vfs_getxattr(&init_user_ns, dentry, name, value, size);
534 #elif defined(HAVE_VFS_SETXATTR)
535         return __vfs_getxattr(dentry, inode, name, value, size);
536 #else
537         if (unlikely(!inode->i_op->getxattr))
538                 return -ENODATA;
539
540         return inode->i_op->getxattr(dentry, name, value, size);
541 #endif
542 }
543
544 static inline int ll_vfs_setxattr(struct dentry *dentry, struct inode *inode,
545                                   const char *name,
546                                   const void *value, size_t size, int flags)
547 {
548 #ifdef HAVE_USER_NAMESPACE_ARG
549         return vfs_setxattr(&init_user_ns, dentry, name, value, size, flags);
550 #elif defined(HAVE_VFS_SETXATTR)
551         return __vfs_setxattr(dentry, inode, name, value, size, flags);
552 #else
553         if (unlikely(!inode->i_op->setxattr))
554                 return -EOPNOTSUPP;
555
556         return inode->i_op->setxattr(dentry, name, value, size, flags);
557 #endif
558 }
559
560 static inline int ll_vfs_removexattr(struct dentry *dentry, struct inode *inode,
561                                      const char *name)
562 {
563 #ifdef HAVE_USER_NAMESPACE_ARG
564         return vfs_removexattr(&init_user_ns, dentry, name);
565 #elif defined(HAVE_VFS_SETXATTR)
566         return __vfs_removexattr(dentry, name);
567 #else
568         if (unlikely(!inode->i_op->setxattr))
569                 return -EOPNOTSUPP;
570
571         return inode->i_op->removexattr(dentry, name);
572 #endif
573 }
574
575 #ifndef FALLOC_FL_COLLAPSE_RANGE
576 #define FALLOC_FL_COLLAPSE_RANGE 0x08 /* remove a range of a file */
577 #endif
578
579 #ifndef FALLOC_FL_ZERO_RANGE
580 #define FALLOC_FL_ZERO_RANGE 0x10 /* convert range to zeros */
581 #endif
582
583 #ifndef FALLOC_FL_INSERT_RANGE
584 #define FALLOC_FL_INSERT_RANGE 0x20 /* insert space within file */
585 #endif
586
587 #ifndef raw_cpu_ptr
588 #define raw_cpu_ptr(p) __this_cpu_ptr(p)
589 #endif
590
591 #ifndef HAVE_IS_ROOT_INODE
592 static inline bool is_root_inode(struct inode *inode)
593 {
594         return inode == inode->i_sb->s_root->d_inode;
595 }
596 #endif
597
598 #ifndef HAVE_REGISTER_SHRINKER_RET
599 #define register_shrinker(_s) (register_shrinker(_s), 0)
600 #endif
601
602 #ifndef fallthrough
603 #define fallthrough do {} while (0)  /* fallthrough */
604 #endif
605
606 static inline void ll_security_release_secctx(char *secdata, u32 seclen)
607 {
608 #ifdef HAVE_SEC_RELEASE_SECCTX_1ARG
609         struct lsmcontext context = { };
610
611         lsmcontext_init(&context, secdata, seclen, 0);
612         return security_release_secctx(&context);
613 #else
614         return security_release_secctx(secdata, seclen);
615 #endif
616 }
617
618 #ifndef HAVE_USER_NAMESPACE_ARG
619 #define posix_acl_update_mode(ns, inode, mode, acl) \
620         posix_acl_update_mode(inode, mode, acl)
621 #define notify_change(ns, de, attr, inode)      notify_change(de, attr, inode)
622 #define inode_owner_or_capable(ns, inode)       inode_owner_or_capable(inode)
623 #define vfs_create(ns, dir, de, mode, ex)       vfs_create(dir, de, mode, ex)
624 #define vfs_mkdir(ns, dir, de, mode)            vfs_mkdir(dir, de, mode)
625 #define ll_set_acl(ns, inode, acl, type)        ll_set_acl(inode, acl, type)
626 #endif
627
628 #endif /* _LUSTRE_COMPAT_H */