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, 2017, 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 #ifdef HAVE_SCHED_HEADERS
46 #include <linux/sched/signal.h>
47 #include <linux/sched/mm.h>
50 #include <libcfs/libcfs.h>
51 #include <uapi/linux/lustre/lustre_idl.h>
52 #include <uapi/linux/lustre/lustre_ver.h>
53 #include <uapi/linux/lustre/lustre_cfg.h>
56 struct ptlrpc_request;
60 #include <lustre_ha.h>
61 #include <lustre_net.h>
63 #define LI_POISON 0x5a5a5a5a
64 #if BITS_PER_LONG > 32
65 # define LL_POISON 0x5a5a5a5a5a5a5a5aL
67 # define LL_POISON 0x5a5a5a5aL
69 #define LP_POISON ((void *)LL_POISON)
71 #ifdef HAVE_SERVER_SUPPORT
72 int rev_import_init(struct obd_export *exp);
73 int target_handle_connect(struct ptlrpc_request *req);
74 int target_handle_disconnect(struct ptlrpc_request *req);
75 void target_destroy_export(struct obd_export *exp);
76 int target_handle_ping(struct ptlrpc_request *req);
77 void target_committed_to_req(struct ptlrpc_request *req);
78 void target_cancel_recovery_timer(struct obd_device *obd);
79 void target_stop_recovery_thread(struct obd_device *obd);
80 void target_cleanup_recovery(struct obd_device *obd);
81 int target_queue_recovery_request(struct ptlrpc_request *req,
82 struct obd_device *obd);
83 int target_bulk_io(struct obd_export *exp, struct ptlrpc_bulk_desc *desc,
84 struct l_wait_info *lwi);
87 int target_pack_pool_reply(struct ptlrpc_request *req);
88 int do_set_info_async(struct obd_import *imp,
89 int opcode, int version,
90 size_t keylen, void *key,
91 size_t vallen, void *val,
92 struct ptlrpc_request_set *set);
94 void target_send_reply(struct ptlrpc_request *req, int rc, int fail_id);
97 * l_wait_event is a flexible sleeping function, permitting simple caller
98 * configuration of interrupt and timeout sensitivity along with actions to
99 * be performed in the event of either exception.
101 * The first form of usage looks like this:
103 * struct l_wait_info lwi = LWI_TIMEOUT_INTR(timeout, timeout_handler,
104 * intr_handler, callback_data);
105 * rc = l_wait_event(waitq, condition, &lwi);
107 * l_wait_event() makes the current process wait on 'waitq' until 'condition'
108 * is TRUE or a "killable" signal (SIGTERM, SIKGILL, SIGINT) is pending. It
109 * returns 0 to signify 'condition' is TRUE, but if a signal wakes it before
110 * 'condition' becomes true, it optionally calls the specified 'intr_handler'
111 * if not NULL, and returns -EINTR.
113 * If a non-zero timeout is specified, signals are ignored until the timeout
114 * has expired. At this time, if 'timeout_handler' is not NULL it is called.
115 * If it returns FALSE l_wait_event() continues to wait as described above with
116 * signals enabled. Otherwise it returns -ETIMEDOUT.
118 * LWI_INTR(intr_handler, callback_data) is shorthand for
119 * LWI_TIMEOUT_INTR(0, NULL, intr_handler, callback_data)
121 * The second form of usage looks like this:
123 * struct l_wait_info lwi = LWI_TIMEOUT(timeout, timeout_handler);
124 * rc = l_wait_event(waitq, condition, &lwi);
126 * This form is the same as the first except that it COMPLETELY IGNORES
127 * SIGNALS. The caller must therefore beware that if 'timeout' is zero, or if
128 * 'timeout_handler' is not NULL and returns FALSE, then the ONLY thing that
129 * can unblock the current process is 'condition' becoming TRUE.
131 * Another form of usage is:
132 * struct l_wait_info lwi = LWI_TIMEOUT_INTERVAL(timeout, interval,
134 * rc = l_wait_event(waitq, condition, &lwi);
135 * This is the same as previous case, but condition is checked once every
136 * 'interval' jiffies (if non-zero).
138 * Subtle synchronization point: this macro does *not* necessary takes
139 * wait-queue spin-lock before returning, and, hence, following idiom is safe
140 * ONLY when caller provides some external locking:
144 * l_wait_event(&obj->wq, ....); (1)
146 * wake_up(&obj->wq): (2)
147 * spin_lock(&q->lock); (2.1)
148 * __wake_up_common(q, ...); (2.2)
149 * spin_unlock(&q->lock, flags); (2.3)
151 * OBD_FREE_PTR(obj); (3)
153 * As l_wait_event() may "short-cut" execution and return without taking
154 * wait-queue spin-lock, some additional synchronization is necessary to
155 * guarantee that step (3) can begin only after (2.3) finishes.
157 * XXX nikita: some ptlrpc daemon threads have races of that sort.
160 static inline int back_to_sleep(void *arg)
165 #define LWI_ON_SIGNAL_NOOP ((void (*)(void *))(-1))
171 int (*lwi_on_timeout)(void *);
172 void (*lwi_on_signal)(void *);
176 /* NB: LWI_TIMEOUT ignores signals completely */
177 #define LWI_TIMEOUT(time, cb, data) \
178 ((struct l_wait_info) { \
179 .lwi_timeout = time, \
180 .lwi_on_timeout = cb, \
181 .lwi_cb_data = data, \
183 .lwi_allow_intr = 0 \
186 #define LWI_TIMEOUT_INTERVAL(time, interval, cb, data) \
187 ((struct l_wait_info) { \
188 .lwi_timeout = time, \
189 .lwi_on_timeout = cb, \
190 .lwi_cb_data = data, \
191 .lwi_interval = interval, \
192 .lwi_allow_intr = 0 \
195 #define LWI_TIMEOUT_INTR(time, time_cb, sig_cb, data) \
196 ((struct l_wait_info) { \
197 .lwi_timeout = time, \
198 .lwi_on_timeout = time_cb, \
199 .lwi_on_signal = sig_cb, \
200 .lwi_cb_data = data, \
202 .lwi_allow_intr = 0 \
205 #define LWI_TIMEOUT_INTR_ALL(time, time_cb, sig_cb, data) \
206 ((struct l_wait_info) { \
207 .lwi_timeout = time, \
208 .lwi_on_timeout = time_cb, \
209 .lwi_on_signal = sig_cb, \
210 .lwi_cb_data = data, \
212 .lwi_allow_intr = 1 \
215 #define LWI_INTR(cb, data) LWI_TIMEOUT_INTR(0, NULL, cb, data)
217 #define LUSTRE_FATAL_SIGS \
218 (sigmask(SIGKILL) | sigmask(SIGINT) | sigmask(SIGTERM) | \
219 sigmask(SIGQUIT) | sigmask(SIGALRM))
224 #if !defined(HAVE___ADD_WAIT_QUEUE_EXCLUSIVE) && !defined(HAVE_WAIT_QUEUE_ENTRY)
225 static inline void __add_wait_queue_exclusive(wait_queue_head_t *q,
228 wait->flags |= WQ_FLAG_EXCLUSIVE;
229 __add_wait_queue(q, wait);
231 #endif /* HAVE___ADD_WAIT_QUEUE_EXCLUSIVE */
234 * wait_queue_t of Linux (version < 2.6.34) is a FIFO list for exclusively
235 * waiting threads, which is not always desirable because all threads will
236 * be waken up again and again, even user only needs a few of them to be
237 * active most time. This is not good for performance because cache can
238 * be polluted by different threads.
240 * LIFO list can resolve this problem because we always wakeup the most
241 * recent active thread by default.
243 * NB: please don't call non-exclusive & exclusive wait on the same
244 * waitq if add_wait_queue_exclusive_head is used.
246 #define add_wait_queue_exclusive_head(waitq, link) \
248 unsigned long flags; \
250 spin_lock_irqsave(&((waitq)->lock), flags); \
251 __add_wait_queue_exclusive(waitq, link); \
252 spin_unlock_irqrestore(&((waitq)->lock), flags); \
256 * wait for @condition to become true, but no longer than timeout, specified
259 #define __l_wait_event(wq, condition, info, ret, l_add_wait) \
261 wait_queue_entry_t __wait; \
262 long __timeout = info->lwi_timeout; \
263 sigset_t __blocked; \
264 int __allow_intr = info->lwi_allow_intr; \
270 init_waitqueue_entry(&__wait, current); \
271 l_add_wait(&wq, &__wait); \
273 /* Block all signals (just the non-fatal ones if no timeout). */ \
274 if (info->lwi_on_signal != NULL && (__timeout == 0 || __allow_intr)) \
275 __blocked = cfs_block_sigsinv(LUSTRE_FATAL_SIGS); \
277 __blocked = cfs_block_sigsinv(0); \
280 set_current_state(TASK_INTERRUPTIBLE); \
282 /* To guarantee that the condition check will be done */ \
283 /* after setting the thread state as TASK_INTERRUPTIBLE. */ \
284 /* Otherwise, out-of-order execution may cause some race. */ \
285 /* Consider the following real execution order: */ \
287 /* 1. Thread1 checks condition on CPU1, gets false. */ \
288 /* 2. Thread2 sets condition on CPU2. */ \
289 /* 3. Thread2 calls wake_up() on CPU2 to wake the threads */ \
290 /* with state TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE. */ \
291 /* But the Thread1's state is TASK_RUNNING at that time. */ \
292 /* 4. Thread1 sets its state as TASK_INTERRUPTIBLE on CPU1, */ \
293 /* then schedule. */ \
295 /* If the '__timeout' variable is zero, the Thread1 will */ \
296 /* have no chance to check the condition again. */ \
298 /* Generally, the interval between out-of-ordered step1 and */ \
299 /* step4 is very tiny, as to above step2 and step3 cannot */ \
300 /* happen. On some degree, it can explain why we seldom hit */ \
301 /* related trouble. But such race really exists, especially */ \
302 /* consider that the step1 and step4 can be interruptible. */ \
303 /* So add barrier to avoid Thread1 out-of-order execution. */ \
309 if (__timeout == 0) { \
312 long interval = info->lwi_interval ? \
313 min_t(long, info->lwi_interval,\
314 __timeout) : __timeout; \
315 long remaining = schedule_timeout(interval); \
317 __timeout -= interval - remaining; \
318 if (__timeout == 0) { \
319 if (info->lwi_on_timeout == NULL || \
320 info->lwi_on_timeout(info->lwi_cb_data)) { \
324 /* Take signals after the timeout expires. */ \
325 if (info->lwi_on_signal != NULL) \
326 (void)cfs_block_sigsinv(LUSTRE_FATAL_SIGS);\
332 if (signal_pending(current)) { \
333 if (info->lwi_on_signal != NULL && \
334 (__timeout == 0 || __allow_intr)) { \
335 if (info->lwi_on_signal != LWI_ON_SIGNAL_NOOP) \
336 info->lwi_on_signal(info->lwi_cb_data);\
340 /* We have to do this here because some signals */ \
341 /* are not blockable - ie from strace(1). */ \
342 /* In these cases we want to schedule_timeout() */ \
343 /* again, because we don't want that to return */ \
344 /* -EINTR when the RPC actually succeeded. */ \
345 /* the recalc_sigpending() below will deliver the */ \
346 /* signal properly. */ \
347 cfs_clear_sigpending(); \
351 cfs_restore_sigs(__blocked); \
353 set_current_state(TASK_RUNNING); \
354 remove_wait_queue(&wq, &__wait); \
358 #define l_wait_event(wq, condition, info) \
361 struct l_wait_info *__info = (info); \
363 __l_wait_event(wq, condition, __info, \
364 __ret, add_wait_queue); \
368 #define l_wait_event_exclusive(wq, condition, info) \
371 struct l_wait_info *__info = (info); \
373 __l_wait_event(wq, condition, __info, \
374 __ret, add_wait_queue_exclusive); \
378 #define l_wait_event_exclusive_head(wq, condition, info) \
381 struct l_wait_info *__info = (info); \
383 __l_wait_event(wq, condition, __info, \
384 __ret, add_wait_queue_exclusive_head); \
388 #define l_wait_condition(wq, condition) \
390 struct l_wait_info lwi = { 0 }; \
391 l_wait_event(wq, condition, &lwi); \
394 #define l_wait_condition_exclusive(wq, condition) \
396 struct l_wait_info lwi = { 0 }; \
397 l_wait_event_exclusive(wq, condition, &lwi); \
400 #define l_wait_condition_exclusive_head(wq, condition) \
402 struct l_wait_info lwi = { 0 }; \
403 l_wait_event_exclusive_head(wq, condition, &lwi); \
408 #endif /* _LUSTRE_LIB_H */