Whamcloud - gitweb
d9fd96b4da567f08ccd09bb57f4cac419016b42d
[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 #include <linux/module.h>
35 #include <linux/kernel.h>
36 #include <linux/fs.h>
37 #include <linux/fs_struct.h>
38 #include <linux/sched.h>
39 #ifdef HAVE_SCHED_HEADERS
40 #include <linux/sched/signal.h>
41 #include <linux/sched/mm.h>
42 #endif
43 #include <linux/uaccess.h>
44 #include <libcfs/libcfs.h>
45
46 #if defined(CONFIG_KGDB)
47 #include <asm/kgdb.h>
48 #endif
49
50 #ifndef HAVE_KTIME_GET_TS64
51 void ktime_get_ts64(struct timespec64 *ts)
52 {
53         struct timespec now;
54
55         ktime_get_ts(&now);
56         *ts = timespec_to_timespec64(now);
57 }
58 EXPORT_SYMBOL(ktime_get_ts64);
59 #endif /* HAVE_KTIME_GET_TS64 */
60
61 #ifndef HAVE_KTIME_GET_REAL_TS64
62 void ktime_get_real_ts64(struct timespec64 *ts)
63 {
64         struct timespec now;
65
66         getnstimeofday(&now);
67         *ts = timespec_to_timespec64(now);
68 }
69 EXPORT_SYMBOL(ktime_get_real_ts64);
70 #endif /* HAVE_KTIME_GET_REAL_TS64 */
71
72 #ifndef HAVE_KTIME_GET_REAL_SECONDS
73 /*
74  * Get the seconds portion of CLOCK_REALTIME (wall clock).
75  * This is the clock that can be altered by NTP and is
76  * independent of a reboot.
77  */
78 time64_t ktime_get_real_seconds(void)
79 {
80         return (time64_t)get_seconds();
81 }
82 EXPORT_SYMBOL(ktime_get_real_seconds);
83 #endif /* HAVE_KTIME_GET_REAL_SECONDS */
84
85 #ifndef HAVE_KTIME_GET_SECONDS
86 /*
87  * Get the seconds portion of CLOCK_MONOTONIC
88  * This clock is immutable and is reset across
89  * reboots. For older platforms this is a
90  * wrapper around get_seconds which is valid
91  * until 2038. By that time this will be gone
92  * one would hope.
93  */
94 time64_t ktime_get_seconds(void)
95 {
96         struct timespec64 now;
97
98         ktime_get_ts64(&now);
99         return now.tv_sec;
100 }
101 EXPORT_SYMBOL(ktime_get_seconds);
102 #endif /* HAVE_KTIME_GET_SECONDS */
103
104 int cfs_kernel_write(struct file *filp, const void *buf, size_t count,
105                      loff_t *pos)
106 {
107 #ifdef HAVE_NEW_KERNEL_WRITE
108         return kernel_write(filp, buf, count, pos);
109 #else
110         mm_segment_t __old_fs = get_fs();
111         int rc;
112
113         set_fs(get_ds());
114         rc = vfs_write(filp, (__force const char __user *)buf, count, pos);
115         set_fs(__old_fs);
116
117         return rc;
118 #endif
119 }
120 EXPORT_SYMBOL(cfs_kernel_write);
121
122 #ifndef HAVE_KSET_FIND_OBJ
123 struct kobject *kset_find_obj(struct kset *kset, const char *name)
124 {
125         struct kobject *ret = NULL;
126         struct kobject *k;
127
128         spin_lock(&kset->list_lock);
129
130         list_for_each_entry(k, &kset->list, entry) {
131                 if (kobject_name(k) && !strcmp(kobject_name(k), name)) {
132                         if (kref_get_unless_zero(&k->kref))
133                                 ret = k;
134                         break;
135                 }
136         }
137
138         spin_unlock(&kset->list_lock);
139         return ret;
140 }
141 EXPORT_SYMBOL_GPL(kset_find_obj);
142 #endif
143
144 #ifndef HAVE_KSTRTOBOOL_FROM_USER
145 int kstrtobool_from_user(const char __user *s, size_t count, bool *res)
146 {
147         /* Longest string needed to differentiate, newline, terminator */
148         char buf[4];
149
150         count = min(count, sizeof(buf) - 1);
151         if (copy_from_user(buf, s, count))
152                 return -EFAULT;
153         buf[count] = '\0';
154         return strtobool(buf, res);
155 }
156 EXPORT_SYMBOL(kstrtobool_from_user);
157 #endif /* !HAVE_KSTRTOBOOL_FROM_USER */
158
159 sigset_t
160 cfs_block_allsigs(void)
161 {
162         unsigned long   flags;
163         sigset_t        old;
164
165         spin_lock_irqsave(&current->sighand->siglock, flags);
166         old = current->blocked;
167         sigfillset(&current->blocked);
168         recalc_sigpending();
169         spin_unlock_irqrestore(&current->sighand->siglock, flags);
170         return old;
171 }
172 EXPORT_SYMBOL(cfs_block_allsigs);
173
174 sigset_t cfs_block_sigs(unsigned long sigs)
175 {
176         unsigned long  flags;
177         sigset_t        old;
178
179         spin_lock_irqsave(&current->sighand->siglock, flags);
180         old = current->blocked;
181         sigaddsetmask(&current->blocked, sigs);
182         recalc_sigpending();
183         spin_unlock_irqrestore(&current->sighand->siglock, flags);
184         return old;
185 }
186 EXPORT_SYMBOL(cfs_block_sigs);
187
188 /* Block all signals except for the @sigs */
189 sigset_t cfs_block_sigsinv(unsigned long sigs)
190 {
191         unsigned long flags;
192         sigset_t old;
193
194         spin_lock_irqsave(&current->sighand->siglock, flags);
195         old = current->blocked;
196         sigaddsetmask(&current->blocked, ~sigs);
197         recalc_sigpending();
198         spin_unlock_irqrestore(&current->sighand->siglock, flags);
199         return old;
200 }
201 EXPORT_SYMBOL(cfs_block_sigsinv);
202
203 void
204 cfs_restore_sigs(sigset_t old)
205 {
206         unsigned long  flags;
207
208         spin_lock_irqsave(&current->sighand->siglock, flags);
209         current->blocked = old;
210         recalc_sigpending();
211         spin_unlock_irqrestore(&current->sighand->siglock, flags);
212 }
213 EXPORT_SYMBOL(cfs_restore_sigs);
214
215 void
216 cfs_clear_sigpending(void)
217 {
218         unsigned long flags;
219
220         spin_lock_irqsave(&current->sighand->siglock, flags);
221         clear_tsk_thread_flag(current, TIF_SIGPENDING);
222         spin_unlock_irqrestore(&current->sighand->siglock, flags);
223 }
224 EXPORT_SYMBOL(cfs_clear_sigpending);