Whamcloud - gitweb
LU-16802 build: compatibility for 6.4 kernels
[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/backing-dev.h>
45 #include <linux/slab.h>
46 #include <linux/security.h>
47 #include <libcfs/linux/linux-fs.h>
48 #ifdef HAVE_XARRAY_SUPPORT
49 #include <linux/xarray.h>
50 #else
51 #include <libcfs/linux/xarray.h>
52 #endif
53 #include <obd_support.h>
54
55 #ifdef HAVE_4ARGS_VFS_SYMLINK
56 #define ll_vfs_symlink(dir, dentry, mnt, path, mode) \
57                 vfs_symlink(dir, dentry, path, mode)
58 #else
59 #define ll_vfs_symlink(dir, dentry, mnt, path, mode) \
60                        vfs_symlink(dir, dentry, path)
61 #endif
62
63 #ifdef HAVE_BVEC_ITER
64 #define bio_idx(bio)                    (bio->bi_iter.bi_idx)
65 #define bio_set_sector(bio, sector)     (bio->bi_iter.bi_sector = sector)
66 #define bvl_to_page(bvl)                (bvl->bv_page)
67 #else
68 #define bio_idx(bio)                    (bio->bi_idx)
69 #define bio_set_sector(bio, sector)     (bio->bi_sector = sector)
70 #define bio_sectors(bio)                ((bio)->bi_size >> 9)
71 #define bvl_to_page(bvl)                (bvl->bv_page)
72 #endif
73
74 #ifdef HAVE_BVEC_ITER
75 #define bio_start_sector(bio) (bio->bi_iter.bi_sector)
76 #else
77 #define bio_start_sector(bio) (bio->bi_sector)
78 #endif
79
80 #ifdef HAVE_BI_BDEV
81 # define bio_get_dev(bio)       ((bio)->bi_bdev)
82 # define bio_get_disk(bio)      (bio_get_dev(bio)->bd_disk)
83 # define bio_get_queue(bio)     bdev_get_queue(bio_get_dev(bio))
84
85 # ifndef HAVE_BIO_SET_DEV
86 #  define bio_set_dev(bio, bdev) (bio_get_dev(bio) = (bdev))
87 # endif
88 #else
89 # define bio_get_disk(bio)      ((bio)->bi_disk)
90 # define bio_get_queue(bio)     (bio_get_disk(bio)->queue)
91 #endif
92
93 #ifndef HAVE_BI_OPF
94 #define bi_opf bi_rw
95 #endif
96
97 static inline struct bio *cfs_bio_alloc(struct block_device *bdev,
98                                         unsigned short nr_vecs,
99                                         __u32 op, gfp_t gfp_mask)
100 {
101         struct bio *bio;
102 #ifdef HAVE_BIO_ALLOC_WITH_BDEV
103         bio = bio_alloc(bdev, nr_vecs, op, gfp_mask);
104 #else
105         bio = bio_alloc(gfp_mask, nr_vecs);
106         if (bio) {
107                 bio_set_dev(bio, bdev);
108                 bio->bi_opf = op;
109         }
110 #endif /* HAVE_BIO_ALLOC_WITH_BDEV */
111         return bio;
112 }
113
114 #ifndef HAVE_DENTRY_D_CHILD
115 #define d_child                 d_u.d_child
116 #endif
117
118 #ifndef HAVE_D_IN_LOOKUP
119 static inline int d_in_lookup(struct dentry *dentry)
120 {
121         return false;
122 }
123 #endif
124
125 #ifndef HAVE_VM_FAULT_T
126 #define vm_fault_t int
127 #endif
128
129 #ifndef HAVE_FOP_ITERATE_SHARED
130 #define iterate_shared iterate
131 #endif
132
133 #ifdef HAVE_OLDSIZE_TRUNCATE_PAGECACHE
134 #define ll_truncate_pagecache(inode, size) truncate_pagecache(inode, 0, size)
135 #else
136 #define ll_truncate_pagecache(inode, size) truncate_pagecache(inode, size)
137 #endif
138
139 #ifdef HAVE_VFS_RENAME_5ARGS
140 #define ll_vfs_rename(a, b, c, d) vfs_rename(a, b, c, d, NULL)
141 #elif defined HAVE_VFS_RENAME_6ARGS
142 #define ll_vfs_rename(a, b, c, d) vfs_rename(a, b, c, d, NULL, 0)
143 #else
144 #define ll_vfs_rename(a, b, c, d) vfs_rename(a, b, c, d)
145 #endif
146
147 #ifdef HAVE_USER_NAMESPACE_ARG
148 #define vfs_unlink(ns, dir, de) vfs_unlink(ns, dir, de, NULL)
149 #elif defined HAVE_VFS_UNLINK_3ARGS
150 #define vfs_unlink(ns, dir, de) vfs_unlink(dir, de, NULL)
151 #else
152 #define vfs_unlink(ns, dir, de) vfs_unlink(dir, de)
153 #endif
154
155 #ifndef HAVE_MNT_IDMAP_ARG
156 #define mnt_idmap       user_namespace
157 #define nop_mnt_idmap   init_user_ns
158 #endif
159
160 static inline int ll_vfs_getattr(struct path *path, struct kstat *st,
161                                  u32 request_mask, unsigned int flags)
162 {
163         int rc;
164
165 #if defined(HAVE_USER_NAMESPACE_ARG) || defined(HAVE_INODEOPS_ENHANCED_GETATTR)
166         rc = vfs_getattr(path, st, request_mask, flags);
167 #else
168         rc = vfs_getattr(path, st);
169 #endif
170         return rc;
171 }
172
173 #ifndef HAVE_D_IS_POSITIVE
174 static inline bool d_is_positive(const struct dentry *dentry)
175 {
176         return dentry->d_inode != NULL;
177 }
178 #endif
179
180 #ifndef HAVE_INODE_LOCK
181 # define inode_lock(inode) mutex_lock(&(inode)->i_mutex)
182 # define inode_unlock(inode) mutex_unlock(&(inode)->i_mutex)
183 # define inode_trylock(inode) mutex_trylock(&(inode)->i_mutex)
184 #endif
185
186 #ifndef HAVE_PAGECACHE_GET_PAGE
187 #define pagecache_get_page(mapping, index, fp, gfp) \
188         grab_cache_page_nowait(mapping, index)
189 #endif
190
191 /* Old kernels lacked both Xarray support and the page cache
192  * using Xarrays. Our back ported Xarray support introduces
193  * the real xa_is_value() but we need a wrapper as well for
194  * the page cache interaction. Lets keep xa_is_value() separate
195  * in old kernels for Xarray support and page cache handling.
196  */
197 #ifndef HAVE_XARRAY_SUPPORT
198 static inline bool ll_xa_is_value(void *entry)
199 {
200         return radix_tree_exceptional_entry(entry);
201 }
202 #else
203 #define ll_xa_is_value  xa_is_value
204 #endif
205
206 /* Linux kernel version v5.0 commit fd9dc93e36231fb6d520e0edd467058fad4fd12d
207  * ("XArray: Change xa_insert to return -EBUSY")
208  * instead of -EEXIST
209  */
210 static inline int __must_check ll_xa_insert(struct xarray *xa,
211                                             unsigned long index,
212                                             void *entry, gfp_t gpf)
213 {
214         int rc = xa_insert(xa, index, entry, gpf);
215
216         if (rc == -EEXIST)
217                 rc = -EBUSY;
218         return rc;
219 }
220
221 #ifndef HAVE_TRUNCATE_INODE_PAGES_FINAL
222 static inline void truncate_inode_pages_final(struct address_space *map)
223 {
224         truncate_inode_pages(map, 0);
225 }
226 #endif
227
228 #ifdef HAVE_U64_CAPABILITY
229 #define ll_capability_u32(kcap) \
230         ((kcap).val & 0xFFFFFFFF)
231 #define ll_set_capability_u32(kcap, val32) \
232         ((kcap)->val = ((kcap)->val & 0xffffffff00000000ull) | (val32))
233 #else
234 #define ll_capability_u32(kcap) \
235         ((kcap).cap[0])
236 #define ll_set_capability_u32(kcap, val32) \
237         ((kcap)->cap[0] = val32)
238 #endif
239
240 #ifndef HAVE_PTR_ERR_OR_ZERO
241 static inline int __must_check PTR_ERR_OR_ZERO(__force const void *ptr)
242 {
243         if (IS_ERR(ptr))
244                 return PTR_ERR(ptr);
245         else
246                 return 0;
247 }
248 #endif
249
250 #ifdef HAVE_PID_NS_FOR_CHILDREN
251 # define ll_task_pid_ns(task) \
252          ((task)->nsproxy ? ((task)->nsproxy->pid_ns_for_children) : NULL)
253 #else
254 # define ll_task_pid_ns(task) \
255          ((task)->nsproxy ? ((task)->nsproxy->pid_ns) : NULL)
256 #endif
257
258 #ifdef HAVE_FULL_NAME_HASH_3ARGS
259 # define ll_full_name_hash(salt, name, len) full_name_hash(salt, name, len)
260 #else
261 # define ll_full_name_hash(salt, name, len) full_name_hash(name, len)
262 #endif
263
264 #ifdef HAVE_STRUCT_POSIX_ACL_XATTR
265 # define posix_acl_xattr_header struct posix_acl_xattr_header
266 # define posix_acl_xattr_entry  struct posix_acl_xattr_entry
267 # define GET_POSIX_ACL_XATTR_ENTRY(head) ((void *)((head) + 1))
268 #else
269 # define GET_POSIX_ACL_XATTR_ENTRY(head) ((head)->a_entries)
270 #endif
271
272 #ifdef HAVE_IOP_XATTR
273 #define ll_setxattr     generic_setxattr
274 #define ll_getxattr     generic_getxattr
275 #define ll_removexattr  generic_removexattr
276 #endif /* HAVE_IOP_XATTR */
277
278 #ifndef HAVE_POSIX_ACL_VALID_USER_NS
279 #define posix_acl_valid(a,b)            posix_acl_valid(b)
280 #endif
281
282 #ifdef HAVE_IOP_SET_ACL
283 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
284 #if !defined(HAVE_USER_NAMESPACE_ARG) \
285  && !defined(HAVE_POSIX_ACL_UPDATE_MODE) \
286  && !defined(HAVE_MNT_IDMAP_ARG)
287 static inline int posix_acl_update_mode(struct inode *inode, umode_t *mode_p,
288                           struct posix_acl **acl)
289 {
290         umode_t mode = inode->i_mode;
291         int error;
292
293         error = posix_acl_equiv_mode(*acl, &mode);
294         if (error < 0)
295                 return error;
296         if (error == 0)
297                 *acl = NULL;
298         if (!in_group_p(inode->i_gid) &&
299             !capable_wrt_inode_uidgid(inode, CAP_FSETID))
300                 mode &= ~S_ISGID;
301         *mode_p = mode;
302         return 0;
303 }
304 #endif /* HAVE_POSIX_ACL_UPDATE_MODE */
305 #endif
306 #endif
307
308 #ifndef HAVE_IOV_ITER_TRUNCATE
309 static inline void iov_iter_truncate(struct iov_iter *i, u64 count)
310 {
311         if (i->count > count)
312                 i->count = count;
313 }
314 #endif
315
316 /*
317  * mount MS_* flags split from superblock SB_* flags
318  * if the SB_* flags are not available use the MS_* flags
319  */
320 #if !defined(SB_RDONLY) && defined(MS_RDONLY)
321 # define SB_RDONLY MS_RDONLY
322 #endif
323 #if !defined(SB_ACTIVE) && defined(MS_ACTIVE)
324 # define SB_ACTIVE MS_ACTIVE
325 #endif
326 #if !defined(SB_NOSEC) && defined(MS_NOSEC)
327 # define SB_NOSEC MS_NOSEC
328 #endif
329 #if !defined(SB_POSIXACL) && defined(MS_POSIXACL)
330 # define SB_POSIXACL MS_POSIXACL
331 #endif
332 #if !defined(SB_NODIRATIME) && defined(MS_NODIRATIME)
333 # define SB_NODIRATIME MS_NODIRATIME
334 #endif
335 #if !defined(SB_KERNMOUNT) && defined(MS_KERNMOUNT)
336 # define SB_KERNMOUNT MS_KERNMOUNT
337 #endif
338
339 #ifndef HAVE_IOV_ITER_IOVEC
340 static inline struct iovec iov_iter_iovec(const struct iov_iter *iter)
341 {
342         return (struct iovec) {
343                 .iov_base = iter->__iov->iov_base + iter->iov_offset,
344                 .iov_len = min(iter->count,
345                                iter->__iov->iov_len - iter->iov_offset),
346         };
347 }
348 #endif
349
350 #ifndef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
351 static inline void iov_iter_reexpand(struct iov_iter *i, size_t count)
352 {
353         i->count = count;
354 }
355
356 #define iov_for_each(iov, iter, start)                                  \
357         for (iter = (start);                                            \
358              (iter).count && ((iov = iov_iter_iovec(&(iter))), 1);      \
359              iov_iter_advance(&(iter), (iov).iov_len))
360
361 static inline ssize_t
362 generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
363 {
364         struct iovec iov;
365         struct iov_iter i;
366         ssize_t bytes = 0;
367
368         iov_for_each(iov, i, *iter) {
369                 ssize_t res;
370
371                 res = generic_file_aio_read(iocb, &iov, 1, iocb->ki_pos);
372                 if (res <= 0) {
373                         if (bytes == 0)
374                                 bytes = res;
375                         break;
376                 }
377
378                 bytes += res;
379                 if (res < iov.iov_len)
380                         break;
381         }
382
383         if (bytes > 0)
384                 iov_iter_advance(iter, bytes);
385         return bytes;
386 }
387
388 static inline ssize_t
389 __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *iter)
390 {
391         struct iovec iov;
392         struct iov_iter i;
393         ssize_t bytes = 0;
394
395         /* Since LLITE updates file size at the end of I/O in
396          * vvp_io_commit_write(), append write has to be done in atomic when
397          * there are multiple segments because otherwise each iteration to
398          * __generic_file_aio_write() will see original file size */
399         if (unlikely(iocb->ki_filp->f_flags & O_APPEND && iter->nr_segs > 1)) {
400                 struct iovec *iov_copy;
401                 int count = 0;
402
403                 OBD_ALLOC_PTR_ARRAY(iov_copy, iter->nr_segs);
404                 if (!iov_copy)
405                         return -ENOMEM;
406
407                 iov_for_each(iov, i, *iter)
408                         iov_copy[count++] = iov;
409
410                 bytes = __generic_file_aio_write(iocb, iov_copy, count,
411                                                  &iocb->ki_pos);
412                 OBD_FREE_PTR_ARRAY(iov_copy, iter->nr_segs);
413
414                 if (bytes > 0)
415                         iov_iter_advance(iter, bytes);
416                 return bytes;
417         }
418
419         iov_for_each(iov, i, *iter) {
420                 ssize_t res;
421
422                 res = __generic_file_aio_write(iocb, &iov, 1, &iocb->ki_pos);
423                 if (res <= 0) {
424                         if (bytes == 0)
425                                 bytes = res;
426                         break;
427                 }
428
429                 bytes += res;
430                 if (res < iov.iov_len)
431                         break;
432         }
433
434         if (bytes > 0)
435                 iov_iter_advance(iter, bytes);
436         return bytes;
437 }
438 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
439
440 static inline void __user *get_vmf_address(struct vm_fault *vmf)
441 {
442 #ifdef HAVE_VM_FAULT_ADDRESS
443         return (void __user *)vmf->address;
444 #else
445         return vmf->virtual_address;
446 #endif
447 }
448
449 #ifdef HAVE_VM_OPS_USE_VM_FAULT_ONLY
450 # define __ll_filemap_fault(vma, vmf) filemap_fault(vmf)
451 #else
452 # define __ll_filemap_fault(vma, vmf) filemap_fault(vma, vmf)
453 #endif
454
455 #ifndef HAVE_CURRENT_TIME
456 static inline struct timespec current_time(struct inode *inode)
457 {
458         return CURRENT_TIME;
459 }
460 #endif
461
462 #ifndef time_after32
463 /**
464  * time_after32 - compare two 32-bit relative times
465  * @a: the time which may be after @b
466  * @b: the time which may be before @a
467  *
468  * Needed for kernels earlier than v4.14-rc1~134^2
469  *
470  * time_after32(a, b) returns true if the time @a is after time @b.
471  * time_before32(b, a) returns true if the time @b is before time @a.
472  *
473  * Similar to time_after(), compare two 32-bit timestamps for relative
474  * times.  This is useful for comparing 32-bit seconds values that can't
475  * be converted to 64-bit values (e.g. due to disk format or wire protocol
476  * issues) when it is known that the times are less than 68 years apart.
477  */
478 #define time_after32(a, b)     ((s32)((u32)(b) - (u32)(a)) < 0)
479 #define time_before32(b, a)    time_after32(a, b)
480
481 #endif
482
483 #ifndef smp_store_mb
484 #define smp_store_mb(var, value)        set_mb(var, value)
485 #endif
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 /* Linux commit v5.15-12273-gab2f9d2d3626
514  *   mm: unexport {,un}lock_page_memcg
515  *
516  * Note that the functions are still defined or declared breaking
517  * the simple approach of just defining the missing functions here
518  */
519 #ifdef HAVE_LOCK_PAGE_MEMCG
520 #define vvp_lock_page_memcg(page)       lock_page_memcg((page))
521 #define vvp_unlock_page_memcg(page)     unlock_page_memcg((page))
522 #else
523 #define vvp_lock_page_memcg(page)
524 #define vvp_unlock_page_memcg(page)
525 #endif
526
527 #ifndef KMEM_CACHE_USERCOPY
528 #define kmem_cache_create_usercopy(name, size, align, flags, useroffset, \
529                                    usersize, ctor)                       \
530         kmem_cache_create(name, size, align, flags, ctor)
531 #endif
532
533 static inline bool ll_security_xattr_wanted(struct inode *in)
534 {
535 #ifdef CONFIG_SECURITY
536         return in->i_security && in->i_sb->s_security;
537 #else
538         return false;
539 #endif
540 }
541
542 static inline int ll_vfs_getxattr(struct dentry *dentry, struct inode *inode,
543                                   const char *name,
544                                   void *value, size_t size)
545 {
546 #if defined(HAVE_MNT_IDMAP_ARG) || defined(HAVE_USER_NAMESPACE_ARG)
547         return vfs_getxattr(&nop_mnt_idmap, dentry, name, value, size);
548 #elif defined(HAVE_VFS_SETXATTR)
549         return __vfs_getxattr(dentry, inode, name, value, size);
550 #else
551         if (unlikely(!inode->i_op->getxattr))
552                 return -ENODATA;
553
554         return inode->i_op->getxattr(dentry, name, value, size);
555 #endif
556 }
557
558 static inline int ll_vfs_setxattr(struct dentry *dentry, struct inode *inode,
559                                   const char *name,
560                                   const void *value, size_t size, int flags)
561 {
562 #if defined(HAVE_MNT_IDMAP_ARG) || defined(HAVE_USER_NAMESPACE_ARG)
563         return vfs_setxattr(&nop_mnt_idmap, dentry, name,
564                             VFS_SETXATTR_VALUE(value), size, flags);
565 #elif defined(HAVE_VFS_SETXATTR)
566         return __vfs_setxattr(dentry, inode, name, value, size, flags);
567 #else
568         if (unlikely(!inode->i_op->setxattr))
569                 return -EOPNOTSUPP;
570
571         return inode->i_op->setxattr(dentry, name, value, size, flags);
572 #endif
573 }
574
575 static inline int ll_vfs_removexattr(struct dentry *dentry, struct inode *inode,
576                                      const char *name)
577 {
578 #if defined(HAVE_MNT_IDMAP_ARG) || defined(HAVE_USER_NAMESPACE_ARG)
579         return vfs_removexattr(&nop_mnt_idmap, dentry, name);
580 #elif defined(HAVE_VFS_SETXATTR)
581         return __vfs_removexattr(dentry, name);
582 #else
583         if (unlikely(!inode->i_op->setxattr))
584                 return -EOPNOTSUPP;
585
586         return inode->i_op->removexattr(dentry, name);
587 #endif
588 }
589
590 /* until v3.19-rc5-3-gb4caecd48005 */
591 #ifndef BDI_CAP_MAP_COPY
592 #define BDI_CAP_MAP_COPY                0
593 #endif
594
595 /* from v4.1-rc2-56-g89e9b9e07a39, until v5.9-rc3-161-gf56753ac2a90 */
596 #ifndef BDI_CAP_CGROUP_WRITEBACK
597 #define BDI_CAP_CGROUP_WRITEBACK        0
598 #endif
599
600 /* from v5.9-rc3-161-gf56753ac2a90 */
601 #ifndef BDI_CAP_WRITEBACK
602 #define BDI_CAP_WRITEBACK               0
603 #endif
604
605 /* from v5.9-rc3-161-gf56753ac2a90 */
606 #ifndef BDI_CAP_WRITEBACK_ACCT
607 #define BDI_CAP_WRITEBACK_ACCT          0
608 #endif
609
610 #define LL_BDI_CAP_FLAGS        (BDI_CAP_CGROUP_WRITEBACK | BDI_CAP_MAP_COPY | \
611                                  BDI_CAP_WRITEBACK | BDI_CAP_WRITEBACK_ACCT)
612
613 #ifndef FALLOC_FL_COLLAPSE_RANGE
614 #define FALLOC_FL_COLLAPSE_RANGE 0x08 /* remove a range of a file */
615 #endif
616
617 #ifndef FALLOC_FL_ZERO_RANGE
618 #define FALLOC_FL_ZERO_RANGE 0x10 /* convert range to zeros */
619 #endif
620
621 #ifndef FALLOC_FL_INSERT_RANGE
622 #define FALLOC_FL_INSERT_RANGE 0x20 /* insert space within file */
623 #endif
624
625 #ifndef raw_cpu_ptr
626 #define raw_cpu_ptr(p) __this_cpu_ptr(p)
627 #endif
628
629 #ifndef HAVE_IS_ROOT_INODE
630 static inline bool is_root_inode(struct inode *inode)
631 {
632         return inode == inode->i_sb->s_root->d_inode;
633 }
634 #endif
635
636 #ifndef HAVE_IOV_ITER_GET_PAGES_ALLOC2
637 #define iov_iter_get_pages_alloc2(i, p, m, s) \
638         iov_iter_get_pages_alloc((i), (p), (m), (s))
639 #endif
640
641 #ifdef HAVE_AOPS_MIGRATE_FOLIO
642 #define folio_migr      folio
643 #else
644 #define folio_migr      page
645 #define migrate_folio   migratepage
646 #endif
647
648 #ifdef HAVE_REGISTER_SHRINKER_FORMAT_NAMED
649 #define register_shrinker(_s) register_shrinker((_s), "%ps", (_s))
650 #elif !defined(HAVE_REGISTER_SHRINKER_RET)
651 #define register_shrinker(_s) (register_shrinker(_s), 0)
652 #endif
653
654 #ifndef fallthrough
655 # if defined(__GNUC__) && __GNUC__ >= 7
656 #  define fallthrough  __attribute__((fallthrough)) /* fallthrough */
657 # else
658 #  define fallthrough do {} while (0)  /* fallthrough */
659 # endif
660 #endif
661
662 #ifdef VERIFY_WRITE /* removed in kernel commit v4.20-10979-g96d4f267e40f */
663 #define ll_access_ok(ptr, len) access_ok(VERIFY_WRITE, ptr, len)
664 #else
665 #define ll_access_ok(ptr, len) access_ok(ptr, len)
666 #endif
667
668 #ifdef HAVE_WB_STAT_MOD
669 #define __add_wb_stat(wb, item, amount)         wb_stat_mod(wb, item, amount)
670 #endif
671
672 #ifdef HAVE_SEC_RELEASE_SECCTX_1ARG
673 #ifndef HAVE_LSMCONTEXT_INIT
674 /* Ubuntu 5.19 */
675 static inline void lsmcontext_init(struct lsmcontext *cp, char *context,
676                                    u32 size, int slot)
677 {
678         cp->slot = slot;
679         cp->context = context;
680         cp->len = size;
681 }
682 #endif
683 #endif
684
685 static inline void ll_security_release_secctx(char *secdata, u32 seclen,
686                                               int slot)
687 {
688 #ifdef HAVE_SEC_RELEASE_SECCTX_1ARG
689         struct lsmcontext context = { };
690
691         lsmcontext_init(&context, secdata, seclen, slot);
692         return security_release_secctx(&context);
693 #else
694         return security_release_secctx(secdata, seclen);
695 #endif
696 }
697
698 #if !defined(HAVE_USER_NAMESPACE_ARG) && !defined(HAVE_MNT_IDMAP_ARG)
699 #define posix_acl_update_mode(ns, inode, mode, acl) \
700         posix_acl_update_mode(inode, mode, acl)
701 #define notify_change(ns, de, attr, inode)      notify_change(de, attr, inode)
702 #define inode_owner_or_capable(ns, inode)       inode_owner_or_capable(inode)
703 #define vfs_create(ns, dir, de, mode, ex)       vfs_create(dir, de, mode, ex)
704 #define vfs_mkdir(ns, dir, de, mode)            vfs_mkdir(dir, de, mode)
705 #define ll_set_acl(ns, inode, acl, type)        ll_set_acl(inode, acl, type)
706 #endif
707
708 /**
709  * delete_from_page_cache is not exported anymore
710  */
711 #ifdef HAVE_DELETE_FROM_PAGE_CACHE
712 #define cfs_delete_from_page_cache(page)        delete_from_page_cache((page))
713 #else
714 static inline void cfs_delete_from_page_cache(struct page *page)
715 {
716         if (!page->mapping)
717                 return;
718         LASSERT(PageLocked(page));
719         get_page(page);
720         unlock_page(page);
721         /* on entry page is locked */
722         if (S_ISREG(page->mapping->host->i_mode)) {
723                 generic_error_remove_page(page->mapping, page);
724         } else {
725                 loff_t lstart = page->index << PAGE_SHIFT;
726                 loff_t lend = lstart + PAGE_SIZE - 1;
727
728                 truncate_inode_pages_range(page->mapping, lstart, lend);
729         }
730         lock_page(page);
731         put_page(page);
732 }
733 #endif
734
735 static inline struct page *ll_read_cache_page(struct address_space *mapping,
736                                               pgoff_t index, filler_t *filler,
737                                               void *data)
738 {
739 #ifdef HAVE_READ_CACHE_PAGE_WANTS_FILE
740         struct file dummy_file;
741
742         dummy_file.f_ra.ra_pages = 32; /* unused, modified on ra error */
743         dummy_file.private_data = data;
744         return read_cache_page(mapping, index, filler, &dummy_file);
745 #else
746         return read_cache_page(mapping, index, filler, data);
747 #endif /* HAVE_READ_CACHE_PAGE_WANTS_FILE */
748 }
749
750 #endif /* _LUSTRE_COMPAT_H */