4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 #ifndef _LUSTRE_COMPAT_H
34 #define _LUSTRE_COMPAT_H
36 #include <linux/aio.h>
38 #include <linux/fs_struct.h>
39 #include <linux/namei.h>
40 #include <linux/pagemap.h>
41 #include <linux/posix_acl_xattr.h>
42 #include <linux/bio.h>
43 #include <linux/xattr.h>
44 #include <linux/workqueue.h>
45 #include <linux/blkdev.h>
46 #include <linux/slab.h>
48 #include <libcfs/linux/linux-fs.h>
49 #include <obd_support.h>
51 #ifdef HAVE_4ARGS_VFS_SYMLINK
52 #define ll_vfs_symlink(dir, dentry, mnt, path, mode) \
53 vfs_symlink(dir, dentry, path, mode)
55 #define ll_vfs_symlink(dir, dentry, mnt, path, mode) \
56 vfs_symlink(dir, dentry, path)
60 #define bio_idx(bio) (bio->bi_iter.bi_idx)
61 #define bio_set_sector(bio, sector) (bio->bi_iter.bi_sector = sector)
62 #define bvl_to_page(bvl) (bvl->bv_page)
64 #define bio_idx(bio) (bio->bi_idx)
65 #define bio_set_sector(bio, sector) (bio->bi_sector = sector)
66 #define bio_sectors(bio) ((bio)->bi_size >> 9)
67 #define bvl_to_page(bvl) (bvl->bv_page)
71 #define bio_start_sector(bio) (bio->bi_iter.bi_sector)
73 #define bio_start_sector(bio) (bio->bi_sector)
76 #ifndef HAVE_DENTRY_D_CHILD
77 #define d_child d_u.d_child
80 #ifdef HAVE_DENTRY_D_U_D_ALIAS
81 #define d_alias d_u.d_alias
84 #ifndef HAVE_D_IN_LOOKUP
85 static inline int d_in_lookup(struct dentry *dentry)
91 #ifndef HAVE_VM_FAULT_T
92 #define vm_fault_t int
95 #ifndef HAVE_FOP_ITERATE_SHARED
96 #define iterate_shared iterate
99 #ifdef HAVE_OLDSIZE_TRUNCATE_PAGECACHE
100 #define ll_truncate_pagecache(inode, size) truncate_pagecache(inode, 0, size)
102 #define ll_truncate_pagecache(inode, size) truncate_pagecache(inode, size)
105 #ifdef HAVE_VFS_RENAME_5ARGS
106 #define ll_vfs_rename(a, b, c, d) vfs_rename(a, b, c, d, NULL)
107 #elif defined HAVE_VFS_RENAME_6ARGS
108 #define ll_vfs_rename(a, b, c, d) vfs_rename(a, b, c, d, NULL, 0)
110 #define ll_vfs_rename(a, b, c, d) vfs_rename(a, b, c, d)
113 #ifdef HAVE_VFS_UNLINK_3ARGS
114 #define ll_vfs_unlink(a, b) vfs_unlink(a, b, NULL)
116 #define ll_vfs_unlink(a, b) vfs_unlink(a, b)
119 static inline int ll_vfs_getattr(struct path *path, struct kstat *st)
123 #ifdef HAVE_INODEOPS_ENHANCED_GETATTR
124 rc = vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
126 rc = vfs_getattr(path, st);
131 #ifndef HAVE_D_IS_POSITIVE
132 static inline bool d_is_positive(const struct dentry *dentry)
134 return dentry->d_inode != NULL;
138 #ifndef HAVE_INODE_LOCK
139 # define inode_lock(inode) mutex_lock(&(inode)->i_mutex)
140 # define inode_unlock(inode) mutex_unlock(&(inode)->i_mutex)
141 # define inode_trylock(inode) mutex_trylock(&(inode)->i_mutex)
144 #ifndef HAVE_XA_IS_VALUE
145 static inline bool xa_is_value(void *entry)
147 return radix_tree_exceptional_entry(entry);
151 #ifndef HAVE_TRUNCATE_INODE_PAGES_FINAL
152 static inline void truncate_inode_pages_final(struct address_space *map)
154 truncate_inode_pages(map, 0);
158 #ifndef HAVE_PTR_ERR_OR_ZERO
159 static inline int __must_check PTR_ERR_OR_ZERO(__force const void *ptr)
168 #ifdef HAVE_PID_NS_FOR_CHILDREN
169 # define ll_task_pid_ns(task) \
170 ((task)->nsproxy ? ((task)->nsproxy->pid_ns_for_children) : NULL)
172 # define ll_task_pid_ns(task) \
173 ((task)->nsproxy ? ((task)->nsproxy->pid_ns) : NULL)
176 #ifdef HAVE_FULL_NAME_HASH_3ARGS
177 # define ll_full_name_hash(salt, name, len) full_name_hash(salt, name, len)
179 # define ll_full_name_hash(salt, name, len) full_name_hash(name, len)
182 #ifdef HAVE_STRUCT_POSIX_ACL_XATTR
183 # define posix_acl_xattr_header struct posix_acl_xattr_header
184 # define posix_acl_xattr_entry struct posix_acl_xattr_entry
185 # define GET_POSIX_ACL_XATTR_ENTRY(head) ((void *)((head) + 1))
187 # define GET_POSIX_ACL_XATTR_ENTRY(head) ((head)->a_entries)
190 #ifdef HAVE_IOP_XATTR
191 #define ll_setxattr generic_setxattr
192 #define ll_getxattr generic_getxattr
193 #define ll_removexattr generic_removexattr
194 #endif /* HAVE_IOP_XATTR */
196 #ifndef HAVE_POSIX_ACL_VALID_USER_NS
197 #define posix_acl_valid(a,b) posix_acl_valid(b)
200 #ifdef HAVE_IOP_SET_ACL
201 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
202 #ifndef HAVE_POSIX_ACL_UPDATE_MODE
203 static inline int posix_acl_update_mode(struct inode *inode, umode_t *mode_p,
204 struct posix_acl **acl)
206 umode_t mode = inode->i_mode;
209 error = posix_acl_equiv_mode(*acl, &mode);
214 if (!in_group_p(inode->i_gid) &&
215 !capable_wrt_inode_uidgid(inode, CAP_FSETID))
220 #endif /* HAVE_POSIX_ACL_UPDATE_MODE */
224 #ifndef HAVE_IOV_ITER_TRUNCATE
225 static inline void iov_iter_truncate(struct iov_iter *i, u64 count)
227 if (i->count > count)
233 * mount MS_* flags split from superblock SB_* flags
234 * if the SB_* flags are not available use the MS_* flags
236 #if !defined(SB_RDONLY) && defined(MS_RDONLY)
237 # define SB_RDONLY MS_RDONLY
239 #if !defined(SB_ACTIVE) && defined(MS_ACTIVE)
240 # define SB_ACTIVE MS_ACTIVE
242 #if !defined(SB_NOSEC) && defined(MS_NOSEC)
243 # define SB_NOSEC MS_NOSEC
245 #if !defined(SB_POSIXACL) && defined(MS_POSIXACL)
246 # define SB_POSIXACL MS_POSIXACL
248 #if !defined(SB_NODIRATIME) && defined(MS_NODIRATIME)
249 # define SB_NODIRATIME MS_NODIRATIME
252 #ifndef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
253 static inline void iov_iter_reexpand(struct iov_iter *i, size_t count)
258 static inline struct iovec iov_iter_iovec(const struct iov_iter *iter)
260 return (struct iovec) {
261 .iov_base = iter->iov->iov_base + iter->iov_offset,
262 .iov_len = min(iter->count,
263 iter->iov->iov_len - iter->iov_offset),
267 #define iov_for_each(iov, iter, start) \
268 for (iter = (start); \
269 (iter).count && ((iov = iov_iter_iovec(&(iter))), 1); \
270 iov_iter_advance(&(iter), (iov).iov_len))
272 static inline ssize_t
273 generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
279 iov_for_each(iov, i, *iter) {
282 res = generic_file_aio_read(iocb, &iov, 1, iocb->ki_pos);
290 if (res < iov.iov_len)
295 iov_iter_advance(iter, bytes);
299 static inline ssize_t
300 __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *iter)
306 /* Since LLITE updates file size at the end of I/O in
307 * vvp_io_commit_write(), append write has to be done in atomic when
308 * there are multiple segments because otherwise each iteration to
309 * __generic_file_aio_write() will see original file size */
310 if (unlikely(iocb->ki_filp->f_flags & O_APPEND && iter->nr_segs > 1)) {
311 struct iovec *iov_copy;
314 OBD_ALLOC(iov_copy, sizeof(*iov_copy) * iter->nr_segs);
318 iov_for_each(iov, i, *iter)
319 iov_copy[count++] = iov;
321 bytes = __generic_file_aio_write(iocb, iov_copy, count,
323 OBD_FREE(iov_copy, sizeof(*iov_copy) * iter->nr_segs);
326 iov_iter_advance(iter, bytes);
330 iov_for_each(iov, i, *iter) {
333 res = __generic_file_aio_write(iocb, &iov, 1, &iocb->ki_pos);
341 if (res < iov.iov_len)
346 iov_iter_advance(iter, bytes);
349 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
351 static inline void __user *get_vmf_address(struct vm_fault *vmf)
353 #ifdef HAVE_VM_FAULT_ADDRESS
354 return (void __user *)vmf->address;
356 return vmf->virtual_address;
360 #ifdef HAVE_VM_OPS_USE_VM_FAULT_ONLY
361 # define ll_filemap_fault(vma, vmf) filemap_fault(vmf)
363 # define ll_filemap_fault(vma, vmf) filemap_fault(vma, vmf)
366 #ifndef HAVE_CURRENT_TIME
367 static inline struct timespec current_time(struct inode *inode)
375 * time_after32 - compare two 32-bit relative times
376 * @a: the time which may be after @b
377 * @b: the time which may be before @a
379 * Needed for kernels earlier than v4.14-rc1~134^2
381 * time_after32(a, b) returns true if the time @a is after time @b.
382 * time_before32(b, a) returns true if the time @b is before time @a.
384 * Similar to time_after(), compare two 32-bit timestamps for relative
385 * times. This is useful for comparing 32-bit seconds values that can't
386 * be converted to 64-bit values (e.g. due to disk format or wire protocol
387 * issues) when it is known that the times are less than 68 years apart.
389 #define time_after32(a, b) ((s32)((u32)(b) - (u32)(a)) < 0)
390 #define time_before32(b, a) time_after32(a, b)
395 #define smp_store_mb(var, value) set_mb(var, value)
398 #if IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY)
399 static inline unsigned short blk_integrity_interval(struct blk_integrity *bi)
401 #ifdef HAVE_INTERVAL_EXP_BLK_INTEGRITY
402 return bi->interval_exp ? 1 << bi->interval_exp : 0;
403 #elif defined(HAVE_INTERVAL_BLK_INTEGRITY)
406 return bi->sector_size;
407 #endif /* !HAVE_INTERVAL_EXP_BLK_INTEGRITY */
410 static inline const char *blk_integrity_name(struct blk_integrity *bi)
412 #ifdef HAVE_INTERVAL_EXP_BLK_INTEGRITY
413 return bi->profile->name;
419 static inline unsigned int bip_size(struct bio_integrity_payload *bip)
421 #ifdef HAVE_BIP_ITER_BIO_INTEGRITY_PAYLOAD
422 return bip->bip_iter.bi_size;
424 return bip->bip_size;
427 #else /* !CONFIG_BLK_DEV_INTEGRITY */
428 static inline unsigned short blk_integrity_interval(struct blk_integrity *bi)
432 static inline const char *blk_integrity_name(struct blk_integrity *bi)
434 /* gcc8 dislikes when strcmp() is called against NULL */
437 #endif /* !CONFIG_BLK_DEV_INTEGRITY */
439 #ifndef INTEGRITY_FLAG_READ
440 #define INTEGRITY_FLAG_READ BLK_INTEGRITY_VERIFY
443 #ifndef INTEGRITY_FLAG_WRITE
444 #define INTEGRITY_FLAG_WRITE BLK_INTEGRITY_GENERATE
447 static inline bool bdev_integrity_enabled(struct block_device *bdev, int rw)
449 #if IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY)
450 struct blk_integrity *bi = bdev_get_integrity(bdev);
455 #ifdef HAVE_INTERVAL_EXP_BLK_INTEGRITY
456 if (rw == 0 && bi->profile->verify_fn != NULL &&
457 (bi->flags & INTEGRITY_FLAG_READ))
460 if (rw == 1 && bi->profile->generate_fn != NULL &&
461 (bi->flags & INTEGRITY_FLAG_WRITE))
464 if (rw == 0 && bi->verify_fn != NULL &&
465 (bi->flags & INTEGRITY_FLAG_READ))
468 if (rw == 1 && bi->generate_fn != NULL &&
469 (bi->flags & INTEGRITY_FLAG_WRITE))
471 #endif /* !HAVE_INTERVAL_EXP_BLK_INTEGRITY */
472 #endif /* !CONFIG_BLK_DEV_INTEGRITY */
477 #ifdef HAVE_PAGEVEC_INIT_ONE_PARAM
478 #define ll_pagevec_init(pvec, n) pagevec_init(pvec)
480 #define ll_pagevec_init(pvec, n) pagevec_init(pvec, n)
484 # define ll_d_count(d) d_count(d)
486 # define ll_d_count(d) ((d)->d_count)
487 #endif /* HAVE_D_COUNT */
489 #ifndef HAVE_IN_COMPAT_SYSCALL
490 #define in_compat_syscall is_compat_task
494 #define page_tree i_pages
496 #define i_pages tree_lock
499 #ifndef xa_lock_irqsave
500 #define xa_lock_irqsave(lockp, flags) spin_lock_irqsave(lockp, flags)
501 #define xa_unlock_irqrestore(lockp, flags) spin_unlock_irqrestore(lockp, flags)
504 #ifndef HAVE_LOCK_PAGE_MEMCG
505 #define lock_page_memcg(page) do {} while (0)
506 #define unlock_page_memcg(page) do {} while (0)
509 #ifndef KMEM_CACHE_USERCOPY
510 #define kmem_cache_create_usercopy(name, size, align, flags, useroffset, \
512 kmem_cache_create(name, size, align, flags, ctor)
515 #ifndef HAVE_LINUX_SELINUX_IS_ENABLED
516 #define selinux_is_enabled() 1
519 static inline int ll_vfs_getxattr(struct dentry *dentry, struct inode *inode,
521 void *value, size_t size)
523 #ifndef HAVE_VFS_SETXATTR
524 if (unlikely(!inode->i_op->getxattr))
527 return inode->i_op->getxattr(dentry, name, value, size);
529 return __vfs_getxattr(dentry, inode, name, value, size);
533 static inline int ll_vfs_setxattr(struct dentry *dentry, struct inode *inode,
535 const void *value, size_t size, int flags)
537 #ifndef HAVE_VFS_SETXATTR
538 if (unlikely(!inode->i_op->setxattr))
541 return inode->i_op->setxattr(dentry, name, value, size, flags);
543 return __vfs_setxattr(dentry, inode, name, value, size, flags);
547 static inline int ll_vfs_removexattr(struct dentry *dentry, struct inode *inode,
550 #ifndef HAVE_VFS_SETXATTR
551 if (unlikely(!inode->i_op->setxattr))
554 return inode->i_op->removexattr(dentry, name);
556 return __vfs_removexattr(dentry, name);
560 #endif /* _LUSTRE_COMPAT_H */