1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42#ifndef __LIBCFS_PRIVATE_H__
43#define __LIBCFS_PRIVATE_H__
44
45
46#include "../lnet/types.h"
47
48#ifndef DEBUG_SUBSYSTEM
49# define DEBUG_SUBSYSTEM S_UNDEFINED
50#endif
51
52
53
54
55
56
57
58#define LASSERT_CHECKED (0)
59
60#define LASSERTF(cond, fmt, ...) \
61do { \
62 if (unlikely(!(cond))) { \
63 LIBCFS_DEBUG_MSG_DATA_DECL(__msg_data, D_EMERG, NULL); \
64 libcfs_debug_msg(&__msg_data, \
65 "ASSERTION( %s ) failed: " fmt, #cond, \
66 ## __VA_ARGS__); \
67 lbug_with_loc(&__msg_data); \
68 } \
69} while (0)
70
71#define LASSERT(cond) LASSERTF(cond, "\n")
72
73#ifdef CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK
74
75
76
77
78
79# define LINVRNT(exp) LASSERT(exp)
80#else
81# define LINVRNT(exp) ((void)sizeof !!(exp))
82#endif
83
84#define KLASSERT(e) LASSERT(e)
85
86void lbug_with_loc(struct libcfs_debug_msg_data *)__attribute__((noreturn));
87
88#define LBUG() \
89do { \
90 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_EMERG, NULL); \
91 lbug_with_loc(&msgdata); \
92} while (0)
93
94extern atomic_t libcfs_kmemory;
95
96
97
98
99# define libcfs_kmem_inc(ptr, size) \
100do { \
101 atomic_add(size, &libcfs_kmemory); \
102} while (0)
103
104# define libcfs_kmem_dec(ptr, size) \
105do { \
106 atomic_sub(size, &libcfs_kmemory); \
107} while (0)
108
109# define libcfs_kmem_read() \
110 atomic_read(&libcfs_kmemory)
111
112#ifndef LIBCFS_VMALLOC_SIZE
113#define LIBCFS_VMALLOC_SIZE (2 << PAGE_CACHE_SHIFT)
114#endif
115
116#define LIBCFS_ALLOC_PRE(size, mask) \
117do { \
118 LASSERT(!in_interrupt() || \
119 ((size) <= LIBCFS_VMALLOC_SIZE && \
120 ((mask) & __GFP_WAIT) == 0)); \
121} while (0)
122
123#define LIBCFS_ALLOC_POST(ptr, size) \
124do { \
125 if (unlikely((ptr) == NULL)) { \
126 CERROR("LNET: out of memory at %s:%d (tried to alloc '" \
127 #ptr "' = %d)\n", __FILE__, __LINE__, (int)(size)); \
128 CERROR("LNET: %d total bytes allocated by lnet\n", \
129 libcfs_kmem_read()); \
130 } else { \
131 memset((ptr), 0, (size)); \
132 libcfs_kmem_inc((ptr), (size)); \
133 CDEBUG(D_MALLOC, "alloc '" #ptr "': %d at %p (tot %d).\n", \
134 (int)(size), (ptr), libcfs_kmem_read()); \
135 } \
136} while (0)
137
138
139
140
141#define LIBCFS_ALLOC_GFP(ptr, size, mask) \
142do { \
143 LIBCFS_ALLOC_PRE((size), (mask)); \
144 (ptr) = (size) <= LIBCFS_VMALLOC_SIZE ? \
145 kmalloc((size), (mask)) : vmalloc(size); \
146 LIBCFS_ALLOC_POST((ptr), (size)); \
147} while (0)
148
149
150
151
152#define LIBCFS_ALLOC(ptr, size) \
153 LIBCFS_ALLOC_GFP(ptr, size, GFP_NOFS)
154
155
156
157
158#define LIBCFS_ALLOC_ATOMIC(ptr, size) \
159 LIBCFS_ALLOC_GFP(ptr, size, GFP_ATOMIC)
160
161
162
163
164
165
166#define LIBCFS_CPT_ALLOC_GFP(ptr, cptab, cpt, size, mask) \
167do { \
168 LIBCFS_ALLOC_PRE((size), (mask)); \
169 (ptr) = (size) <= LIBCFS_VMALLOC_SIZE ? \
170 kmalloc_node((size), (mask), cfs_cpt_spread_node(cptab, cpt)) :\
171 vmalloc_node(size, cfs_cpt_spread_node(cptab, cpt)); \
172 LIBCFS_ALLOC_POST((ptr), (size)); \
173} while (0)
174
175
176#define LIBCFS_CPT_ALLOC(ptr, cptab, cpt, size) \
177 LIBCFS_CPT_ALLOC_GFP(ptr, cptab, cpt, size, GFP_NOFS)
178
179#define LIBCFS_FREE(ptr, size) \
180do { \
181 int s = (size); \
182 if (unlikely((ptr) == NULL)) { \
183 CERROR("LIBCFS: free NULL '" #ptr "' (%d bytes) at " \
184 "%s:%d\n", s, __FILE__, __LINE__); \
185 break; \
186 } \
187 libcfs_kmem_dec((ptr), s); \
188 CDEBUG(D_MALLOC, "kfreed '" #ptr "': %d at %p (tot %d).\n", \
189 s, (ptr), libcfs_kmem_read()); \
190 if (unlikely(s > LIBCFS_VMALLOC_SIZE)) \
191 vfree(ptr); \
192 else \
193 kfree(ptr); \
194} while (0)
195
196
197
198
199#if defined(__GNUC__) && (__GNUC__ >= 2) && !defined(__OPTIMIZE__)
200#define ___htonl(x) __cpu_to_be32(x)
201#define ___htons(x) __cpu_to_be16(x)
202#define ___ntohl(x) __be32_to_cpu(x)
203#define ___ntohs(x) __be16_to_cpu(x)
204#define htonl(x) ___htonl(x)
205#define ntohl(x) ___ntohl(x)
206#define htons(x) ___htons(x)
207#define ntohs(x) ___ntohs(x)
208#endif
209
210void libcfs_run_upcall(char **argv);
211void libcfs_run_lbug_upcall(struct libcfs_debug_msg_data *);
212void libcfs_debug_dumplog(void);
213int libcfs_debug_init(unsigned long bufsize);
214int libcfs_debug_cleanup(void);
215int libcfs_debug_clear_buffer(void);
216int libcfs_debug_mark_buffer(const char *text);
217
218void libcfs_debug_set_level(unsigned int debug_level);
219
220
221
222
223
224
225
226void *cfs_percpt_alloc(struct cfs_cpt_table *cptab, unsigned int size);
227
228
229
230void cfs_percpt_free(void *vars);
231int cfs_percpt_number(void *vars);
232void *cfs_percpt_current(void *vars);
233void *cfs_percpt_index(void *vars, int idx);
234
235#define cfs_percpt_for_each(var, i, vars) \
236 for (i = 0; i < cfs_percpt_number(vars) && \
237 ((var) = (vars)[i]) != NULL; i++)
238
239
240
241
242
243void *cfs_array_alloc(int count, unsigned int size);
244void cfs_array_free(void *vars);
245
246#define LASSERT_ATOMIC_ENABLED (1)
247
248#if LASSERT_ATOMIC_ENABLED
249
250
251#define LASSERT_ATOMIC_EQ(a, v) \
252do { \
253 LASSERTF(atomic_read(a) == v, \
254 "value: %d\n", atomic_read((a))); \
255} while (0)
256
257
258#define LASSERT_ATOMIC_NE(a, v) \
259do { \
260 LASSERTF(atomic_read(a) != v, \
261 "value: %d\n", atomic_read((a))); \
262} while (0)
263
264
265#define LASSERT_ATOMIC_LT(a, v) \
266do { \
267 LASSERTF(atomic_read(a) < v, \
268 "value: %d\n", atomic_read((a))); \
269} while (0)
270
271
272#define LASSERT_ATOMIC_LE(a, v) \
273do { \
274 LASSERTF(atomic_read(a) <= v, \
275 "value: %d\n", atomic_read((a))); \
276} while (0)
277
278
279#define LASSERT_ATOMIC_GT(a, v) \
280do { \
281 LASSERTF(atomic_read(a) > v, \
282 "value: %d\n", atomic_read((a))); \
283} while (0)
284
285
286#define LASSERT_ATOMIC_GE(a, v) \
287do { \
288 LASSERTF(atomic_read(a) >= v, \
289 "value: %d\n", atomic_read((a))); \
290} while (0)
291
292
293#define LASSERT_ATOMIC_GT_LT(a, v1, v2) \
294do { \
295 int __v = atomic_read(a); \
296 LASSERTF(__v > v1 && __v < v2, "value: %d\n", __v); \
297} while (0)
298
299
300#define LASSERT_ATOMIC_GT_LE(a, v1, v2) \
301do { \
302 int __v = atomic_read(a); \
303 LASSERTF(__v > v1 && __v <= v2, "value: %d\n", __v); \
304} while (0)
305
306
307#define LASSERT_ATOMIC_GE_LT(a, v1, v2) \
308do { \
309 int __v = atomic_read(a); \
310 LASSERTF(__v >= v1 && __v < v2, "value: %d\n", __v); \
311} while (0)
312
313
314#define LASSERT_ATOMIC_GE_LE(a, v1, v2) \
315do { \
316 int __v = atomic_read(a); \
317 LASSERTF(__v >= v1 && __v <= v2, "value: %d\n", __v); \
318} while (0)
319
320#else
321
322#define LASSERT_ATOMIC_EQ(a, v) do {} while (0)
323#define LASSERT_ATOMIC_NE(a, v) do {} while (0)
324#define LASSERT_ATOMIC_LT(a, v) do {} while (0)
325#define LASSERT_ATOMIC_LE(a, v) do {} while (0)
326#define LASSERT_ATOMIC_GT(a, v) do {} while (0)
327#define LASSERT_ATOMIC_GE(a, v) do {} while (0)
328#define LASSERT_ATOMIC_GT_LT(a, v1, v2) do {} while (0)
329#define LASSERT_ATOMIC_GT_LE(a, v1, v2) do {} while (0)
330#define LASSERT_ATOMIC_GE_LT(a, v1, v2) do {} while (0)
331#define LASSERT_ATOMIC_GE_LE(a, v1, v2) do {} while (0)
332
333#endif
334
335#define LASSERT_ATOMIC_ZERO(a) LASSERT_ATOMIC_EQ(a, 0)
336#define LASSERT_ATOMIC_POS(a) LASSERT_ATOMIC_GT(a, 0)
337
338#define CFS_ALLOC_PTR(ptr) LIBCFS_ALLOC(ptr, sizeof(*(ptr)))
339#define CFS_FREE_PTR(ptr) LIBCFS_FREE(ptr, sizeof(*(ptr)))
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358enum {
359 CFS_PERCPT_LOCK_EX = -1,
360};
361
362struct cfs_percpt_lock {
363
364 struct cfs_cpt_table *pcl_cptab;
365
366 unsigned int pcl_locked;
367
368 spinlock_t **pcl_locks;
369};
370
371
372static inline int
373cfs_percpt_lock_num(struct cfs_percpt_lock *pcl)
374{
375 return cfs_cpt_number(pcl->pcl_cptab);
376}
377
378
379
380
381
382struct cfs_percpt_lock *cfs_percpt_lock_alloc(struct cfs_cpt_table *cptab);
383
384void cfs_percpt_lock_free(struct cfs_percpt_lock *pcl);
385
386
387void cfs_percpt_lock(struct cfs_percpt_lock *pcl, int index);
388
389void cfs_percpt_unlock(struct cfs_percpt_lock *pcl, int index);
390
391atomic_t **cfs_percpt_atomic_alloc(struct cfs_cpt_table *cptab, int val);
392
393void cfs_percpt_atomic_free(atomic_t **refs);
394
395int cfs_percpt_atomic_summary(atomic_t **refs);
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411#define CLASSERT(cond) do {switch (42) {case (cond): case 0: break; } } while (0)
412
413
414int libcfs_isknown_lnd(int type);
415char *libcfs_lnd2modname(int type);
416char *libcfs_lnd2str(int type);
417int libcfs_str2lnd(const char *str);
418char *libcfs_net2str(__u32 net);
419char *libcfs_nid2str(lnet_nid_t nid);
420__u32 libcfs_str2net(const char *str);
421lnet_nid_t libcfs_str2nid(const char *str);
422int libcfs_str2anynid(lnet_nid_t *nid, const char *str);
423char *libcfs_id2str(lnet_process_id_t id);
424void cfs_free_nidlist(struct list_head *list);
425int cfs_parse_nidlist(char *str, int len, struct list_head *list);
426int cfs_match_nid(lnet_nid_t nid, struct list_head *list);
427
428
429
430
431
432#define LNET_NIDADDR(nid) ((__u32)((nid) & 0xffffffff))
433
434#define LNET_NIDNET(nid) ((__u32)(((nid) >> 32)) & 0xffffffff)
435
436#define LNET_MKNID(net, addr) ((((__u64)(net))<<32)|((__u64)(addr)))
437
438#define LNET_NETNUM(net) ((net) & 0xffff)
439#define LNET_NETTYP(net) (((net) >> 16) & 0xffff)
440#define LNET_MKNET(typ, num) ((((__u32)(typ))<<16)|((__u32)(num)))
441
442
443
444#define MAX_NUMERIC_VALUE 0xffffffff
445
446
447#define ergo(a, b) (!(a) || (b))
448
449#define equi(a, b) (!!(a) == !!(b))
450
451
452
453
454
455
456struct libcfs_device_userstate {
457 int ldu_memhog_pages;
458 struct page *ldu_memhog_root_page;
459};
460
461#define MKSTR(ptr) ((ptr)) ? (ptr) : ""
462
463static inline int cfs_size_round4(int val)
464{
465 return (val + 3) & (~0x3);
466}
467
468#ifndef HAVE_CFS_SIZE_ROUND
469static inline int cfs_size_round(int val)
470{
471 return (val + 7) & (~0x7);
472}
473
474#define HAVE_CFS_SIZE_ROUND
475#endif
476
477static inline int cfs_size_round16(int val)
478{
479 return (val + 0xf) & (~0xf);
480}
481
482static inline int cfs_size_round32(int val)
483{
484 return (val + 0x1f) & (~0x1f);
485}
486
487static inline int cfs_size_round0(int val)
488{
489 if (!val)
490 return 0;
491 return (val + 1 + 7) & (~0x7);
492}
493
494static inline size_t cfs_round_strlen(char *fset)
495{
496 return (size_t)cfs_size_round((int)strlen(fset) + 1);
497}
498
499
500static inline unsigned int cfs_power2_roundup(unsigned int val)
501{
502 if (val != LOWEST_BIT_SET(val)) {
503 do {
504 val &= ~LOWEST_BIT_SET(val);
505 } while (val != LOWEST_BIT_SET(val));
506
507 val <<= 1;
508 }
509 return val;
510}
511
512#define LOGL(var, len, ptr) \
513do { \
514 if (var) \
515 memcpy((char *)ptr, (const char *)var, len); \
516 ptr += cfs_size_round(len); \
517} while (0)
518
519#define LOGU(var, len, ptr) \
520do { \
521 if (var) \
522 memcpy((char *)var, (const char *)ptr, len); \
523 ptr += cfs_size_round(len); \
524} while (0)
525
526#define LOGL0(var, len, ptr) \
527do { \
528 if (!len) \
529 break; \
530 memcpy((char *)ptr, (const char *)var, len); \
531 *((char *)(ptr) + len) = 0; \
532 ptr += cfs_size_round(len + 1); \
533} while (0)
534
535
536
537
538enum {
539
540
541 QSWLND = 1,
542 SOCKLND = 2,
543 GMLND = 3,
544 PTLLND = 4,
545 O2IBLND = 5,
546 CIBLND = 6,
547 OPENIBLND = 7,
548 IIBLND = 8,
549 LOLND = 9,
550 RALND = 10,
551 VIBLND = 11,
552 MXLND = 12,
553 GNILND = 13,
554};
555
556#endif
557