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