Whamcloud - gitweb
LU-3963 obdclass: convert to linux list api
[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, 2013, 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 <libcfs/libcfs.h>
50 #include <lustre/lustre_idl.h>
51 #include <lustre_ver.h>
52 #include <lustre_cfg.h>
53 #if defined(__linux__)
54 #include <linux/lustre_lib.h>
55 #elif defined(__APPLE__)
56 #include <darwin/lustre_lib.h>
57 #elif defined(__WINNT__)
58 #include <winnt/lustre_lib.h>
59 #else
60 #error Unsupported operating system.
61 #endif
62
63 /* target.c */
64 struct ptlrpc_request;
65 struct obd_export;
66 struct lu_target;
67 struct l_wait_info;
68 #include <lustre_ha.h>
69 #include <lustre_net.h>
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 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                       obd_count keylen, void *key,
92                       obd_count vallen, void *val,
93                       struct ptlrpc_request_set *set);
94
95 #define OBD_RECOVERY_MAX_TIME (obd_timeout * 18) /* b13079 */
96
97 void target_send_reply(struct ptlrpc_request *req, int rc, int fail_id);
98
99 /* client.c */
100
101 int client_sanobd_setup(struct obd_device *obddev, struct lustre_cfg* lcfg);
102 struct client_obd *client_conn2cli(struct lustre_handle *conn);
103
104 struct md_open_data;
105 struct obd_client_handle {
106         struct lustre_handle     och_fh;
107         struct lu_fid            och_fid;
108         struct md_open_data     *och_mod;
109         struct lustre_handle     och_lease_handle; /* open lock for lease */
110         __u32                    och_magic;
111         int                      och_flags;
112 };
113 #define OBD_CLIENT_HANDLE_MAGIC 0xd15ea5ed
114
115 /* statfs_pack.c */
116 void statfs_pack(struct obd_statfs *osfs, struct kstatfs *sfs);
117 void statfs_unpack(struct kstatfs *sfs, struct obd_statfs *osfs);
118
119 /* Until such time as we get_info the per-stripe maximum from the OST,
120  * we define this to be 2T - 4k, which is the ext3 maxbytes. */
121 #define LUSTRE_STRIPE_MAXBYTES 0x1fffffff000ULL
122
123 /* Special values for remove LOV EA from disk */
124 #define LOVEA_DELETE_VALUES(size, count, offset) (size == 0 && count == 0 && \
125                                                  offset == (typeof(offset))(-1))
126
127 #define LMVEA_DELETE_VALUES(count, offset) ((count) == 0 && \
128                                             (offset) == (typeof(offset))(-1))
129 /* #define POISON_BULK 0 */
130
131 /*
132  * l_wait_event is a flexible sleeping function, permitting simple caller
133  * configuration of interrupt and timeout sensitivity along with actions to
134  * be performed in the event of either exception.
135  *
136  * The first form of usage looks like this:
137  *
138  * struct l_wait_info lwi = LWI_TIMEOUT_INTR(timeout, timeout_handler,
139  *                                           intr_handler, callback_data);
140  * rc = l_wait_event(waitq, condition, &lwi);
141  *
142  * l_wait_event() makes the current process wait on 'waitq' until 'condition'
143  * is TRUE or a "killable" signal (SIGTERM, SIKGILL, SIGINT) is pending.  It
144  * returns 0 to signify 'condition' is TRUE, but if a signal wakes it before
145  * 'condition' becomes true, it optionally calls the specified 'intr_handler'
146  * if not NULL, and returns -EINTR.
147  *
148  * If a non-zero timeout is specified, signals are ignored until the timeout
149  * has expired.  At this time, if 'timeout_handler' is not NULL it is called.
150  * If it returns FALSE l_wait_event() continues to wait as described above with
151  * signals enabled.  Otherwise it returns -ETIMEDOUT.
152  *
153  * LWI_INTR(intr_handler, callback_data) is shorthand for
154  * LWI_TIMEOUT_INTR(0, NULL, intr_handler, callback_data)
155  *
156  * The second form of usage looks like this:
157  *
158  * struct l_wait_info lwi = LWI_TIMEOUT(timeout, timeout_handler);
159  * rc = l_wait_event(waitq, condition, &lwi);
160  *
161  * This form is the same as the first except that it COMPLETELY IGNORES
162  * SIGNALS.  The caller must therefore beware that if 'timeout' is zero, or if
163  * 'timeout_handler' is not NULL and returns FALSE, then the ONLY thing that
164  * can unblock the current process is 'condition' becoming TRUE.
165  *
166  * Another form of usage is:
167  * struct l_wait_info lwi = LWI_TIMEOUT_INTERVAL(timeout, interval,
168  *                                               timeout_handler);
169  * rc = l_wait_event(waitq, condition, &lwi);
170  * This is the same as previous case, but condition is checked once every
171  * 'interval' jiffies (if non-zero).
172  *
173  * Subtle synchronization point: this macro does *not* necessary takes
174  * wait-queue spin-lock before returning, and, hence, following idiom is safe
175  * ONLY when caller provides some external locking:
176  *
177  *             Thread1                            Thread2
178  *
179  *   l_wait_event(&obj->wq, ....);                                       (1)
180  *
181  *                                    wake_up(&obj->wq):                 (2)
182  *                                         spin_lock(&q->lock);          (2.1)
183  *                                         __wake_up_common(q, ...);     (2.2)
184  *                                         spin_unlock(&q->lock, flags); (2.3)
185  *
186  *   OBD_FREE_PTR(obj);                                                  (3)
187  *
188  * As l_wait_event() may "short-cut" execution and return without taking
189  * wait-queue spin-lock, some additional synchronization is necessary to
190  * guarantee that step (3) can begin only after (2.3) finishes.
191  *
192  * XXX nikita: some ptlrpc daemon threads have races of that sort.
193  *
194  */
195 static inline int back_to_sleep(void *arg)
196 {
197         return 0;
198 }
199
200 #define LWI_ON_SIGNAL_NOOP ((void (*)(void *))(-1))
201
202 struct l_wait_info {
203         cfs_duration_t lwi_timeout;
204         cfs_duration_t lwi_interval;
205         int            lwi_allow_intr;
206         int  (*lwi_on_timeout)(void *);
207         void (*lwi_on_signal)(void *);
208         void  *lwi_cb_data;
209 };
210
211 /* NB: LWI_TIMEOUT ignores signals completely */
212 #define LWI_TIMEOUT(time, cb, data)             \
213 ((struct l_wait_info) {                         \
214         .lwi_timeout    = time,                 \
215         .lwi_on_timeout = cb,                   \
216         .lwi_cb_data    = data,                 \
217         .lwi_interval   = 0,                    \
218         .lwi_allow_intr = 0                     \
219 })
220
221 #define LWI_TIMEOUT_INTERVAL(time, interval, cb, data)  \
222 ((struct l_wait_info) {                                 \
223         .lwi_timeout    = time,                         \
224         .lwi_on_timeout = cb,                           \
225         .lwi_cb_data    = data,                         \
226         .lwi_interval   = interval,                     \
227         .lwi_allow_intr = 0                             \
228 })
229
230 #define LWI_TIMEOUT_INTR(time, time_cb, sig_cb, data)   \
231 ((struct l_wait_info) {                                 \
232         .lwi_timeout    = time,                         \
233         .lwi_on_timeout = time_cb,                      \
234         .lwi_on_signal  = sig_cb,                       \
235         .lwi_cb_data    = data,                         \
236         .lwi_interval   = 0,                            \
237         .lwi_allow_intr = 0                             \
238 })
239
240 #define LWI_TIMEOUT_INTR_ALL(time, time_cb, sig_cb, data)       \
241 ((struct l_wait_info) {                                         \
242         .lwi_timeout    = time,                                 \
243         .lwi_on_timeout = time_cb,                              \
244         .lwi_on_signal  = sig_cb,                               \
245         .lwi_cb_data    = data,                                 \
246         .lwi_interval   = 0,                                    \
247         .lwi_allow_intr = 1                                     \
248 })
249
250 #define LWI_INTR(cb, data)  LWI_TIMEOUT_INTR(0, NULL, cb, data)
251
252 #ifdef __KERNEL__
253
254 /*
255  * wait for @condition to become true, but no longer than timeout, specified
256  * by @info.
257  */
258 #define __l_wait_event(wq, condition, info, ret, l_add_wait)                   \
259 do {                                                                           \
260         wait_queue_t __wait;                                                   \
261         cfs_duration_t __timeout = info->lwi_timeout;                          \
262         sigset_t   __blocked;                                              \
263         int   __allow_intr = info->lwi_allow_intr;                             \
264                                                                                \
265         ret = 0;                                                               \
266         if (condition)                                                         \
267                 break;                                                         \
268                                                                                \
269         init_waitqueue_entry_current(&__wait);                                 \
270         l_add_wait(&wq, &__wait);                                              \
271                                                                                \
272         /* Block all signals (just the non-fatal ones if no timeout). */       \
273         if (info->lwi_on_signal != NULL && (__timeout == 0 || __allow_intr))   \
274                 __blocked = cfs_block_sigsinv(LUSTRE_FATAL_SIGS);              \
275         else                                                                   \
276                 __blocked = cfs_block_sigsinv(0);                              \
277                                                                                \
278         for (;;) {                                                             \
279                 unsigned       __wstate;                                       \
280                                                                                \
281                 __wstate = info->lwi_on_signal != NULL &&                      \
282                            (__timeout == 0 || __allow_intr) ?                  \
283                         TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;             \
284                                                                                \
285                 set_current_state(TASK_INTERRUPTIBLE);                         \
286                                                                                \
287                 if (condition)                                                 \
288                         break;                                                 \
289                                                                                \
290                 if (__timeout == 0) {                                          \
291                         waitq_wait(&__wait, __wstate);                         \
292                 } else {                                                       \
293                         cfs_duration_t interval = info->lwi_interval?          \
294                                              min_t(cfs_duration_t,             \
295                                                  info->lwi_interval,__timeout):\
296                                              __timeout;                        \
297                         cfs_duration_t remaining = waitq_timedwait(&__wait,    \
298                                                    __wstate,                   \
299                                                    interval);                  \
300                         __timeout = cfs_time_sub(__timeout,                    \
301                                             cfs_time_sub(interval, remaining));\
302                         if (__timeout == 0) {                                  \
303                                 if (info->lwi_on_timeout == NULL ||            \
304                                     info->lwi_on_timeout(info->lwi_cb_data)) { \
305                                         ret = -ETIMEDOUT;                      \
306                                         break;                                 \
307                                 }                                              \
308                                 /* Take signals after the timeout expires. */  \
309                                 if (info->lwi_on_signal != NULL)               \
310                                     (void)cfs_block_sigsinv(LUSTRE_FATAL_SIGS);\
311                         }                                                      \
312                 }                                                              \
313                                                                                \
314                 if (condition)                                                 \
315                         break;                                                 \
316                 if (cfs_signal_pending()) {                                    \
317                         if (info->lwi_on_signal != NULL &&                     \
318                             (__timeout == 0 || __allow_intr)) {                \
319                                 if (info->lwi_on_signal != LWI_ON_SIGNAL_NOOP) \
320                                         info->lwi_on_signal(info->lwi_cb_data);\
321                                 ret = -EINTR;                                  \
322                                 break;                                         \
323                         }                                                      \
324                         /* We have to do this here because some signals */     \
325                         /* are not blockable - ie from strace(1).       */     \
326                         /* In these cases we want to schedule_timeout() */     \
327                         /* again, because we don't want that to return  */     \
328                         /* -EINTR when the RPC actually succeeded.      */     \
329                         /* the recalc_sigpending() below will deliver the */   \
330                         /* signal properly.                             */     \
331                         cfs_clear_sigpending();                                \
332                 }                                                              \
333         }                                                                      \
334                                                                                \
335         cfs_restore_sigs(__blocked);                                           \
336                                                                                \
337         set_current_state(TASK_RUNNING);                                       \
338         remove_wait_queue(&wq, &__wait);                                       \
339 } while (0)
340
341 #else /* !__KERNEL__ */
342
343 #define __l_wait_event(wq, condition, info, ret, l_add_wait)            \
344 do {                                                                    \
345         long __timeout = info->lwi_timeout;                             \
346         long __now;                                                     \
347         long __then = 0;                                                \
348         int  __timed_out = 0;                                           \
349         int  __interval = obd_timeout;                                  \
350                                                                         \
351         ret = 0;                                                        \
352         if (condition)                                                  \
353                 break;                                                  \
354                                                                         \
355         if (__timeout != 0)                                             \
356                 __then = time(NULL);                                    \
357                                                                         \
358         if (__timeout && __timeout < __interval)                        \
359                 __interval = __timeout;                                 \
360         if (info->lwi_interval && info->lwi_interval < __interval)      \
361                 __interval = info->lwi_interval;                        \
362                                                                         \
363         while (!(condition)) {                                          \
364                 liblustre_wait_event(__interval);                       \
365                 if (condition)                                          \
366                         break;                                          \
367                                                                         \
368                 if (!__timed_out && info->lwi_timeout != 0) {           \
369                         __now = time(NULL);                             \
370                         __timeout -= __now - __then;                    \
371                         __then = __now;                                 \
372                                                                         \
373                         if (__timeout > 0)                              \
374                                 continue;                               \
375                                                                         \
376                         __timeout = 0;                                  \
377                         __timed_out = 1;                                \
378                         if (info->lwi_on_timeout == NULL ||             \
379                             info->lwi_on_timeout(info->lwi_cb_data)) {  \
380                                 ret = -ETIMEDOUT;                       \
381                                 break;                                  \
382                         }                                               \
383                 }                                                       \
384         }                                                               \
385 } while (0)
386
387 #endif /* __KERNEL__ */
388
389
390 #define l_wait_event(wq, condition, info)                       \
391 ({                                                              \
392         int                 __ret;                              \
393         struct l_wait_info *__info = (info);                    \
394                                                                 \
395         __l_wait_event(wq, condition, __info,                   \
396                        __ret, add_wait_queue);                  \
397         __ret;                                                  \
398 })
399
400 #define l_wait_event_exclusive(wq, condition, info)             \
401 ({                                                              \
402         int                 __ret;                              \
403         struct l_wait_info *__info = (info);                    \
404                                                                 \
405         __l_wait_event(wq, condition, __info,                   \
406                        __ret, add_wait_queue_exclusive);        \
407         __ret;                                                  \
408 })
409
410 #define l_wait_event_exclusive_head(wq, condition, info)        \
411 ({                                                              \
412         int                 __ret;                              \
413         struct l_wait_info *__info = (info);                    \
414                                                                 \
415         __l_wait_event(wq, condition, __info,                   \
416                        __ret, add_wait_queue_exclusive_head);   \
417         __ret;                                                  \
418 })
419
420 #define l_wait_condition(wq, condition)                         \
421 ({                                                              \
422         struct l_wait_info lwi = { 0 };                         \
423         l_wait_event(wq, condition, &lwi);                      \
424 })
425
426 #define l_wait_condition_exclusive(wq, condition)               \
427 ({                                                              \
428         struct l_wait_info lwi = { 0 };                         \
429         l_wait_event_exclusive(wq, condition, &lwi);            \
430 })
431
432 #define l_wait_condition_exclusive_head(wq, condition)          \
433 ({                                                              \
434         struct l_wait_info lwi = { 0 };                         \
435         l_wait_event_exclusive_head(wq, condition, &lwi);       \
436 })
437
438 #ifdef __KERNEL__
439 #define LIBLUSTRE_CLIENT (0)
440 #else
441 #define LIBLUSTRE_CLIENT (1)
442 #endif
443
444 /** @} lib */
445
446 #endif /* _LUSTRE_LIB_H */