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/bio.h>
42 #include <linux/xattr.h>
44 #include <libcfs/linux/linux-fs.h>
45 #include <lustre_patchless_compat.h>
46 #include <obd_support.h>
48 #ifdef HAVE_FS_STRUCT_RWLOCK
49 # define LOCK_FS_STRUCT(fs) write_lock(&(fs)->lock)
50 # define UNLOCK_FS_STRUCT(fs) write_unlock(&(fs)->lock)
52 # define LOCK_FS_STRUCT(fs) spin_lock(&(fs)->lock)
53 # define UNLOCK_FS_STRUCT(fs) spin_unlock(&(fs)->lock)
56 static inline void ll_set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
57 struct dentry *dentry)
75 * set ATTR_BLOCKS to a high value to avoid any risk of collision with other
76 * ATTR_* attributes (see bug 13828)
78 #define ATTR_BLOCKS (1 << 27)
80 #define current_ngroups current_cred()->group_info->ngroups
81 #define current_groups current_cred()->group_info->small_block
84 * OBD need working random driver, thus all our
85 * initialization routines must be called after device
86 * driver initialization
90 #define module_init(a) late_initcall(a)
93 #ifndef MODULE_ALIAS_FS
94 #define MODULE_ALIAS_FS(name)
97 #define LTIME_S(time) (time.tv_sec)
99 #ifdef HAVE_GENERIC_PERMISSION_2ARGS
100 # define ll_generic_permission(inode, mask, flags, check_acl) \
101 generic_permission(inode, mask)
102 #elif defined HAVE_GENERIC_PERMISSION_4ARGS
103 # define ll_generic_permission(inode, mask, flags, check_acl) \
104 generic_permission(inode, mask, flags, check_acl)
106 # define ll_generic_permission(inode, mask, flags, check_acl) \
107 generic_permission(inode, mask, check_acl)
110 #ifdef HAVE_4ARGS_VFS_SYMLINK
111 #define ll_vfs_symlink(dir, dentry, mnt, path, mode) \
112 vfs_symlink(dir, dentry, path, mode)
114 #define ll_vfs_symlink(dir, dentry, mnt, path, mode) \
115 vfs_symlink(dir, dentry, path)
118 #if !defined(HAVE_FILE_LLSEEK_SIZE) || defined(HAVE_FILE_LLSEEK_SIZE_5ARGS)
119 #define ll_generic_file_llseek_size(file, offset, origin, maxbytes, eof) \
120 generic_file_llseek_size(file, offset, origin, maxbytes, eof);
122 #define ll_generic_file_llseek_size(file, offset, origin, maxbytes, eof) \
123 generic_file_llseek_size(file, offset, origin, maxbytes);
126 #ifdef HAVE_INODE_DIO_WAIT
127 /* inode_dio_wait(i) use as-is for write lock */
128 # define inode_dio_write_done(i) do {} while (0) /* for write unlock */
130 # define inode_dio_wait(i) down_write(&(i)->i_alloc_sem)
131 # define inode_dio_write_done(i) up_write(&(i)->i_alloc_sem)
134 #ifndef FS_HAS_FIEMAP
135 #define FS_HAS_FIEMAP (0)
138 #ifndef HAVE_SIMPLE_SETATTR
139 #define simple_setattr(dentry, ops) inode_setattr((dentry)->d_inode, ops)
142 #ifndef SLAB_DESTROY_BY_RCU
143 #define SLAB_DESTROY_BY_RCU 0
146 #ifndef HAVE_DQUOT_SUSPEND
147 # define ll_vfs_dq_init vfs_dq_init
148 # define ll_vfs_dq_drop vfs_dq_drop
149 # define ll_vfs_dq_transfer vfs_dq_transfer
150 # define ll_vfs_dq_off(sb, remount) vfs_dq_off(sb, remount)
152 # define ll_vfs_dq_init dquot_initialize
153 # define ll_vfs_dq_drop dquot_drop
154 # define ll_vfs_dq_transfer dquot_transfer
155 # define ll_vfs_dq_off(sb, remount) dquot_suspend(sb, -1)
158 #ifndef HAVE_BLKDEV_GET_BY_DEV
159 # define blkdev_get_by_dev(dev, mode, holder) open_by_devnum(dev, mode)
162 #ifdef HAVE_BVEC_ITER
163 #define bio_idx(bio) (bio->bi_iter.bi_idx)
164 #define bio_set_sector(bio, sector) (bio->bi_iter.bi_sector = sector)
165 #define bvl_to_page(bvl) (bvl->bv_page)
167 #define bio_idx(bio) (bio->bi_idx)
168 #define bio_set_sector(bio, sector) (bio->bi_sector = sector)
169 #define bio_sectors(bio) ((bio)->bi_size >> 9)
170 #ifndef HAVE_BIO_END_SECTOR
171 #define bio_end_sector(bio) (bio->bi_sector + bio_sectors(bio))
173 #define bvl_to_page(bvl) (bvl->bv_page)
176 #ifndef HAVE_BLK_QUEUE_MAX_SEGMENTS
177 #define blk_queue_max_segments(rq, seg) \
178 do { blk_queue_max_phys_segments(rq, seg); \
179 blk_queue_max_hw_segments(rq, seg); } while (0)
181 #define queue_max_phys_segments(rq) queue_max_segments(rq)
182 #define queue_max_hw_segments(rq) queue_max_segments(rq)
186 #define DECLARE_PLUG(plug) struct blk_plug plug
187 #else /* !HAVE_BLK_PLUG */
188 #define DECLARE_PLUG(name)
189 #define blk_start_plug(plug) do {} while (0)
190 #define blk_finish_plug(plug) do {} while (0)
193 #ifdef HAVE_KMAP_ATOMIC_HAS_1ARG
194 #define ll_kmap_atomic(a, b) kmap_atomic(a)
195 #define ll_kunmap_atomic(a, b) kunmap_atomic(a)
197 #define ll_kmap_atomic(a, b) kmap_atomic(a, b)
198 #define ll_kunmap_atomic(a, b) kunmap_atomic(a, b)
201 #ifndef HAVE_CLEAR_INODE
202 #define clear_inode(i) end_writeback(i)
205 #ifndef HAVE_DENTRY_D_CHILD
206 #define d_child d_u.d_child
209 #ifdef HAVE_DENTRY_D_U_D_ALIAS
210 #define d_alias d_u.d_alias
213 #ifndef DATA_FOR_LLITE_IS_LIST
214 #define ll_d_hlist_node hlist_node
215 #define ll_d_hlist_empty(list) hlist_empty(list)
216 #define ll_d_hlist_entry(ptr, type, name) hlist_entry(ptr.first, type, name)
217 #define ll_d_hlist_for_each(tmp, i_dentry) hlist_for_each(tmp, i_dentry)
218 # ifdef HAVE_HLIST_FOR_EACH_3ARG
219 # define ll_d_hlist_for_each_entry(dentry, p, i_dentry) \
220 p = NULL; hlist_for_each_entry(dentry, i_dentry, d_alias)
222 # define ll_d_hlist_for_each_entry(dentry, p, i_dentry) \
223 hlist_for_each_entry(dentry, p, i_dentry, d_alias)
225 #define DECLARE_LL_D_HLIST_NODE_PTR(name) struct ll_d_hlist_node *name
227 #define ll_d_hlist_node list_head
228 #define ll_d_hlist_empty(list) list_empty(list)
229 #define ll_d_hlist_entry(ptr, type, name) list_entry(ptr.next, type, name)
230 #define ll_d_hlist_for_each(tmp, i_dentry) list_for_each(tmp, i_dentry)
231 #define ll_d_hlist_for_each_entry(dentry, p, i_dentry) \
232 list_for_each_entry(dentry, i_dentry, d_alias)
233 #define DECLARE_LL_D_HLIST_NODE_PTR(name) /* nothing */
234 #endif /* !DATA_FOR_LLITE_IS_LIST */
240 # define NO_QUOTA (-EDQUOT)
244 #define SEEK_DATA 3 /* seek to the next data */
247 #define SEEK_HOLE 4 /* seek to the next hole */
250 #ifndef FMODE_UNSIGNED_OFFSET
251 #define FMODE_UNSIGNED_OFFSET ((__force fmode_t)0x2000)
254 #if !defined(_ASM_GENERIC_BITOPS_EXT2_NON_ATOMIC_H_) && !defined(ext2_set_bit)
255 # define ext2_set_bit __test_and_set_bit_le
256 # define ext2_clear_bit __test_and_clear_bit_le
257 # define ext2_test_bit test_bit_le
258 # define ext2_find_first_zero_bit find_first_zero_bit_le
259 # define ext2_find_next_zero_bit find_next_zero_bit_le
262 #ifdef ATTR_TIMES_SET
263 # define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)
265 # define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET)
268 #ifndef XATTR_NAME_POSIX_ACL_ACCESS
269 # define XATTR_NAME_POSIX_ACL_ACCESS POSIX_ACL_XATTR_ACCESS
272 #ifndef XATTR_NAME_POSIX_ACL_DEFAULT
273 # define XATTR_NAME_POSIX_ACL_DEFAULT POSIX_ACL_XATTR_DEFAULT
276 #ifndef HAVE_LM_XXX_LOCK_MANAGER_OPS
277 # define lm_compare_owner fl_compare_owner
281 * After 3.1, kernel's nameidata.intent.open.flags is different
282 * with lustre's lookup_intent.it_flags, as lustre's it_flags'
283 * lower bits equal to FMODE_xxx while kernel doesn't transliterate
284 * lower bits of nameidata.intent.open.flags to FMODE_xxx.
286 #include <linux/version.h>
287 static inline int ll_namei_to_lookup_intent_flag(int flag)
289 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 1, 0)
290 flag = (flag & ~O_ACCMODE) | OPEN_FMODE(flag);
295 #include <linux/fs.h>
296 #ifndef HAVE_PROTECT_I_NLINK
297 static inline void set_nlink(struct inode *inode, unsigned int nlink)
299 inode->i_nlink = nlink;
303 #ifdef HAVE_INODEOPS_USE_UMODE_T
304 # define ll_umode_t umode_t
306 # define ll_umode_t int
309 #include <linux/dcache.h>
310 #ifndef HAVE_D_MAKE_ROOT
311 static inline struct dentry *d_make_root(struct inode *root)
313 struct dentry *res = d_alloc_root(root);
315 if (res == NULL && root)
322 #ifdef HAVE_DIRTY_INODE_HAS_FLAG
323 # define ll_dirty_inode(inode, flag) (inode)->i_sb->s_op->dirty_inode((inode), flag)
325 # define ll_dirty_inode(inode, flag) (inode)->i_sb->s_op->dirty_inode((inode))
328 #ifdef HAVE_FILE_F_INODE
329 # define set_file_inode(file, inode) (file)->f_inode = inode
331 # define set_file_inode(file, inode)
334 #ifndef HAVE_FILE_INODE
335 static inline struct inode *file_inode(const struct file *file)
337 return file->f_path.dentry->d_inode;
341 #ifdef HAVE_OLDSIZE_TRUNCATE_PAGECACHE
342 #define ll_truncate_pagecache(inode, size) truncate_pagecache(inode, 0, size)
344 #define ll_truncate_pagecache(inode, size) truncate_pagecache(inode, size)
347 #ifdef HAVE_VFS_RENAME_5ARGS
348 #define ll_vfs_rename(a, b, c, d) vfs_rename(a, b, c, d, NULL)
349 #elif defined HAVE_VFS_RENAME_6ARGS
350 #define ll_vfs_rename(a, b, c, d) vfs_rename(a, b, c, d, NULL, 0)
352 #define ll_vfs_rename(a, b, c, d) vfs_rename(a, b, c, d)
355 #ifdef HAVE_VFS_UNLINK_3ARGS
356 #define ll_vfs_unlink(a, b) vfs_unlink(a, b, NULL)
358 #define ll_vfs_unlink(a, b) vfs_unlink(a, b)
361 #ifndef HAVE_INODE_LOCK
362 # define inode_lock(inode) mutex_lock(&(inode)->i_mutex)
363 # define inode_unlock(inode) mutex_unlock(&(inode)->i_mutex)
364 # define inode_trylock(inode) mutex_trylock(&(inode)->i_mutex)
367 #ifndef HAVE_RADIX_EXCEPTION_ENTRY
368 static inline int radix_tree_exceptional_entry(void *arg)
374 #ifndef HAVE_TRUNCATE_INODE_PAGES_FINAL
375 static inline void truncate_inode_pages_final(struct address_space *map)
377 truncate_inode_pages(map, 0);
378 /* Workaround for LU-118 */
380 spin_lock_irq(&map->tree_lock);
381 spin_unlock_irq(&map->tree_lock);
382 } /* Workaround end */
387 #define SIZE_MAX (~(size_t)0)
390 #ifdef HAVE_SECURITY_IINITSEC_CALLBACK
391 # define ll_security_inode_init_security(inode, dir, name, value, len, \
392 initxattrs, dentry) \
393 security_inode_init_security(inode, dir, &((dentry)->d_name), \
395 #elif defined HAVE_SECURITY_IINITSEC_QSTR
396 # define ll_security_inode_init_security(inode, dir, name, value, len, \
397 initxattrs, dentry) \
398 security_inode_init_security(inode, dir, &((dentry)->d_name), \
400 #else /* !HAVE_SECURITY_IINITSEC_CALLBACK && !HAVE_SECURITY_IINITSEC_QSTR */
401 # define ll_security_inode_init_security(inode, dir, name, value, len, \
402 initxattrs, dentry) \
403 security_inode_init_security(inode, dir, name, value, len)
406 #ifndef bio_for_each_segment_all /* since kernel version 3.9 */
407 #ifdef HAVE_BVEC_ITER
408 #define bio_for_each_segment_all(bv, bio, it) \
409 for (it = 0, bv = (bio)->bi_io_vec; it < (bio)->bi_vcnt; it++, bv++)
411 #define bio_for_each_segment_all(bv, bio, it) bio_for_each_segment(bv, bio, it)
415 #ifdef HAVE_PID_NS_FOR_CHILDREN
416 # define ll_task_pid_ns(task) ((task)->nsproxy->pid_ns_for_children)
418 # define ll_task_pid_ns(task) ((task)->nsproxy->pid_ns)
421 #ifdef HAVE_FULL_NAME_HASH_3ARGS
422 # define ll_full_name_hash(salt, name, len) full_name_hash(salt, name, len)
424 # define ll_full_name_hash(salt, name, len) full_name_hash(name, len)
427 #ifdef HAVE_STRUCT_POSIX_ACL_XATTR
428 # define posix_acl_xattr_header struct posix_acl_xattr_header
429 # define posix_acl_xattr_entry struct posix_acl_xattr_entry
430 # define GET_POSIX_ACL_XATTR_ENTRY(head) ((void *)((head) + 1))
432 # define GET_POSIX_ACL_XATTR_ENTRY(head) ((head)->a_entries)
435 #ifdef HAVE_IOP_XATTR
436 #ifdef HAVE_XATTR_HANDLER_FLAGS
437 #define ll_setxattr generic_setxattr
438 #define ll_getxattr generic_getxattr
439 #define ll_removexattr generic_removexattr
441 int ll_setxattr(struct dentry *dentry, const char *name,
442 const void *value, size_t size, int flags);
443 ssize_t ll_getxattr(struct dentry *dentry, const char *name,
444 void *buf, size_t buf_size);
445 int ll_removexattr(struct dentry *dentry, const char *name);
446 #endif /* ! HAVE_XATTR_HANDLER_FLAGS */
447 #endif /* HAVE_IOP_XATTR */
449 #ifndef HAVE_VFS_SETXATTR
450 const struct xattr_handler *get_xattr_type(const char *name);
452 #ifdef HAVE_XATTR_HANDLER_FLAGS
454 __vfs_setxattr(struct dentry *dentry, struct inode *inode, const char *name,
455 const void *value, size_t size, int flags)
457 const struct xattr_handler *handler;
460 handler = get_xattr_type(name);
464 #if defined(HAVE_XATTR_HANDLER_INODE_PARAM)
465 rc = handler->set(handler, dentry, inode, name, value, size,
467 #elif defined(HAVE_XATTR_HANDLER_SIMPLIFIED)
468 rc = handler->set(handler, dentry, name, value, size, XATTR_CREATE);
470 rc = handler->set(dentry, name, value, size, XATTR_CREATE,
472 #endif /* !HAVE_XATTR_HANDLER_INODE_PARAM */
475 #else /* !HAVE_XATTR_HANDLER_FLAGS */
477 __vfs_setxattr(struct dentry *dentry, struct inode *inode, const char *name,
478 const void *value, size_t size, int flags)
480 return ll_setxattr(dentry, name, value, size, flags);
482 #endif /* HAVE_XATTR_HANDLER_FLAGS */
483 #endif /* HAVE_VFS_SETXATTR */
485 #ifdef HAVE_IOP_SET_ACL
486 #ifdef CONFIG_FS_POSIX_ACL
487 #ifndef HAVE_POSIX_ACL_UPDATE_MODE
488 static inline int posix_acl_update_mode(struct inode *inode, umode_t *mode_p,
489 struct posix_acl **acl)
491 umode_t mode = inode->i_mode;
494 error = posix_acl_equiv_mode(*acl, &mode);
499 if (!in_group_p(inode->i_gid) &&
500 !capable_wrt_inode_uidgid(inode, CAP_FSETID))
505 #endif /* HAVE_POSIX_ACL_UPDATE_MODE */
509 #ifndef HAVE_IOV_ITER_TRUNCATE
510 static inline void iov_iter_truncate(struct iov_iter *i, u64 count)
512 if (i->count > count)
518 static inline bool is_sxid(umode_t mode)
520 return (mode & S_ISUID) || ((mode & S_ISGID) && (mode & S_IXGRP));
525 #define IS_NOSEC(inode) (!is_sxid(inode->i_mode))
529 static inline void inode_has_no_xattr(struct inode *inode)
535 #ifndef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
536 static inline void iov_iter_reexpand(struct iov_iter *i, size_t count)
541 static inline struct iovec iov_iter_iovec(const struct iov_iter *iter)
543 return (struct iovec) {
544 .iov_base = iter->iov->iov_base + iter->iov_offset,
545 .iov_len = min(iter->count,
546 iter->iov->iov_len - iter->iov_offset),
550 #define iov_for_each(iov, iter, start) \
551 for (iter = (start); \
552 (iter).count && ((iov = iov_iter_iovec(&(iter))), 1); \
553 iov_iter_advance(&(iter), (iov).iov_len))
555 static inline ssize_t
556 generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
562 iov_for_each(iov, i, *iter) {
565 res = generic_file_aio_read(iocb, &iov, 1, iocb->ki_pos);
573 if (res < iov.iov_len)
578 iov_iter_advance(iter, bytes);
582 static inline ssize_t
583 __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *iter)
589 /* Since LLITE updates file size at the end of I/O in
590 * vvp_io_commit_write(), append write has to be done in atomic when
591 * there are multiple segments because otherwise each iteration to
592 * __generic_file_aio_write() will see original file size */
593 if (unlikely(iocb->ki_filp->f_flags & O_APPEND && iter->nr_segs > 1)) {
594 struct iovec *iov_copy;
597 OBD_ALLOC(iov_copy, sizeof(*iov_copy) * iter->nr_segs);
601 iov_for_each(iov, i, *iter)
602 iov_copy[count++] = iov;
604 bytes = __generic_file_aio_write(iocb, iov_copy, count,
606 OBD_FREE(iov_copy, sizeof(*iov_copy) * iter->nr_segs);
609 iov_iter_advance(iter, bytes);
613 iov_for_each(iov, i, *iter) {
616 res = __generic_file_aio_write(iocb, &iov, 1, &iocb->ki_pos);
624 if (res < iov.iov_len)
629 iov_iter_advance(iter, bytes);
632 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
634 static inline void __user *get_vmf_address(struct vm_fault *vmf)
636 #ifdef HAVE_VM_FAULT_ADDRESS
637 return (void __user *)vmf->address;
639 return vmf->virtual_address;
643 #ifdef HAVE_VM_OPS_USE_VM_FAULT_ONLY
644 # define ll_filemap_fault(vma, vmf) filemap_fault(vmf)
646 # define ll_filemap_fault(vma, vmf) filemap_fault(vma, vmf)
649 #ifndef HAVE_CURRENT_TIME
650 static inline struct timespec current_time(struct inode *inode)
658 * time_after32 - compare two 32-bit relative times
659 * @a: the time which may be after @b
660 * @b: the time which may be before @a
662 * time_after32(a, b) returns true if the time @a is after time @b.
663 * time_before32(b, a) returns true if the time @b is before time @a.
665 * Similar to time_after(), compare two 32-bit timestamps for relative
666 * times. This is useful for comparing 32-bit seconds values that can't
667 * be converted to 64-bit values (e.g. due to disk format or wire protocol
668 * issues) when it is known that the times are less than 68 years apart.
670 #define time_after32(a, b) ((s32)((u32)(b) - (u32)(a)) < 0)
671 #define time_before32(b, a) time_after32(a, b)
674 #endif /* _LUSTRE_COMPAT_H */