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