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