Whamcloud - gitweb
5acfd89e9bc71146e29050c52143b5e458886778
[fs/lustre-release.git] / lustre / include / lustre_lib.h
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) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, 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
45 #ifdef HAVE_SCHED_HEADERS
46 #include <linux/sched/signal.h>
47 #include <linux/sched/mm.h>
48 #endif
49
50 #include <libcfs/linux/linux-misc.h>
51 #include <libcfs/libcfs.h>
52 #include <uapi/linux/lustre/lustre_idl.h>
53 #include <uapi/linux/lustre/lustre_ver.h>
54 #include <uapi/linux/lustre/lustre_cfg.h>
55
56 /* target.c */
57 struct ptlrpc_request;
58 struct obd_export;
59 struct lu_target;
60 struct l_wait_info;
61 #include <lustre_ha.h>
62 #include <lustre_net.h>
63
64 #define LI_POISON 0x5a5a5a5a
65 #if BITS_PER_LONG > 32
66 # define LL_POISON 0x5a5a5a5a5a5a5a5aL
67 #else
68 # define LL_POISON 0x5a5a5a5aL
69 #endif
70 #define LP_POISON ((void *)LL_POISON)
71
72 #ifdef HAVE_SERVER_SUPPORT
73 int rev_import_init(struct obd_export *exp);
74 int target_handle_connect(struct ptlrpc_request *req);
75 int target_handle_disconnect(struct ptlrpc_request *req);
76 void target_destroy_export(struct obd_export *exp);
77 int target_handle_ping(struct ptlrpc_request *req);
78 void target_committed_to_req(struct ptlrpc_request *req);
79 void target_cancel_recovery_timer(struct obd_device *obd);
80 void target_stop_recovery_thread(struct obd_device *obd);
81 void target_cleanup_recovery(struct obd_device *obd);
82 int target_queue_recovery_request(struct ptlrpc_request *req,
83                                   struct obd_device *obd);
84 int target_bulk_io(struct obd_export *exp, struct ptlrpc_bulk_desc *desc,
85                    struct l_wait_info *lwi);
86 #endif
87
88 int target_pack_pool_reply(struct ptlrpc_request *req);
89 int do_set_info_async(struct obd_import *imp,
90                       int opcode, int version,
91                       size_t keylen, void *key,
92                       size_t vallen, void *val,
93                       struct ptlrpc_request_set *set);
94
95 void target_send_reply(struct ptlrpc_request *req, int rc, int fail_id);
96
97 /*
98  * l_wait_event is a flexible sleeping function, permitting simple caller
99  * configuration of interrupt and timeout sensitivity along with actions to
100  * be performed in the event of either exception.
101  *
102  * The first form of usage looks like this:
103  *
104  * struct l_wait_info lwi = LWI_TIMEOUT_INTR(timeout, timeout_handler,
105  *                                           intr_handler, callback_data);
106  * rc = l_wait_event(waitq, condition, &lwi);
107  *
108  * l_wait_event() makes the current process wait on 'waitq' until 'condition'
109  * is TRUE or a "killable" signal (SIGTERM, SIKGILL, SIGINT) is pending.  It
110  * returns 0 to signify 'condition' is TRUE, but if a signal wakes it before
111  * 'condition' becomes true, it optionally calls the specified 'intr_handler'
112  * if not NULL, and returns -EINTR.
113  *
114  * If a non-zero timeout is specified, signals are ignored until the timeout
115  * has expired.  At this time, if 'timeout_handler' is not NULL it is called.
116  * If it returns FALSE l_wait_event() continues to wait as described above with
117  * signals enabled.  Otherwise it returns -ETIMEDOUT.
118  *
119  * LWI_INTR(intr_handler, callback_data) is shorthand for
120  * LWI_TIMEOUT_INTR(0, NULL, intr_handler, callback_data)
121  *
122  * The second form of usage looks like this:
123  *
124  * struct l_wait_info lwi = LWI_TIMEOUT(timeout, timeout_handler);
125  * rc = l_wait_event(waitq, condition, &lwi);
126  *
127  * This form is the same as the first except that it COMPLETELY IGNORES
128  * SIGNALS.  The caller must therefore beware that if 'timeout' is zero, or if
129  * 'timeout_handler' is not NULL and returns FALSE, then the ONLY thing that
130  * can unblock the current process is 'condition' becoming TRUE.
131  *
132  * Another form of usage is:
133  * struct l_wait_info lwi = LWI_TIMEOUT_INTERVAL(timeout, interval,
134  *                                               timeout_handler);
135  * rc = l_wait_event(waitq, condition, &lwi);
136  * This is the same as previous case, but condition is checked once every
137  * 'interval' jiffies (if non-zero).
138  *
139  * Subtle synchronization point: this macro does *not* necessary takes
140  * wait-queue spin-lock before returning, and, hence, following idiom is safe
141  * ONLY when caller provides some external locking:
142  *
143  *             Thread1                            Thread2
144  *
145  *   l_wait_event(&obj->wq, ....);                                       (1)
146  *
147  *                                    wake_up(&obj->wq):                 (2)
148  *                                         spin_lock(&q->lock);          (2.1)
149  *                                         __wake_up_common(q, ...);     (2.2)
150  *                                         spin_unlock(&q->lock, flags); (2.3)
151  *
152  *   OBD_FREE_PTR(obj);                                                  (3)
153  *
154  * As l_wait_event() may "short-cut" execution and return without taking
155  * wait-queue spin-lock, some additional synchronization is necessary to
156  * guarantee that step (3) can begin only after (2.3) finishes.
157  *
158  * XXX nikita: some ptlrpc daemon threads have races of that sort.
159  *
160  */
161 static inline int back_to_sleep(void *arg)
162 {
163         return 0;
164 }
165
166 #define LWI_ON_SIGNAL_NOOP ((void (*)(void *))(-1))
167
168 struct l_wait_info {
169         cfs_duration_t lwi_timeout;
170         cfs_duration_t lwi_interval;
171         int            lwi_allow_intr;
172         int  (*lwi_on_timeout)(void *);
173         void (*lwi_on_signal)(void *);
174         void  *lwi_cb_data;
175 };
176
177 /* NB: LWI_TIMEOUT ignores signals completely */
178 #define LWI_TIMEOUT(time, cb, data)             \
179 ((struct l_wait_info) {                         \
180         .lwi_timeout    = time,                 \
181         .lwi_on_timeout = cb,                   \
182         .lwi_cb_data    = data,                 \
183         .lwi_interval   = 0,                    \
184         .lwi_allow_intr = 0                     \
185 })
186
187 #define LWI_TIMEOUT_INTERVAL(time, interval, cb, data)  \
188 ((struct l_wait_info) {                                 \
189         .lwi_timeout    = time,                         \
190         .lwi_on_timeout = cb,                           \
191         .lwi_cb_data    = data,                         \
192         .lwi_interval   = interval,                     \
193         .lwi_allow_intr = 0                             \
194 })
195
196 #define LWI_TIMEOUT_INTR(time, time_cb, sig_cb, data)   \
197 ((struct l_wait_info) {                                 \
198         .lwi_timeout    = time,                         \
199         .lwi_on_timeout = time_cb,                      \
200         .lwi_on_signal  = sig_cb,                       \
201         .lwi_cb_data    = data,                         \
202         .lwi_interval   = 0,                            \
203         .lwi_allow_intr = 0                             \
204 })
205
206 #define LWI_TIMEOUT_INTR_ALL(time, time_cb, sig_cb, data)       \
207 ((struct l_wait_info) {                                         \
208         .lwi_timeout    = time,                                 \
209         .lwi_on_timeout = time_cb,                              \
210         .lwi_on_signal  = sig_cb,                               \
211         .lwi_cb_data    = data,                                 \
212         .lwi_interval   = 0,                                    \
213         .lwi_allow_intr = 1                                     \
214 })
215
216 #define LWI_INTR(cb, data)  LWI_TIMEOUT_INTR(0, NULL, cb, data)
217
218 #define LUSTRE_FATAL_SIGS                                        \
219         (sigmask(SIGKILL) | sigmask(SIGINT) | sigmask(SIGTERM) | \
220          sigmask(SIGQUIT) | sigmask(SIGALRM))
221
222 /*
223  * Wait Queue
224  */
225 #if !defined(HAVE___ADD_WAIT_QUEUE_EXCLUSIVE) && !defined(HAVE_WAIT_QUEUE_ENTRY)
226 static inline void __add_wait_queue_exclusive(wait_queue_head_t *q,
227                                               wait_queue_t *wait)
228 {
229         wait->flags |= WQ_FLAG_EXCLUSIVE;
230         __add_wait_queue(q, wait);
231 }
232 #endif /* HAVE___ADD_WAIT_QUEUE_EXCLUSIVE */
233
234 /**
235  * wait_queue_t of Linux (version < 2.6.34) is a FIFO list for exclusively
236  * waiting threads, which is not always desirable because all threads will
237  * be waken up again and again, even user only needs a few of them to be
238  * active most time. This is not good for performance because cache can
239  * be polluted by different threads.
240  *
241  * LIFO list can resolve this problem because we always wakeup the most
242  * recent active thread by default.
243  *
244  * NB: please don't call non-exclusive & exclusive wait on the same
245  * waitq if add_wait_queue_exclusive_head is used.
246  */
247 #define add_wait_queue_exclusive_head(waitq, link)              \
248 {                                                               \
249         unsigned long flags;                                    \
250                                                                 \
251         spin_lock_irqsave(&((waitq)->lock), flags);             \
252         __add_wait_queue_exclusive(waitq, link);                \
253         spin_unlock_irqrestore(&((waitq)->lock), flags);        \
254 }
255
256 /*
257  * wait for @condition to become true, but no longer than timeout, specified
258  * by @info.
259  */
260 #define __l_wait_event(wq, condition, info, ret, l_add_wait)                   \
261 do {                                                                           \
262         wait_queue_entry_t __wait;                                             \
263         cfs_duration_t __timeout = info->lwi_timeout;                          \
264         sigset_t   __blocked;                                              \
265         int   __allow_intr = info->lwi_allow_intr;                             \
266                                                                                \
267         ret = 0;                                                               \
268         if (condition)                                                         \
269                 break;                                                         \
270                                                                                \
271         init_waitqueue_entry(&__wait, current);                                \
272         l_add_wait(&wq, &__wait);                                              \
273                                                                                \
274         /* Block all signals (just the non-fatal ones if no timeout). */       \
275         if (info->lwi_on_signal != NULL && (__timeout == 0 || __allow_intr))   \
276                 __blocked = cfs_block_sigsinv(LUSTRE_FATAL_SIGS);              \
277         else                                                                   \
278                 __blocked = cfs_block_sigsinv(0);                              \
279                                                                                \
280         for (;;) {                                                             \
281                 set_current_state(TASK_INTERRUPTIBLE);                         \
282                                                                                \
283                 /* To guarantee that the condition check will be done */       \
284                 /* after setting the thread state as TASK_INTERRUPTIBLE. */    \
285                 /* Otherwise, out-of-order execution may cause some race. */   \
286                 /* Consider the following real execution order: */             \
287                                                                                \
288                 /* 1. Thread1 checks condition on CPU1, gets false. */         \
289                 /* 2. Thread2 sets condition on CPU2. */                       \
290                 /* 3. Thread2 calls wake_up() on CPU2 to wake the threads */   \
291                 /*    with state TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE. */ \
292                 /*    But the Thread1's state is TASK_RUNNING at that time. */ \
293                 /* 4. Thread1 sets its state as TASK_INTERRUPTIBLE on CPU1, */ \
294                 /*    then schedule. */                                        \
295                                                                                \
296                 /* If the '__timeout' variable is zero, the Thread1 will */    \
297                 /* have no chance to check the condition again. */             \
298                                                                                \
299                 /* Generally, the interval between out-of-ordered step1 and */ \
300                 /* step4 is very tiny, as to above step2 and step3 cannot */   \
301                 /* happen. On some degree, it can explain why we seldom hit */ \
302                 /* related trouble. But such race really exists, especially */ \
303                 /* consider that the step1 and step4 can be interruptible. */  \
304                 /* So add barrier to avoid Thread1 out-of-order execution. */  \
305                 smp_mb();                                                      \
306                                                                                \
307                 if (condition)                                                 \
308                         break;                                                 \
309                                                                                \
310                 if (__timeout == 0) {                                          \
311                         schedule();                                            \
312                 } else {                                                       \
313                         cfs_duration_t interval = info->lwi_interval?          \
314                                              min_t(cfs_duration_t,             \
315                                                  info->lwi_interval,__timeout):\
316                                              __timeout;                        \
317                         cfs_duration_t remaining = schedule_timeout(interval); \
318                         __timeout = cfs_time_sub(__timeout,                    \
319                                             cfs_time_sub(interval, remaining));\
320                         if (__timeout == 0) {                                  \
321                                 if (info->lwi_on_timeout == NULL ||            \
322                                     info->lwi_on_timeout(info->lwi_cb_data)) { \
323                                         ret = -ETIMEDOUT;                      \
324                                         break;                                 \
325                                 }                                              \
326                                 /* Take signals after the timeout expires. */  \
327                                 if (info->lwi_on_signal != NULL)               \
328                                     (void)cfs_block_sigsinv(LUSTRE_FATAL_SIGS);\
329                         }                                                      \
330                 }                                                              \
331                                                                                \
332                 if (condition)                                                 \
333                         break;                                                 \
334                 if (signal_pending(current)) {                                 \
335                         if (info->lwi_on_signal != NULL &&                     \
336                             (__timeout == 0 || __allow_intr)) {                \
337                                 if (info->lwi_on_signal != LWI_ON_SIGNAL_NOOP) \
338                                         info->lwi_on_signal(info->lwi_cb_data);\
339                                 ret = -EINTR;                                  \
340                                 break;                                         \
341                         }                                                      \
342                         /* We have to do this here because some signals */     \
343                         /* are not blockable - ie from strace(1).       */     \
344                         /* In these cases we want to schedule_timeout() */     \
345                         /* again, because we don't want that to return  */     \
346                         /* -EINTR when the RPC actually succeeded.      */     \
347                         /* the recalc_sigpending() below will deliver the */   \
348                         /* signal properly.                             */     \
349                         cfs_clear_sigpending();                                \
350                 }                                                              \
351         }                                                                      \
352                                                                                \
353         cfs_restore_sigs(__blocked);                                           \
354                                                                                \
355         set_current_state(TASK_RUNNING);                                       \
356         remove_wait_queue(&wq, &__wait);                                       \
357 } while (0)
358
359
360 #define l_wait_event(wq, condition, info)                       \
361 ({                                                              \
362         int                 __ret;                              \
363         struct l_wait_info *__info = (info);                    \
364                                                                 \
365         __l_wait_event(wq, condition, __info,                   \
366                        __ret, add_wait_queue);                  \
367         __ret;                                                  \
368 })
369
370 #define l_wait_event_exclusive(wq, condition, info)             \
371 ({                                                              \
372         int                 __ret;                              \
373         struct l_wait_info *__info = (info);                    \
374                                                                 \
375         __l_wait_event(wq, condition, __info,                   \
376                        __ret, add_wait_queue_exclusive);        \
377         __ret;                                                  \
378 })
379
380 #define l_wait_event_exclusive_head(wq, condition, info)        \
381 ({                                                              \
382         int                 __ret;                              \
383         struct l_wait_info *__info = (info);                    \
384                                                                 \
385         __l_wait_event(wq, condition, __info,                   \
386                        __ret, add_wait_queue_exclusive_head);   \
387         __ret;                                                  \
388 })
389
390 #define l_wait_condition(wq, condition)                         \
391 ({                                                              \
392         struct l_wait_info lwi = { 0 };                         \
393         l_wait_event(wq, condition, &lwi);                      \
394 })
395
396 #define l_wait_condition_exclusive(wq, condition)               \
397 ({                                                              \
398         struct l_wait_info lwi = { 0 };                         \
399         l_wait_event_exclusive(wq, condition, &lwi);            \
400 })
401
402 #define l_wait_condition_exclusive_head(wq, condition)          \
403 ({                                                              \
404         struct l_wait_info lwi = { 0 };                         \
405         l_wait_event_exclusive_head(wq, condition, &lwi);       \
406 })
407
408 /** @} lib */
409
410 #endif /* _LUSTRE_LIB_H */