]>
Commit | Line | Data |
---|---|---|
d7e09d03 PT |
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 | |
6a5b99a4 | 18 | * http://www.gnu.org/licenses/gpl-2.0.html |
d7e09d03 | 19 | * |
d7e09d03 PT |
20 | * GPL HEADER END |
21 | */ | |
22 | /* | |
23 | * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved. | |
24 | * Use is subject to license terms. | |
25 | * | |
26 | * Copyright (c) 2011, 2012, 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 | * lustre/include/lustre_lib.h | |
33 | * | |
34 | * Basic Lustre library routines. | |
35 | */ | |
36 | ||
37 | #ifndef _LUSTRE_LIB_H | |
38 | #define _LUSTRE_LIB_H | |
39 | ||
40 | /** \defgroup lib lib | |
41 | * | |
42 | * @{ | |
43 | */ | |
44 | ||
174cd4b1 | 45 | #include <linux/sched/signal.h> |
376ef86b JH |
46 | #include <linux/signal.h> |
47 | #include <linux/types.h> | |
9fdaf8c0 | 48 | #include "../../include/linux/libcfs/libcfs.h" |
1accaadf GKH |
49 | #include "lustre/lustre_idl.h" |
50 | #include "lustre_ver.h" | |
51 | #include "lustre_cfg.h" | |
d7e09d03 PT |
52 | |
53 | /* target.c */ | |
54 | struct ptlrpc_request; | |
55 | struct obd_export; | |
56 | struct lu_target; | |
57 | struct l_wait_info; | |
1accaadf GKH |
58 | #include "lustre_ha.h" |
59 | #include "lustre_net.h" | |
d7e09d03 | 60 | |
376ef86b JH |
61 | #define LI_POISON 0x5a5a5a5a |
62 | #if BITS_PER_LONG > 32 | |
63 | # define LL_POISON 0x5a5a5a5a5a5a5a5aL | |
64 | #else | |
65 | # define LL_POISON 0x5a5a5a5aL | |
66 | #endif | |
67 | #define LP_POISON ((void *)LL_POISON) | |
d7e09d03 PT |
68 | |
69 | int target_pack_pool_reply(struct ptlrpc_request *req); | |
70 | int do_set_info_async(struct obd_import *imp, | |
71 | int opcode, int version, | |
21aef7d9 OD |
72 | u32 keylen, void *key, |
73 | u32 vallen, void *val, | |
d7e09d03 PT |
74 | struct ptlrpc_request_set *set); |
75 | ||
d7e09d03 PT |
76 | void target_send_reply(struct ptlrpc_request *req, int rc, int fail_id); |
77 | ||
d7e09d03 PT |
78 | /* |
79 | * l_wait_event is a flexible sleeping function, permitting simple caller | |
80 | * configuration of interrupt and timeout sensitivity along with actions to | |
81 | * be performed in the event of either exception. | |
82 | * | |
83 | * The first form of usage looks like this: | |
84 | * | |
85 | * struct l_wait_info lwi = LWI_TIMEOUT_INTR(timeout, timeout_handler, | |
86 | * intr_handler, callback_data); | |
87 | * rc = l_wait_event(waitq, condition, &lwi); | |
88 | * | |
89 | * l_wait_event() makes the current process wait on 'waitq' until 'condition' | |
90 | * is TRUE or a "killable" signal (SIGTERM, SIKGILL, SIGINT) is pending. It | |
91 | * returns 0 to signify 'condition' is TRUE, but if a signal wakes it before | |
92 | * 'condition' becomes true, it optionally calls the specified 'intr_handler' | |
93 | * if not NULL, and returns -EINTR. | |
94 | * | |
95 | * If a non-zero timeout is specified, signals are ignored until the timeout | |
96 | * has expired. At this time, if 'timeout_handler' is not NULL it is called. | |
97 | * If it returns FALSE l_wait_event() continues to wait as described above with | |
98 | * signals enabled. Otherwise it returns -ETIMEDOUT. | |
99 | * | |
100 | * LWI_INTR(intr_handler, callback_data) is shorthand for | |
101 | * LWI_TIMEOUT_INTR(0, NULL, intr_handler, callback_data) | |
102 | * | |
103 | * The second form of usage looks like this: | |
104 | * | |
105 | * struct l_wait_info lwi = LWI_TIMEOUT(timeout, timeout_handler); | |
106 | * rc = l_wait_event(waitq, condition, &lwi); | |
107 | * | |
108 | * This form is the same as the first except that it COMPLETELY IGNORES | |
109 | * SIGNALS. The caller must therefore beware that if 'timeout' is zero, or if | |
110 | * 'timeout_handler' is not NULL and returns FALSE, then the ONLY thing that | |
111 | * can unblock the current process is 'condition' becoming TRUE. | |
112 | * | |
113 | * Another form of usage is: | |
114 | * struct l_wait_info lwi = LWI_TIMEOUT_INTERVAL(timeout, interval, | |
115 | * timeout_handler); | |
116 | * rc = l_wait_event(waitq, condition, &lwi); | |
117 | * This is the same as previous case, but condition is checked once every | |
118 | * 'interval' jiffies (if non-zero). | |
119 | * | |
120 | * Subtle synchronization point: this macro does *not* necessary takes | |
121 | * wait-queue spin-lock before returning, and, hence, following idiom is safe | |
122 | * ONLY when caller provides some external locking: | |
123 | * | |
124 | * Thread1 Thread2 | |
125 | * | |
126 | * l_wait_event(&obj->wq, ....); (1) | |
127 | * | |
128 | * wake_up(&obj->wq): (2) | |
129 | * spin_lock(&q->lock); (2.1) | |
130 | * __wake_up_common(q, ...); (2.2) | |
131 | * spin_unlock(&q->lock, flags); (2.3) | |
132 | * | |
c0a2472f | 133 | * kfree(obj); (3) |
d7e09d03 PT |
134 | * |
135 | * As l_wait_event() may "short-cut" execution and return without taking | |
136 | * wait-queue spin-lock, some additional synchronization is necessary to | |
137 | * guarantee that step (3) can begin only after (2.3) finishes. | |
138 | * | |
139 | * XXX nikita: some ptlrpc daemon threads have races of that sort. | |
140 | * | |
141 | */ | |
142 | static inline int back_to_sleep(void *arg) | |
143 | { | |
144 | return 0; | |
145 | } | |
146 | ||
147 | #define LWI_ON_SIGNAL_NOOP ((void (*)(void *))(-1)) | |
148 | ||
149 | struct l_wait_info { | |
b2d201bd GKH |
150 | long lwi_timeout; |
151 | long lwi_interval; | |
d7e09d03 PT |
152 | int lwi_allow_intr; |
153 | int (*lwi_on_timeout)(void *); | |
154 | void (*lwi_on_signal)(void *); | |
155 | void *lwi_cb_data; | |
156 | }; | |
157 | ||
158 | /* NB: LWI_TIMEOUT ignores signals completely */ | |
159 | #define LWI_TIMEOUT(time, cb, data) \ | |
160 | ((struct l_wait_info) { \ | |
161 | .lwi_timeout = time, \ | |
162 | .lwi_on_timeout = cb, \ | |
163 | .lwi_cb_data = data, \ | |
164 | .lwi_interval = 0, \ | |
165 | .lwi_allow_intr = 0 \ | |
166 | }) | |
167 | ||
168 | #define LWI_TIMEOUT_INTERVAL(time, interval, cb, data) \ | |
169 | ((struct l_wait_info) { \ | |
170 | .lwi_timeout = time, \ | |
171 | .lwi_on_timeout = cb, \ | |
172 | .lwi_cb_data = data, \ | |
173 | .lwi_interval = interval, \ | |
174 | .lwi_allow_intr = 0 \ | |
175 | }) | |
176 | ||
177 | #define LWI_TIMEOUT_INTR(time, time_cb, sig_cb, data) \ | |
178 | ((struct l_wait_info) { \ | |
179 | .lwi_timeout = time, \ | |
180 | .lwi_on_timeout = time_cb, \ | |
181 | .lwi_on_signal = sig_cb, \ | |
182 | .lwi_cb_data = data, \ | |
183 | .lwi_interval = 0, \ | |
184 | .lwi_allow_intr = 0 \ | |
185 | }) | |
186 | ||
187 | #define LWI_TIMEOUT_INTR_ALL(time, time_cb, sig_cb, data) \ | |
188 | ((struct l_wait_info) { \ | |
189 | .lwi_timeout = time, \ | |
190 | .lwi_on_timeout = time_cb, \ | |
191 | .lwi_on_signal = sig_cb, \ | |
192 | .lwi_cb_data = data, \ | |
193 | .lwi_interval = 0, \ | |
194 | .lwi_allow_intr = 1 \ | |
195 | }) | |
196 | ||
197 | #define LWI_INTR(cb, data) LWI_TIMEOUT_INTR(0, NULL, cb, data) | |
198 | ||
376ef86b JH |
199 | #define LUSTRE_FATAL_SIGS (sigmask(SIGKILL) | sigmask(SIGINT) | \ |
200 | sigmask(SIGTERM) | sigmask(SIGQUIT) | \ | |
201 | sigmask(SIGALRM)) | |
202 | ||
564f5d6e | 203 | /** |
ac6424b9 | 204 | * wait_queue_entry_t of Linux (version < 2.6.34) is a FIFO list for exclusively |
564f5d6e JS |
205 | * waiting threads, which is not always desirable because all threads will |
206 | * be waken up again and again, even user only needs a few of them to be | |
207 | * active most time. This is not good for performance because cache can | |
208 | * be polluted by different threads. | |
209 | * | |
210 | * LIFO list can resolve this problem because we always wakeup the most | |
211 | * recent active thread by default. | |
212 | * | |
213 | * NB: please don't call non-exclusive & exclusive wait on the same | |
214 | * waitq if add_wait_queue_exclusive_head is used. | |
215 | */ | |
216 | #define add_wait_queue_exclusive_head(waitq, link) \ | |
217 | { \ | |
218 | unsigned long flags; \ | |
219 | \ | |
220 | spin_lock_irqsave(&((waitq)->lock), flags); \ | |
221 | __add_wait_queue_exclusive(waitq, link); \ | |
222 | spin_unlock_irqrestore(&((waitq)->lock), flags); \ | |
223 | } | |
224 | ||
d7e09d03 PT |
225 | /* |
226 | * wait for @condition to become true, but no longer than timeout, specified | |
227 | * by @info. | |
228 | */ | |
229 | #define __l_wait_event(wq, condition, info, ret, l_add_wait) \ | |
230 | do { \ | |
ac6424b9 | 231 | wait_queue_entry_t __wait; \ |
b2d201bd | 232 | long __timeout = info->lwi_timeout; \ |
d7e09d03 PT |
233 | sigset_t __blocked; \ |
234 | int __allow_intr = info->lwi_allow_intr; \ | |
235 | \ | |
236 | ret = 0; \ | |
237 | if (condition) \ | |
238 | break; \ | |
239 | \ | |
9e795d35 | 240 | init_waitqueue_entry(&__wait, current); \ |
d7e09d03 PT |
241 | l_add_wait(&wq, &__wait); \ |
242 | \ | |
243 | /* Block all signals (just the non-fatal ones if no timeout). */ \ | |
d2a13989 | 244 | if (info->lwi_on_signal && (__timeout == 0 || __allow_intr)) \ |
d7e09d03 PT |
245 | __blocked = cfs_block_sigsinv(LUSTRE_FATAL_SIGS); \ |
246 | else \ | |
247 | __blocked = cfs_block_sigsinv(0); \ | |
248 | \ | |
249 | for (;;) { \ | |
d7e09d03 PT |
250 | if (condition) \ |
251 | break; \ | |
252 | \ | |
65609bd6 OD |
253 | set_current_state(TASK_INTERRUPTIBLE); \ |
254 | \ | |
d7e09d03 | 255 | if (__timeout == 0) { \ |
65609bd6 | 256 | schedule(); \ |
d7e09d03 | 257 | } else { \ |
b2952d62 | 258 | long interval = info->lwi_interval ? \ |
b2d201bd | 259 | min_t(long, \ |
b2952d62 | 260 | info->lwi_interval, __timeout) : \ |
d7e09d03 | 261 | __timeout; \ |
b2d201bd | 262 | long remaining = schedule_timeout(interval);\ |
d7e09d03 PT |
263 | __timeout = cfs_time_sub(__timeout, \ |
264 | cfs_time_sub(interval, remaining));\ | |
265 | if (__timeout == 0) { \ | |
d2a13989 | 266 | if (!info->lwi_on_timeout || \ |
d7e09d03 PT |
267 | info->lwi_on_timeout(info->lwi_cb_data)) { \ |
268 | ret = -ETIMEDOUT; \ | |
269 | break; \ | |
270 | } \ | |
271 | /* Take signals after the timeout expires. */ \ | |
d2a13989 | 272 | if (info->lwi_on_signal) \ |
d7e09d03 PT |
273 | (void)cfs_block_sigsinv(LUSTRE_FATAL_SIGS);\ |
274 | } \ | |
275 | } \ | |
276 | \ | |
65609bd6 OD |
277 | set_current_state(TASK_RUNNING); \ |
278 | \ | |
d7e09d03 PT |
279 | if (condition) \ |
280 | break; \ | |
9af4826a | 281 | if (signal_pending(current)) { \ |
d2a13989 | 282 | if (info->lwi_on_signal && \ |
d7e09d03 PT |
283 | (__timeout == 0 || __allow_intr)) { \ |
284 | if (info->lwi_on_signal != LWI_ON_SIGNAL_NOOP) \ | |
285 | info->lwi_on_signal(info->lwi_cb_data);\ | |
286 | ret = -EINTR; \ | |
287 | break; \ | |
288 | } \ | |
289 | /* We have to do this here because some signals */ \ | |
290 | /* are not blockable - ie from strace(1). */ \ | |
291 | /* In these cases we want to schedule_timeout() */ \ | |
292 | /* again, because we don't want that to return */ \ | |
293 | /* -EINTR when the RPC actually succeeded. */ \ | |
294 | /* the recalc_sigpending() below will deliver the */ \ | |
295 | /* signal properly. */ \ | |
296 | cfs_clear_sigpending(); \ | |
297 | } \ | |
298 | } \ | |
299 | \ | |
300 | cfs_restore_sigs(__blocked); \ | |
301 | \ | |
d7e09d03 PT |
302 | remove_wait_queue(&wq, &__wait); \ |
303 | } while (0) | |
304 | ||
d7e09d03 PT |
305 | #define l_wait_event(wq, condition, info) \ |
306 | ({ \ | |
307 | int __ret; \ | |
308 | struct l_wait_info *__info = (info); \ | |
309 | \ | |
310 | __l_wait_event(wq, condition, __info, \ | |
311 | __ret, add_wait_queue); \ | |
312 | __ret; \ | |
313 | }) | |
314 | ||
315 | #define l_wait_event_exclusive(wq, condition, info) \ | |
316 | ({ \ | |
317 | int __ret; \ | |
318 | struct l_wait_info *__info = (info); \ | |
319 | \ | |
320 | __l_wait_event(wq, condition, __info, \ | |
321 | __ret, add_wait_queue_exclusive); \ | |
322 | __ret; \ | |
323 | }) | |
324 | ||
325 | #define l_wait_event_exclusive_head(wq, condition, info) \ | |
326 | ({ \ | |
327 | int __ret; \ | |
328 | struct l_wait_info *__info = (info); \ | |
329 | \ | |
330 | __l_wait_event(wq, condition, __info, \ | |
331 | __ret, add_wait_queue_exclusive_head); \ | |
332 | __ret; \ | |
333 | }) | |
334 | ||
335 | #define l_wait_condition(wq, condition) \ | |
336 | ({ \ | |
337 | struct l_wait_info lwi = { 0 }; \ | |
338 | l_wait_event(wq, condition, &lwi); \ | |
339 | }) | |
340 | ||
341 | #define l_wait_condition_exclusive(wq, condition) \ | |
342 | ({ \ | |
343 | struct l_wait_info lwi = { 0 }; \ | |
344 | l_wait_event_exclusive(wq, condition, &lwi); \ | |
345 | }) | |
346 | ||
347 | #define l_wait_condition_exclusive_head(wq, condition) \ | |
348 | ({ \ | |
349 | struct l_wait_info lwi = { 0 }; \ | |
350 | l_wait_event_exclusive_head(wq, condition, &lwi); \ | |
351 | }) | |
352 | ||
d7e09d03 PT |
353 | /** @} lib */ |
354 | ||
355 | #endif /* _LUSTRE_LIB_H */ |