4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2014, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/include/lustre_lib.h
34 * Basic Lustre library routines.
45 #include <linux/signal.h>
46 #include <libcfs/libcfs.h>
47 #include <lustre/lustre_idl.h>
48 #include <lustre_ver.h>
49 #include <lustre_cfg.h>
52 struct ptlrpc_request;
56 #include <lustre_ha.h>
57 #include <lustre_net.h>
59 #define LI_POISON 0x5a5a5a5a
60 #if BITS_PER_LONG > 32
61 # define LL_POISON 0x5a5a5a5a5a5a5a5aL
63 # define LL_POISON 0x5a5a5a5aL
65 #define LP_POISON ((void *)LL_POISON)
67 #ifdef HAVE_SERVER_SUPPORT
68 void target_client_add_cb(struct obd_device *obd, __u64 transno, void *cb_data,
70 int rev_import_init(struct obd_export *exp);
71 int target_handle_connect(struct ptlrpc_request *req);
72 int target_handle_disconnect(struct ptlrpc_request *req);
73 void target_destroy_export(struct obd_export *exp);
74 int target_handle_ping(struct ptlrpc_request *req);
75 void target_committed_to_req(struct ptlrpc_request *req);
76 void target_cancel_recovery_timer(struct obd_device *obd);
77 void target_stop_recovery_thread(struct obd_device *obd);
78 void target_cleanup_recovery(struct obd_device *obd);
79 int target_queue_recovery_request(struct ptlrpc_request *req,
80 struct obd_device *obd);
81 int target_bulk_io(struct obd_export *exp, struct ptlrpc_bulk_desc *desc,
82 struct l_wait_info *lwi);
85 int target_pack_pool_reply(struct ptlrpc_request *req);
86 int do_set_info_async(struct obd_import *imp,
87 int opcode, int version,
88 size_t keylen, void *key,
89 size_t vallen, void *val,
90 struct ptlrpc_request_set *set);
92 void target_send_reply(struct ptlrpc_request *req, int rc, int fail_id);
95 * l_wait_event is a flexible sleeping function, permitting simple caller
96 * configuration of interrupt and timeout sensitivity along with actions to
97 * be performed in the event of either exception.
99 * The first form of usage looks like this:
101 * struct l_wait_info lwi = LWI_TIMEOUT_INTR(timeout, timeout_handler,
102 * intr_handler, callback_data);
103 * rc = l_wait_event(waitq, condition, &lwi);
105 * l_wait_event() makes the current process wait on 'waitq' until 'condition'
106 * is TRUE or a "killable" signal (SIGTERM, SIKGILL, SIGINT) is pending. It
107 * returns 0 to signify 'condition' is TRUE, but if a signal wakes it before
108 * 'condition' becomes true, it optionally calls the specified 'intr_handler'
109 * if not NULL, and returns -EINTR.
111 * If a non-zero timeout is specified, signals are ignored until the timeout
112 * has expired. At this time, if 'timeout_handler' is not NULL it is called.
113 * If it returns FALSE l_wait_event() continues to wait as described above with
114 * signals enabled. Otherwise it returns -ETIMEDOUT.
116 * LWI_INTR(intr_handler, callback_data) is shorthand for
117 * LWI_TIMEOUT_INTR(0, NULL, intr_handler, callback_data)
119 * The second form of usage looks like this:
121 * struct l_wait_info lwi = LWI_TIMEOUT(timeout, timeout_handler);
122 * rc = l_wait_event(waitq, condition, &lwi);
124 * This form is the same as the first except that it COMPLETELY IGNORES
125 * SIGNALS. The caller must therefore beware that if 'timeout' is zero, or if
126 * 'timeout_handler' is not NULL and returns FALSE, then the ONLY thing that
127 * can unblock the current process is 'condition' becoming TRUE.
129 * Another form of usage is:
130 * struct l_wait_info lwi = LWI_TIMEOUT_INTERVAL(timeout, interval,
132 * rc = l_wait_event(waitq, condition, &lwi);
133 * This is the same as previous case, but condition is checked once every
134 * 'interval' jiffies (if non-zero).
136 * Subtle synchronization point: this macro does *not* necessary takes
137 * wait-queue spin-lock before returning, and, hence, following idiom is safe
138 * ONLY when caller provides some external locking:
142 * l_wait_event(&obj->wq, ....); (1)
144 * wake_up(&obj->wq): (2)
145 * spin_lock(&q->lock); (2.1)
146 * __wake_up_common(q, ...); (2.2)
147 * spin_unlock(&q->lock, flags); (2.3)
149 * OBD_FREE_PTR(obj); (3)
151 * As l_wait_event() may "short-cut" execution and return without taking
152 * wait-queue spin-lock, some additional synchronization is necessary to
153 * guarantee that step (3) can begin only after (2.3) finishes.
155 * XXX nikita: some ptlrpc daemon threads have races of that sort.
158 static inline int back_to_sleep(void *arg)
163 #define LWI_ON_SIGNAL_NOOP ((void (*)(void *))(-1))
166 cfs_duration_t lwi_timeout;
167 cfs_duration_t lwi_interval;
169 int (*lwi_on_timeout)(void *);
170 void (*lwi_on_signal)(void *);
174 /* NB: LWI_TIMEOUT ignores signals completely */
175 #define LWI_TIMEOUT(time, cb, data) \
176 ((struct l_wait_info) { \
177 .lwi_timeout = time, \
178 .lwi_on_timeout = cb, \
179 .lwi_cb_data = data, \
181 .lwi_allow_intr = 0 \
184 #define LWI_TIMEOUT_INTERVAL(time, interval, cb, data) \
185 ((struct l_wait_info) { \
186 .lwi_timeout = time, \
187 .lwi_on_timeout = cb, \
188 .lwi_cb_data = data, \
189 .lwi_interval = interval, \
190 .lwi_allow_intr = 0 \
193 #define LWI_TIMEOUT_INTR(time, time_cb, sig_cb, data) \
194 ((struct l_wait_info) { \
195 .lwi_timeout = time, \
196 .lwi_on_timeout = time_cb, \
197 .lwi_on_signal = sig_cb, \
198 .lwi_cb_data = data, \
200 .lwi_allow_intr = 0 \
203 #define LWI_TIMEOUT_INTR_ALL(time, time_cb, sig_cb, data) \
204 ((struct l_wait_info) { \
205 .lwi_timeout = time, \
206 .lwi_on_timeout = time_cb, \
207 .lwi_on_signal = sig_cb, \
208 .lwi_cb_data = data, \
210 .lwi_allow_intr = 1 \
213 #define LWI_INTR(cb, data) LWI_TIMEOUT_INTR(0, NULL, cb, data)
215 #define LUSTRE_FATAL_SIGS \
216 (sigmask(SIGKILL) | sigmask(SIGINT) | sigmask(SIGTERM) | \
217 sigmask(SIGQUIT) | sigmask(SIGALRM))
222 #ifndef HAVE___ADD_WAIT_QUEUE_EXCLUSIVE
223 static inline void __add_wait_queue_exclusive(wait_queue_head_t *q,
226 wait->flags |= WQ_FLAG_EXCLUSIVE;
227 __add_wait_queue(q, wait);
229 #endif /* HAVE___ADD_WAIT_QUEUE_EXCLUSIVE */
232 * wait_queue_t of Linux (version < 2.6.34) is a FIFO list for exclusively
233 * waiting threads, which is not always desirable because all threads will
234 * be waken up again and again, even user only needs a few of them to be
235 * active most time. This is not good for performance because cache can
236 * be polluted by different threads.
238 * LIFO list can resolve this problem because we always wakeup the most
239 * recent active thread by default.
241 * NB: please don't call non-exclusive & exclusive wait on the same
242 * waitq if add_wait_queue_exclusive_head is used.
244 #define add_wait_queue_exclusive_head(waitq, link) \
246 unsigned long flags; \
248 spin_lock_irqsave(&((waitq)->lock), flags); \
249 __add_wait_queue_exclusive(waitq, link); \
250 spin_unlock_irqrestore(&((waitq)->lock), flags); \
254 * wait for @condition to become true, but no longer than timeout, specified
257 #define __l_wait_event(wq, condition, info, ret, l_add_wait) \
259 wait_queue_t __wait; \
260 cfs_duration_t __timeout = info->lwi_timeout; \
261 sigset_t __blocked; \
262 int __allow_intr = info->lwi_allow_intr; \
268 init_waitqueue_entry(&__wait, current); \
269 l_add_wait(&wq, &__wait); \
271 /* Block all signals (just the non-fatal ones if no timeout). */ \
272 if (info->lwi_on_signal != NULL && (__timeout == 0 || __allow_intr)) \
273 __blocked = cfs_block_sigsinv(LUSTRE_FATAL_SIGS); \
275 __blocked = cfs_block_sigsinv(0); \
278 set_current_state(TASK_INTERRUPTIBLE); \
283 if (__timeout == 0) { \
286 cfs_duration_t interval = info->lwi_interval? \
287 min_t(cfs_duration_t, \
288 info->lwi_interval,__timeout):\
290 cfs_duration_t remaining = schedule_timeout(interval); \
291 __timeout = cfs_time_sub(__timeout, \
292 cfs_time_sub(interval, remaining));\
293 if (__timeout == 0) { \
294 if (info->lwi_on_timeout == NULL || \
295 info->lwi_on_timeout(info->lwi_cb_data)) { \
299 /* Take signals after the timeout expires. */ \
300 if (info->lwi_on_signal != NULL) \
301 (void)cfs_block_sigsinv(LUSTRE_FATAL_SIGS);\
307 if (signal_pending(current)) { \
308 if (info->lwi_on_signal != NULL && \
309 (__timeout == 0 || __allow_intr)) { \
310 if (info->lwi_on_signal != LWI_ON_SIGNAL_NOOP) \
311 info->lwi_on_signal(info->lwi_cb_data);\
315 /* We have to do this here because some signals */ \
316 /* are not blockable - ie from strace(1). */ \
317 /* In these cases we want to schedule_timeout() */ \
318 /* again, because we don't want that to return */ \
319 /* -EINTR when the RPC actually succeeded. */ \
320 /* the recalc_sigpending() below will deliver the */ \
321 /* signal properly. */ \
322 cfs_clear_sigpending(); \
326 cfs_restore_sigs(__blocked); \
328 set_current_state(TASK_RUNNING); \
329 remove_wait_queue(&wq, &__wait); \
333 #define l_wait_event(wq, condition, info) \
336 struct l_wait_info *__info = (info); \
338 __l_wait_event(wq, condition, __info, \
339 __ret, add_wait_queue); \
343 #define l_wait_event_exclusive(wq, condition, info) \
346 struct l_wait_info *__info = (info); \
348 __l_wait_event(wq, condition, __info, \
349 __ret, add_wait_queue_exclusive); \
353 #define l_wait_event_exclusive_head(wq, condition, info) \
356 struct l_wait_info *__info = (info); \
358 __l_wait_event(wq, condition, __info, \
359 __ret, add_wait_queue_exclusive_head); \
363 #define l_wait_condition(wq, condition) \
365 struct l_wait_info lwi = { 0 }; \
366 l_wait_event(wq, condition, &lwi); \
369 #define l_wait_condition_exclusive(wq, condition) \
371 struct l_wait_info lwi = { 0 }; \
372 l_wait_event_exclusive(wq, condition, &lwi); \
375 #define l_wait_condition_exclusive_head(wq, condition) \
377 struct l_wait_info lwi = { 0 }; \
378 l_wait_event_exclusive_head(wq, condition, &lwi); \
383 #endif /* _LUSTRE_LIB_H */