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