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