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