Whamcloud - gitweb
LU-6245 libcfs: add ktime_get_real_seconds support
[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 void cfs_init_timer(struct timer_list *t)
46 {
47         init_timer(t);
48 }
49 EXPORT_SYMBOL(cfs_init_timer);
50
51 void cfs_timer_init(struct timer_list *t, cfs_timer_func_t *func, void *arg)
52 {
53         init_timer(t);
54         t->function = func;
55         t->data = (unsigned long)arg;
56 }
57 EXPORT_SYMBOL(cfs_timer_init);
58
59 void cfs_timer_arm(struct timer_list *t, cfs_time_t deadline)
60 {
61         mod_timer(t, deadline);
62 }
63 EXPORT_SYMBOL(cfs_timer_arm);
64
65 void cfs_timer_disarm(struct timer_list *t)
66 {
67         del_timer(t);
68 }
69 EXPORT_SYMBOL(cfs_timer_disarm);
70
71 int  cfs_timer_is_armed(struct timer_list *t)
72 {
73         return timer_pending(t);
74 }
75 EXPORT_SYMBOL(cfs_timer_is_armed);
76
77 cfs_time_t cfs_timer_deadline(struct timer_list *t)
78 {
79         return t->expires;
80 }
81 EXPORT_SYMBOL(cfs_timer_deadline);
82
83 #ifndef HAVE_KTIME_GET_TS64
84 void ktime_get_ts64(struct timespec64 *ts)
85 {
86         struct timespec now;
87
88         ktime_get_ts(&now);
89         *ts = timespec_to_timespec64(now);
90 }
91 EXPORT_SYMBOL(ktime_get_ts64);
92 #endif /* HAVE_KTIME_GET_TS64 */
93
94 #ifndef HAVE_KTIME_GET_REAL_TS64
95 void ktime_get_real_ts64(struct timespec64 *ts)
96 {
97         struct timespec now;
98
99         getnstimeofday(&now);
100         *ts = timespec_to_timespec64(now);
101 }
102 EXPORT_SYMBOL(ktime_get_real_ts64);
103 #endif /* HAVE_KTIME_GET_REAL_TS64 */
104
105 #ifndef HAVE_KTIME_GET_REAL_SECONDS
106 /*
107  * Get the seconds portion of CLOCK_REALTIME (wall clock).
108  * This is the clock that can be altered by NTP and is
109  * independent of a reboot.
110  */
111 time64_t ktime_get_real_seconds(void)
112 {
113         return (time64_t)get_seconds();
114 }
115 EXPORT_SYMBOL(ktime_get_real_seconds);
116 #endif /* HAVE_KTIME_GET_REAL_SECONDS */
117
118 #ifndef HAVE_KTIME_GET_SECONDS
119 /*
120  * Get the seconds portion of CLOCK_MONOTONIC
121  * This clock is immutable and is reset across
122  * reboots. For older platforms this is a
123  * wrapper around get_seconds which is valid
124  * until 2038. By that time this will be gone
125  * one would hope.
126  */
127 time64_t ktime_get_seconds(void)
128 {
129         struct timespec64 now;
130
131         ktime_get_ts64(&now);
132         return now.tv_sec;
133 }
134 EXPORT_SYMBOL(ktime_get_seconds);
135 #endif /* HAVE_KTIME_GET_SECONDS */
136
137 sigset_t
138 cfs_block_allsigs(void)
139 {
140         unsigned long   flags;
141         sigset_t        old;
142
143         spin_lock_irqsave(&current->sighand->siglock, flags);
144         old = current->blocked;
145         sigfillset(&current->blocked);
146         recalc_sigpending();
147         spin_unlock_irqrestore(&current->sighand->siglock, flags);
148         return old;
149 }
150 EXPORT_SYMBOL(cfs_block_allsigs);
151
152 sigset_t cfs_block_sigs(unsigned long sigs)
153 {
154         unsigned long  flags;
155         sigset_t        old;
156
157         spin_lock_irqsave(&current->sighand->siglock, flags);
158         old = current->blocked;
159         sigaddsetmask(&current->blocked, sigs);
160         recalc_sigpending();
161         spin_unlock_irqrestore(&current->sighand->siglock, flags);
162         return old;
163 }
164 EXPORT_SYMBOL(cfs_block_sigs);
165
166 /* Block all signals except for the @sigs */
167 sigset_t cfs_block_sigsinv(unsigned long sigs)
168 {
169         unsigned long flags;
170         sigset_t old;
171
172         spin_lock_irqsave(&current->sighand->siglock, flags);
173         old = current->blocked;
174         sigaddsetmask(&current->blocked, ~sigs);
175         recalc_sigpending();
176         spin_unlock_irqrestore(&current->sighand->siglock, flags);
177         return old;
178 }
179 EXPORT_SYMBOL(cfs_block_sigsinv);
180
181 void
182 cfs_restore_sigs(sigset_t old)
183 {
184         unsigned long  flags;
185
186         spin_lock_irqsave(&current->sighand->siglock, flags);
187         current->blocked = old;
188         recalc_sigpending();
189         spin_unlock_irqrestore(&current->sighand->siglock, flags);
190 }
191 EXPORT_SYMBOL(cfs_restore_sigs);
192
193 void
194 cfs_clear_sigpending(void)
195 {
196         unsigned long flags;
197
198         spin_lock_irqsave(&current->sighand->siglock, flags);
199         clear_tsk_thread_flag(current, TIF_SIGPENDING);
200         spin_unlock_irqrestore(&current->sighand->siglock, flags);
201 }
202 EXPORT_SYMBOL(cfs_clear_sigpending);