Whamcloud - gitweb
LU-12262 llite: harden ll_sbi ll_flags
[fs/lustre-release.git] / libcfs / libcfs / linux / linux-prim.c
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) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2013, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  */
31
32 #define DEBUG_SUBSYSTEM S_LNET
33
34 #include <linux/module.h>
35 #include <linux/kernel.h>
36 #include <linux/fs.h>
37 #include <linux/sched.h>
38 #ifdef HAVE_SCHED_HEADERS
39 #include <linux/sched/mm.h>
40 #endif
41 #include <linux/slab.h>
42 #include <linux/uaccess.h>
43 #include <net/netlink.h>
44
45 #if defined(CONFIG_KGDB)
46 #include <asm/kgdb.h>
47 #endif
48
49 #include <libcfs/linux/linux-time.h>
50 #include <libcfs/linux/linux-wait.h>
51 #include <libcfs/linux/linux-misc.h>
52
53 #ifndef HAVE_KTIME_GET_TS64
54 void ktime_get_ts64(struct timespec64 *ts)
55 {
56         struct timespec now;
57
58         ktime_get_ts(&now);
59         *ts = timespec_to_timespec64(now);
60 }
61 EXPORT_SYMBOL(ktime_get_ts64);
62 #endif /* HAVE_KTIME_GET_TS64 */
63
64 #ifndef HAVE_KTIME_GET_REAL_TS64
65 void ktime_get_real_ts64(struct timespec64 *ts)
66 {
67         struct timespec now;
68
69         getnstimeofday(&now);
70         *ts = timespec_to_timespec64(now);
71 }
72 EXPORT_SYMBOL(ktime_get_real_ts64);
73 #endif /* HAVE_KTIME_GET_REAL_TS64 */
74
75 #ifndef HAVE_KTIME_GET_REAL_SECONDS
76 /*
77  * Get the seconds portion of CLOCK_REALTIME (wall clock).
78  * This is the clock that can be altered by NTP and is
79  * independent of a reboot.
80  */
81 time64_t ktime_get_real_seconds(void)
82 {
83         return (time64_t)get_seconds();
84 }
85 EXPORT_SYMBOL(ktime_get_real_seconds);
86 #endif /* HAVE_KTIME_GET_REAL_SECONDS */
87
88 #ifndef HAVE_KTIME_GET_SECONDS
89 /*
90  * Get the seconds portion of CLOCK_MONOTONIC
91  * This clock is immutable and is reset across
92  * reboots. For older platforms this is a
93  * wrapper around get_seconds which is valid
94  * until 2038. By that time this will be gone
95  * one would hope.
96  */
97 time64_t ktime_get_seconds(void)
98 {
99         struct timespec64 now;
100
101         ktime_get_ts64(&now);
102         return now.tv_sec;
103 }
104 EXPORT_SYMBOL(ktime_get_seconds);
105 #endif /* HAVE_KTIME_GET_SECONDS */
106
107 static int (*cfs_apply_workqueue_attrs_t)(struct workqueue_struct *wq,
108                                           const struct workqueue_attrs *attrs);
109
110 int cfs_apply_workqueue_attrs(struct workqueue_struct *wq,
111                               const struct workqueue_attrs *attrs)
112 {
113         if (cfs_apply_workqueue_attrs_t)
114                 return cfs_apply_workqueue_attrs_t(wq, attrs);
115         return 0;
116 }
117 EXPORT_SYMBOL_GPL(cfs_apply_workqueue_attrs);
118
119 #ifndef HAVE_XARRAY_SUPPORT
120 struct kmem_cache (*radix_tree_node_cachep);
121 #endif
122
123 void __init cfs_arch_init(void)
124 {
125 #ifndef HAVE_WAIT_VAR_EVENT
126         wait_bit_init();
127 #endif
128         cfs_apply_workqueue_attrs_t =
129                 (void *)cfs_kallsyms_lookup_name("apply_workqueue_attrs");
130 #ifndef HAVE_XARRAY_SUPPORT
131         radix_tree_node_cachep =
132                 (void *)cfs_kallsyms_lookup_name("radix_tree_node_cachep");
133 #endif
134 }
135
136 int cfs_kernel_write(struct file *filp, const void *buf, size_t count,
137                      loff_t *pos)
138 {
139 #ifdef HAVE_NEW_KERNEL_WRITE
140         return kernel_write(filp, buf, count, pos);
141 #else
142         mm_segment_t __old_fs = get_fs();
143         int rc;
144
145         set_fs(KERNEL_DS);
146         rc = vfs_write(filp, (__force const char __user *)buf, count, pos);
147         set_fs(__old_fs);
148
149         return rc;
150 #endif
151 }
152 EXPORT_SYMBOL(cfs_kernel_write);
153
154 ssize_t cfs_kernel_read(struct file *file, void *buf, size_t count, loff_t *pos)
155 {
156 #ifdef HAVE_KERNEL_READ_LAST_POSP
157         return kernel_read(file, buf, count, pos);
158 #else
159         ssize_t size = kernel_read(file, *pos, buf, count);
160
161         if (size > 0)
162                 *pos += size;
163         return size;
164 #endif
165 }
166 EXPORT_SYMBOL(cfs_kernel_read);
167
168 #ifndef HAVE_KSET_FIND_OBJ
169 struct kobject *kset_find_obj(struct kset *kset, const char *name)
170 {
171         struct kobject *ret = NULL;
172         struct kobject *k;
173
174         spin_lock(&kset->list_lock);
175
176         list_for_each_entry(k, &kset->list, entry) {
177                 if (kobject_name(k) && !strcmp(kobject_name(k), name)) {
178                         if (kref_get_unless_zero(&k->kref))
179                                 ret = k;
180                         break;
181                 }
182         }
183
184         spin_unlock(&kset->list_lock);
185         return ret;
186 }
187 EXPORT_SYMBOL_GPL(kset_find_obj);
188 #endif
189
190 #ifndef HAVE_MATCH_WILDCARD
191 /**
192  * match_wildcard: - parse if a string matches given wildcard pattern
193  * @pattern: wildcard pattern
194  * @str: the string to be parsed
195  *
196  * Description: Parse the string @str to check if matches wildcard
197  * pattern @pattern. The pattern may contain two type wildcardes:
198  *   '*' - matches zero or more characters
199  *   '?' - matches one character
200  * If it's matched, return true, else return false.
201  */
202 bool match_wildcard(const char *pattern, const char *str)
203 {
204         const char *s = str;
205         const char *p = pattern;
206         bool star = false;
207
208         while (*s) {
209                 switch (*p) {
210                 case '?':
211                         s++;
212                         p++;
213                         break;
214                 case '*':
215                         star = true;
216                         str = s;
217                         if (!*++p)
218                                 return true;
219                         pattern = p;
220                         break;
221                 default:
222                         if (*s == *p) {
223                                 s++;
224                                 p++;
225                         } else {
226                                 if (!star)
227                                         return false;
228                                 str++;
229                                 s = str;
230                                 p = pattern;
231                         }
232                         break;
233                 }
234         }
235
236         if (*p == '*')
237                 ++p;
238         return !*p;
239 }
240 EXPORT_SYMBOL(match_wildcard);
241 #endif /* !HAVE_MATCH_WILDCARD */
242
243 #ifndef HAVE_KSTRTOBOOL_FROM_USER
244 int kstrtobool_from_user(const char __user *s, size_t count, bool *res)
245 {
246         /* Longest string needed to differentiate, newline, terminator */
247         char buf[4];
248
249         count = min(count, sizeof(buf) - 1);
250         if (copy_from_user(buf, s, count))
251                 return -EFAULT;
252         buf[count] = '\0';
253         return strtobool(buf, res);
254 }
255 EXPORT_SYMBOL(kstrtobool_from_user);
256 #endif /* !HAVE_KSTRTOBOOL_FROM_USER */
257
258 #ifndef HAVE_NLA_STRDUP
259 char *nla_strdup(const struct nlattr *nla, gfp_t flags)
260 {
261         size_t srclen = nla_len(nla);
262         char *src = nla_data(nla), *dst;
263
264         if (srclen > 0 && src[srclen - 1] == '\0')
265                 srclen--;
266
267         dst = kmalloc(srclen + 1, flags);
268         if (dst != NULL) {
269                 memcpy(dst, src, srclen);
270                 dst[srclen] = '\0';
271         }
272         return dst;
273 }
274 EXPORT_SYMBOL(nla_strdup);
275 #endif /* !HAVE_NLA_STRDUP */