Whamcloud - gitweb
LU-9859 libcfs: merge UMP and SMP libcfs cpu header code
[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 sigset_t
125 cfs_block_allsigs(void)
126 {
127         unsigned long   flags;
128         sigset_t        old;
129
130         spin_lock_irqsave(&current->sighand->siglock, flags);
131         old = current->blocked;
132         sigfillset(&current->blocked);
133         recalc_sigpending();
134         spin_unlock_irqrestore(&current->sighand->siglock, flags);
135         return old;
136 }
137 EXPORT_SYMBOL(cfs_block_allsigs);
138
139 sigset_t cfs_block_sigs(unsigned long sigs)
140 {
141         unsigned long  flags;
142         sigset_t        old;
143
144         spin_lock_irqsave(&current->sighand->siglock, flags);
145         old = current->blocked;
146         sigaddsetmask(&current->blocked, sigs);
147         recalc_sigpending();
148         spin_unlock_irqrestore(&current->sighand->siglock, flags);
149         return old;
150 }
151 EXPORT_SYMBOL(cfs_block_sigs);
152
153 /* Block all signals except for the @sigs */
154 sigset_t cfs_block_sigsinv(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_sigsinv);
167
168 void
169 cfs_restore_sigs(sigset_t old)
170 {
171         unsigned long  flags;
172
173         spin_lock_irqsave(&current->sighand->siglock, flags);
174         current->blocked = old;
175         recalc_sigpending();
176         spin_unlock_irqrestore(&current->sighand->siglock, flags);
177 }
178 EXPORT_SYMBOL(cfs_restore_sigs);
179
180 void
181 cfs_clear_sigpending(void)
182 {
183         unsigned long flags;
184
185         spin_lock_irqsave(&current->sighand->siglock, flags);
186         clear_tsk_thread_flag(current, TIF_SIGPENDING);
187         spin_unlock_irqrestore(&current->sighand->siglock, flags);
188 }
189 EXPORT_SYMBOL(cfs_clear_sigpending);