Merge remote-tracking branch 'staging/staging-next'
[deliverable/linux.git] / drivers / staging / lustre / lustre / include / lustre_lib.h
CommitLineData
d7e09d03
PT
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
6a5b99a4 18 * http://www.gnu.org/licenses/gpl-2.0.html
d7e09d03 19 *
d7e09d03
PT
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, 2012, 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
376ef86b
JH
45#include <linux/sched.h>
46#include <linux/signal.h>
47#include <linux/types.h>
9fdaf8c0 48#include "../../include/linux/libcfs/libcfs.h"
1accaadf
GKH
49#include "lustre/lustre_idl.h"
50#include "lustre_ver.h"
51#include "lustre_cfg.h"
d7e09d03
PT
52
53/* target.c */
a9c7db39 54struct kstatfs;
d7e09d03
PT
55struct ptlrpc_request;
56struct obd_export;
57struct lu_target;
58struct l_wait_info;
1accaadf
GKH
59#include "lustre_ha.h"
60#include "lustre_net.h"
d7e09d03 61
376ef86b
JH
62#define LI_POISON 0x5a5a5a5a
63#if BITS_PER_LONG > 32
64# define LL_POISON 0x5a5a5a5a5a5a5a5aL
65#else
66# define LL_POISON 0x5a5a5a5aL
67#endif
68#define LP_POISON ((void *)LL_POISON)
d7e09d03
PT
69
70int target_pack_pool_reply(struct ptlrpc_request *req);
71int do_set_info_async(struct obd_import *imp,
72 int opcode, int version,
21aef7d9
OD
73 u32 keylen, void *key,
74 u32 vallen, void *val,
d7e09d03
PT
75 struct ptlrpc_request_set *set);
76
77#define OBD_RECOVERY_MAX_TIME (obd_timeout * 18) /* b13079 */
d7e09d03
PT
78
79void target_send_reply(struct ptlrpc_request *req, int rc, int fail_id);
80
81/* client.c */
82
9c234f6c 83int client_sanobd_setup(struct obd_device *obddev, struct lustre_cfg *lcfg);
d7e09d03
PT
84struct client_obd *client_conn2cli(struct lustre_handle *conn);
85
86struct md_open_data;
87struct obd_client_handle {
d3a8a4e2
JX
88 struct lustre_handle och_fh;
89 struct lu_fid och_fid;
90 struct md_open_data *och_mod;
91 struct lustre_handle och_lease_handle; /* open lock for lease */
92 __u32 och_magic;
93 fmode_t och_flags;
d7e09d03 94};
c9f6bb96 95
d7e09d03
PT
96#define OBD_CLIENT_HANDLE_MAGIC 0xd15ea5ed
97
98/* statfs_pack.c */
d7e09d03
PT
99void statfs_unpack(struct kstatfs *sfs, struct obd_statfs *osfs);
100
d7e09d03 101/* Until such time as we get_info the per-stripe maximum from the OST,
c56e256d
OD
102 * we define this to be 2T - 4k, which is the ext3 maxbytes.
103 */
d7e09d03
PT
104#define LUSTRE_STRIPE_MAXBYTES 0x1fffffff000ULL
105
106/* Special values for remove LOV EA from disk */
107#define LOVEA_DELETE_VALUES(size, count, offset) (size == 0 && count == 0 && \
108 offset == (typeof(offset))(-1))
109
2de35386 110#define LMVEA_DELETE_VALUES(count, offset) ((count) == 0 && \
111 (offset) == (typeof(offset))(-1))
d7e09d03
PT
112/* #define POISON_BULK 0 */
113
114/*
115 * l_wait_event is a flexible sleeping function, permitting simple caller
116 * configuration of interrupt and timeout sensitivity along with actions to
117 * be performed in the event of either exception.
118 *
119 * The first form of usage looks like this:
120 *
121 * struct l_wait_info lwi = LWI_TIMEOUT_INTR(timeout, timeout_handler,
122 * intr_handler, callback_data);
123 * rc = l_wait_event(waitq, condition, &lwi);
124 *
125 * l_wait_event() makes the current process wait on 'waitq' until 'condition'
126 * is TRUE or a "killable" signal (SIGTERM, SIKGILL, SIGINT) is pending. It
127 * returns 0 to signify 'condition' is TRUE, but if a signal wakes it before
128 * 'condition' becomes true, it optionally calls the specified 'intr_handler'
129 * if not NULL, and returns -EINTR.
130 *
131 * If a non-zero timeout is specified, signals are ignored until the timeout
132 * has expired. At this time, if 'timeout_handler' is not NULL it is called.
133 * If it returns FALSE l_wait_event() continues to wait as described above with
134 * signals enabled. Otherwise it returns -ETIMEDOUT.
135 *
136 * LWI_INTR(intr_handler, callback_data) is shorthand for
137 * LWI_TIMEOUT_INTR(0, NULL, intr_handler, callback_data)
138 *
139 * The second form of usage looks like this:
140 *
141 * struct l_wait_info lwi = LWI_TIMEOUT(timeout, timeout_handler);
142 * rc = l_wait_event(waitq, condition, &lwi);
143 *
144 * This form is the same as the first except that it COMPLETELY IGNORES
145 * SIGNALS. The caller must therefore beware that if 'timeout' is zero, or if
146 * 'timeout_handler' is not NULL and returns FALSE, then the ONLY thing that
147 * can unblock the current process is 'condition' becoming TRUE.
148 *
149 * Another form of usage is:
150 * struct l_wait_info lwi = LWI_TIMEOUT_INTERVAL(timeout, interval,
151 * timeout_handler);
152 * rc = l_wait_event(waitq, condition, &lwi);
153 * This is the same as previous case, but condition is checked once every
154 * 'interval' jiffies (if non-zero).
155 *
156 * Subtle synchronization point: this macro does *not* necessary takes
157 * wait-queue spin-lock before returning, and, hence, following idiom is safe
158 * ONLY when caller provides some external locking:
159 *
160 * Thread1 Thread2
161 *
162 * l_wait_event(&obj->wq, ....); (1)
163 *
164 * wake_up(&obj->wq): (2)
165 * spin_lock(&q->lock); (2.1)
166 * __wake_up_common(q, ...); (2.2)
167 * spin_unlock(&q->lock, flags); (2.3)
168 *
c0a2472f 169 * kfree(obj); (3)
d7e09d03
PT
170 *
171 * As l_wait_event() may "short-cut" execution and return without taking
172 * wait-queue spin-lock, some additional synchronization is necessary to
173 * guarantee that step (3) can begin only after (2.3) finishes.
174 *
175 * XXX nikita: some ptlrpc daemon threads have races of that sort.
176 *
177 */
178static inline int back_to_sleep(void *arg)
179{
180 return 0;
181}
182
183#define LWI_ON_SIGNAL_NOOP ((void (*)(void *))(-1))
184
185struct l_wait_info {
b2d201bd
GKH
186 long lwi_timeout;
187 long lwi_interval;
d7e09d03
PT
188 int lwi_allow_intr;
189 int (*lwi_on_timeout)(void *);
190 void (*lwi_on_signal)(void *);
191 void *lwi_cb_data;
192};
193
194/* NB: LWI_TIMEOUT ignores signals completely */
195#define LWI_TIMEOUT(time, cb, data) \
196((struct l_wait_info) { \
197 .lwi_timeout = time, \
198 .lwi_on_timeout = cb, \
199 .lwi_cb_data = data, \
200 .lwi_interval = 0, \
201 .lwi_allow_intr = 0 \
202})
203
204#define LWI_TIMEOUT_INTERVAL(time, interval, cb, data) \
205((struct l_wait_info) { \
206 .lwi_timeout = time, \
207 .lwi_on_timeout = cb, \
208 .lwi_cb_data = data, \
209 .lwi_interval = interval, \
210 .lwi_allow_intr = 0 \
211})
212
213#define LWI_TIMEOUT_INTR(time, time_cb, sig_cb, data) \
214((struct l_wait_info) { \
215 .lwi_timeout = time, \
216 .lwi_on_timeout = time_cb, \
217 .lwi_on_signal = sig_cb, \
218 .lwi_cb_data = data, \
219 .lwi_interval = 0, \
220 .lwi_allow_intr = 0 \
221})
222
223#define LWI_TIMEOUT_INTR_ALL(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 = 1 \
231})
232
233#define LWI_INTR(cb, data) LWI_TIMEOUT_INTR(0, NULL, cb, data)
234
376ef86b
JH
235#define LUSTRE_FATAL_SIGS (sigmask(SIGKILL) | sigmask(SIGINT) | \
236 sigmask(SIGTERM) | sigmask(SIGQUIT) | \
237 sigmask(SIGALRM))
238
564f5d6e
JS
239/**
240 * wait_queue_t of Linux (version < 2.6.34) is a FIFO list for exclusively
241 * waiting threads, which is not always desirable because all threads will
242 * be waken up again and again, even user only needs a few of them to be
243 * active most time. This is not good for performance because cache can
244 * be polluted by different threads.
245 *
246 * LIFO list can resolve this problem because we always wakeup the most
247 * recent active thread by default.
248 *
249 * NB: please don't call non-exclusive & exclusive wait on the same
250 * waitq if add_wait_queue_exclusive_head is used.
251 */
252#define add_wait_queue_exclusive_head(waitq, link) \
253{ \
254 unsigned long flags; \
255 \
256 spin_lock_irqsave(&((waitq)->lock), flags); \
257 __add_wait_queue_exclusive(waitq, link); \
258 spin_unlock_irqrestore(&((waitq)->lock), flags); \
259}
260
d7e09d03
PT
261/*
262 * wait for @condition to become true, but no longer than timeout, specified
263 * by @info.
264 */
265#define __l_wait_event(wq, condition, info, ret, l_add_wait) \
266do { \
267 wait_queue_t __wait; \
b2d201bd 268 long __timeout = info->lwi_timeout; \
d7e09d03
PT
269 sigset_t __blocked; \
270 int __allow_intr = info->lwi_allow_intr; \
271 \
272 ret = 0; \
273 if (condition) \
274 break; \
275 \
9e795d35 276 init_waitqueue_entry(&__wait, current); \
d7e09d03
PT
277 l_add_wait(&wq, &__wait); \
278 \
279 /* Block all signals (just the non-fatal ones if no timeout). */ \
d2a13989 280 if (info->lwi_on_signal && (__timeout == 0 || __allow_intr)) \
d7e09d03
PT
281 __blocked = cfs_block_sigsinv(LUSTRE_FATAL_SIGS); \
282 else \
283 __blocked = cfs_block_sigsinv(0); \
284 \
285 for (;;) { \
d7e09d03
PT
286 if (condition) \
287 break; \
288 \
65609bd6
OD
289 set_current_state(TASK_INTERRUPTIBLE); \
290 \
d7e09d03 291 if (__timeout == 0) { \
65609bd6 292 schedule(); \
d7e09d03 293 } else { \
b2952d62 294 long interval = info->lwi_interval ? \
b2d201bd 295 min_t(long, \
b2952d62 296 info->lwi_interval, __timeout) : \
d7e09d03 297 __timeout; \
b2d201bd 298 long remaining = schedule_timeout(interval);\
d7e09d03
PT
299 __timeout = cfs_time_sub(__timeout, \
300 cfs_time_sub(interval, remaining));\
301 if (__timeout == 0) { \
d2a13989 302 if (!info->lwi_on_timeout || \
d7e09d03
PT
303 info->lwi_on_timeout(info->lwi_cb_data)) { \
304 ret = -ETIMEDOUT; \
305 break; \
306 } \
307 /* Take signals after the timeout expires. */ \
d2a13989 308 if (info->lwi_on_signal) \
d7e09d03
PT
309 (void)cfs_block_sigsinv(LUSTRE_FATAL_SIGS);\
310 } \
311 } \
312 \
65609bd6
OD
313 set_current_state(TASK_RUNNING); \
314 \
d7e09d03
PT
315 if (condition) \
316 break; \
9af4826a 317 if (signal_pending(current)) { \
d2a13989 318 if (info->lwi_on_signal && \
d7e09d03
PT
319 (__timeout == 0 || __allow_intr)) { \
320 if (info->lwi_on_signal != LWI_ON_SIGNAL_NOOP) \
321 info->lwi_on_signal(info->lwi_cb_data);\
322 ret = -EINTR; \
323 break; \
324 } \
325 /* We have to do this here because some signals */ \
326 /* are not blockable - ie from strace(1). */ \
327 /* In these cases we want to schedule_timeout() */ \
328 /* again, because we don't want that to return */ \
329 /* -EINTR when the RPC actually succeeded. */ \
330 /* the recalc_sigpending() below will deliver the */ \
331 /* signal properly. */ \
332 cfs_clear_sigpending(); \
333 } \
334 } \
335 \
336 cfs_restore_sigs(__blocked); \
337 \
d7e09d03
PT
338 remove_wait_queue(&wq, &__wait); \
339} while (0)
340
d7e09d03
PT
341#define l_wait_event(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); \
348 __ret; \
349})
350
351#define l_wait_event_exclusive(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); \
358 __ret; \
359})
360
361#define l_wait_event_exclusive_head(wq, condition, info) \
362({ \
363 int __ret; \
364 struct l_wait_info *__info = (info); \
365 \
366 __l_wait_event(wq, condition, __info, \
367 __ret, add_wait_queue_exclusive_head); \
368 __ret; \
369})
370
371#define l_wait_condition(wq, condition) \
372({ \
373 struct l_wait_info lwi = { 0 }; \
374 l_wait_event(wq, condition, &lwi); \
375})
376
377#define l_wait_condition_exclusive(wq, condition) \
378({ \
379 struct l_wait_info lwi = { 0 }; \
380 l_wait_event_exclusive(wq, condition, &lwi); \
381})
382
383#define l_wait_condition_exclusive_head(wq, condition) \
384({ \
385 struct l_wait_info lwi = { 0 }; \
386 l_wait_event_exclusive_head(wq, condition, &lwi); \
387})
388
389#define LIBLUSTRE_CLIENT (0)
390
391/** @} lib */
392
393#endif /* _LUSTRE_LIB_H */
This page took 0.561941 seconds and 5 git commands to generate.