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) 2008, 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/
32 #ifndef __LIBCFS_LINUX_MISC_H__
33 #define __LIBCFS_LINUX_MISC_H__
36 /* Since Commit 2f8b544477e6 ("block,fs: untangle fs.h and blk_types.h")
37 * fs.h doesn't include blk_types.h, but we need it.
39 #include <linux/blk_types.h>
40 #include <linux/mutex.h>
41 #include <linux/user_namespace.h>
42 #include <linux/uio.h>
43 #include <linux/kallsyms.h>
46 * Since 4.20 commit 00e23707442a75b404392cef1405ab4fd498de6b
47 * iov_iter: Use accessor functions to access an iterator's type and direction.
48 * iter_is_iovec() and iov_iter_is_* are available, supply the missing
49 * functionality for older kernels.
51 #ifndef HAVE_IOV_ITER_TYPE
53 * Since 3.15-rc4 commit 71d8e532b1549a478e6a6a8a44f309d050294d00
54 * The iov iterator has a type and can iterate over numerous vector types.
55 * Prior to this only iovec is supported, so all iov_iter_is_* are false.
57 #ifdef HAVE_IOV_ITER_HAS_TYPE_MEMBER
58 #define iter_is_iovec(iter) ((iter)->type & ITER_IOVEC)
59 #define iov_iter_is_kvec(iter) ((iter)->type & ITER_KVEC)
60 #define iov_iter_is_bvec(iter) ((iter)->type & ITER_BVEC)
61 #define iov_iter_is_pipe(iter) ((iter)->type & ITER_PIPE)
62 #define iov_iter_is_discard(iter) ((iter)->type & ITER_DISCARD)
64 #define iter_is_iovec(iter) 1
65 #define iov_iter_is_kvec(iter) 0
66 #define iov_iter_is_bvec(iter) 0
67 #define iov_iter_is_pipe(iter) 0
68 #define iov_iter_is_discard(iter) 0
70 #endif /* HAVE_IOV_ITER_TYPE */
72 int cfs_kernel_write(struct file *filp, const void *buf, size_t count,
74 ssize_t cfs_kernel_read(struct file *file, void *buf, size_t count,
78 * For RHEL6 struct kernel_parm_ops doesn't exist. Also
79 * the arguments for .set and .get take different
80 * parameters which is handled below
82 #ifdef HAVE_KERNEL_PARAM_OPS
83 #define cfs_kernel_param_arg_t const struct kernel_param
85 #define cfs_kernel_param_arg_t struct kernel_param_ops
86 #define kernel_param_ops kernel_param
87 #endif /* ! HAVE_KERNEL_PARAM_OPS */
89 #ifndef HAVE_KERNEL_PARAM_LOCK
90 static inline void kernel_param_unlock(struct module *mod)
92 __kernel_param_unlock();
95 static inline void kernel_param_lock(struct module *mod)
97 __kernel_param_lock();
99 #endif /* ! HAVE_KERNEL_PARAM_LOCK */
101 int cfs_apply_workqueue_attrs(struct workqueue_struct *wq,
102 const struct workqueue_attrs *attrs);
104 #ifndef HAVE_KSTRTOBOOL_FROM_USER
106 #define kstrtobool strtobool
108 int kstrtobool_from_user(const char __user *s, size_t count, bool *res);
109 #endif /* HAVE_KSTRTOBOOL_FROM_USER */
111 #ifndef HAVE_MATCH_WILDCARD
112 bool match_wildcard(const char *pattern, const char *str);
113 #endif /* !HAVE_MATCH_WILDCARD */
115 #ifndef HAVE_KREF_READ
116 static inline int kref_read(const struct kref *kref)
118 return atomic_read(&kref->refcount);
120 #endif /* HAVE_KREF_READ */
122 int cfs_arch_init(void);
123 void cfs_arch_exit(void);
125 #ifndef container_of_safe
127 * container_of_safe - cast a member of a structure out to the containing structure
128 * @ptr: the pointer to the member.
129 * @type: the type of the container struct this is embedded in.
130 * @member: the name of the member within the struct.
132 * If IS_ERR_OR_NULL(ptr), ptr is returned unchanged.
134 * Note: Copied from Linux 5.6, with BUILD_BUG_ON_MSG section removed.
136 #define container_of_safe(ptr, type, member) ({ \
137 void *__mptr = (void *)(ptr); \
138 IS_ERR_OR_NULL(__mptr) ? ERR_CAST(__mptr) : \
139 ((type *)(__mptr - offsetof(type, member))); })
143 * Linux v4.15-rc2-5-g4229a470175b added sizeof_field()
144 * Linux v5.5-rc4-1-g1f07dcc459d5 removed FIELD_SIZEOF()
145 * Proved a sizeof_field in terms of FIELD_SIZEOF() when one is not provided
148 #define sizeof_field(type, member) FIELD_SIZEOF(type, member)
151 #ifndef HAVE_TASK_IS_RUNNING
152 #define task_is_running(task) (task->state == TASK_RUNNING)
156 #ifdef HAVE_INTERVAL_TREE_CACHED
157 #define interval_tree_root rb_root_cached
158 #define interval_tree_first rb_first_cached
159 #define INTERVAL_TREE_ROOT RB_ROOT_CACHED
161 #define interval_tree_root rb_root
162 #define interval_tree_first rb_first
163 #define INTERVAL_TREE_ROOT RB_ROOT
164 #endif /* HAVE_INTERVAL_TREE_CACHED */
166 /* Linux v5.1-rc5 214d8ca6ee ("stacktrace: Provide common infrastructure")
167 * CONFIG_ARCH_STACKWALK indicates that save_stack_trace_tsk symbol is not
168 * exported. Use symbol_get() to find if save_stack_trace_tsk is available.
170 #ifdef CONFIG_ARCH_STACKWALK
171 int cfs_stack_trace_save_tsk(struct task_struct *task, unsigned long *store,
172 unsigned int size, unsigned int skipnr);
175 #ifndef memset_startat
176 /** from linux 5.19 include/linux/string.h: */
177 #define memset_startat(obj, v, member) \
179 u8 *__ptr = (u8 *)(obj); \
180 typeof(v) __val = (v); \
181 memset(__ptr + offsetof(typeof(*(obj)), member), __val, \
182 sizeof(*(obj)) - offsetof(typeof(*(obj)), member)); \
184 #endif /* memset_startat() */
186 #ifdef HAVE_KALLSYMS_LOOKUP_NAME
187 static inline void *cfs_kallsyms_lookup_name(const char *name)
189 return (void *)kallsyms_lookup_name(name);
192 static inline void *cfs_kallsyms_lookup_name(const char *name)
199 #define strscpy(s1, s2, sz) strlcpy((s1), (s2), (sz))
202 #ifndef HAVE_BITMAP_TO_ARR32
203 void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap, unsigned int nbits);
206 #ifndef HAVE_KOBJ_TYPE_DEFAULT_GROUPS
207 #define default_groups default_attrs
208 #define KOBJ_ATTR_GROUPS(_name) _name##_attrs
209 #define KOBJ_ATTRIBUTE_GROUPS(_name)
211 #define KOBJ_ATTR_GROUPS(_name) _name##_groups
212 #define KOBJ_ATTRIBUTE_GROUPS(_name) ATTRIBUTE_GROUPS(_name)
215 #endif /* __LIBCFS_LINUX_MISC_H__ */