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