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