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