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