Whamcloud - gitweb
LU-13783 libcfs: add cfs_kallsyms_lookup_name()
[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/uaccess.h>
42
43 #if defined(CONFIG_KGDB)
44 #include <asm/kgdb.h>
45 #endif
46
47 #include <libcfs/linux/linux-time.h>
48 #include <libcfs/linux/linux-wait.h>
49 #include <libcfs/linux/linux-misc.h>
50
51 #ifndef HAVE_KTIME_GET_TS64
52 void ktime_get_ts64(struct timespec64 *ts)
53 {
54         struct timespec now;
55
56         ktime_get_ts(&now);
57         *ts = timespec_to_timespec64(now);
58 }
59 EXPORT_SYMBOL(ktime_get_ts64);
60 #endif /* HAVE_KTIME_GET_TS64 */
61
62 #ifndef HAVE_KTIME_GET_REAL_TS64
63 void ktime_get_real_ts64(struct timespec64 *ts)
64 {
65         struct timespec now;
66
67         getnstimeofday(&now);
68         *ts = timespec_to_timespec64(now);
69 }
70 EXPORT_SYMBOL(ktime_get_real_ts64);
71 #endif /* HAVE_KTIME_GET_REAL_TS64 */
72
73 #ifndef HAVE_KTIME_GET_REAL_SECONDS
74 /*
75  * Get the seconds portion of CLOCK_REALTIME (wall clock).
76  * This is the clock that can be altered by NTP and is
77  * independent of a reboot.
78  */
79 time64_t ktime_get_real_seconds(void)
80 {
81         return (time64_t)get_seconds();
82 }
83 EXPORT_SYMBOL(ktime_get_real_seconds);
84 #endif /* HAVE_KTIME_GET_REAL_SECONDS */
85
86 #ifndef HAVE_KTIME_GET_SECONDS
87 /*
88  * Get the seconds portion of CLOCK_MONOTONIC
89  * This clock is immutable and is reset across
90  * reboots. For older platforms this is a
91  * wrapper around get_seconds which is valid
92  * until 2038. By that time this will be gone
93  * one would hope.
94  */
95 time64_t ktime_get_seconds(void)
96 {
97         struct timespec64 now;
98
99         ktime_get_ts64(&now);
100         return now.tv_sec;
101 }
102 EXPORT_SYMBOL(ktime_get_seconds);
103 #endif /* HAVE_KTIME_GET_SECONDS */
104
105 static int (*cfs_apply_workqueue_attrs_t)(struct workqueue_struct *wq,
106                                           const struct workqueue_attrs *attrs);
107
108 int cfs_apply_workqueue_attrs(struct workqueue_struct *wq,
109                               const struct workqueue_attrs *attrs)
110 {
111         if (cfs_apply_workqueue_attrs_t)
112                 return cfs_apply_workqueue_attrs_t(wq, attrs);
113         return 0;
114 }
115 EXPORT_SYMBOL_GPL(cfs_apply_workqueue_attrs);
116
117 #ifndef HAVE_XARRAY_SUPPORT
118 struct kmem_cache (*radix_tree_node_cachep);
119 #endif
120
121 void __init cfs_arch_init(void)
122 {
123 #ifndef HAVE_WAIT_VAR_EVENT
124         wait_bit_init();
125 #endif
126         cfs_apply_workqueue_attrs_t =
127                 (void *)cfs_kallsyms_lookup_name("apply_workqueue_attrs");
128 #ifndef HAVE_XARRAY_SUPPORT
129         radix_tree_node_cachep =
130                 (void *)cfs_kallsyms_lookup_name("radix_tree_node_cachep");
131 #endif
132 }
133
134 int cfs_kernel_write(struct file *filp, const void *buf, size_t count,
135                      loff_t *pos)
136 {
137 #ifdef HAVE_NEW_KERNEL_WRITE
138         return kernel_write(filp, buf, count, pos);
139 #else
140         mm_segment_t __old_fs = get_fs();
141         int rc;
142
143         set_fs(KERNEL_DS);
144         rc = vfs_write(filp, (__force const char __user *)buf, count, pos);
145         set_fs(__old_fs);
146
147         return rc;
148 #endif
149 }
150 EXPORT_SYMBOL(cfs_kernel_write);
151
152 ssize_t cfs_kernel_read(struct file *file, void *buf, size_t count, loff_t *pos)
153 {
154 #ifdef HAVE_KERNEL_READ_LAST_POSP
155         return kernel_read(file, buf, count, pos);
156 #else
157         ssize_t size = kernel_read(file, *pos, buf, count);
158
159         if (size > 0)
160                 *pos += size;
161         return size;
162 #endif
163 }
164 EXPORT_SYMBOL(cfs_kernel_read);
165
166 #ifndef HAVE_KSET_FIND_OBJ
167 struct kobject *kset_find_obj(struct kset *kset, const char *name)
168 {
169         struct kobject *ret = NULL;
170         struct kobject *k;
171
172         spin_lock(&kset->list_lock);
173
174         list_for_each_entry(k, &kset->list, entry) {
175                 if (kobject_name(k) && !strcmp(kobject_name(k), name)) {
176                         if (kref_get_unless_zero(&k->kref))
177                                 ret = k;
178                         break;
179                 }
180         }
181
182         spin_unlock(&kset->list_lock);
183         return ret;
184 }
185 EXPORT_SYMBOL_GPL(kset_find_obj);
186 #endif
187
188 #ifndef HAVE_KSTRTOBOOL_FROM_USER
189 int kstrtobool_from_user(const char __user *s, size_t count, bool *res)
190 {
191         /* Longest string needed to differentiate, newline, terminator */
192         char buf[4];
193
194         count = min(count, sizeof(buf) - 1);
195         if (copy_from_user(buf, s, count))
196                 return -EFAULT;
197         buf[count] = '\0';
198         return strtobool(buf, res);
199 }
200 EXPORT_SYMBOL(kstrtobool_from_user);
201 #endif /* !HAVE_KSTRTOBOOL_FROM_USER */