linux/drivers/staging/lustre/include/linux/libcfs/libcfs_private.h
<<
>>
Prefs
   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) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  28 * Use is subject to license terms.
  29 *
  30 * Copyright (c) 2011, 2012, 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 * libcfs/include/libcfs/libcfs_private.h
  37 *
  38 * Various defines for libcfs.
  39 *
  40 */
  41
  42#ifndef __LIBCFS_PRIVATE_H__
  43#define __LIBCFS_PRIVATE_H__
  44
  45/* XXX this layering violation is for nidstrings */
  46#include <linux/lnet/types.h>
  47
  48#ifndef DEBUG_SUBSYSTEM
  49# define DEBUG_SUBSYSTEM S_UNDEFINED
  50#endif
  51
  52
  53
  54/*
  55 * When this is on, LASSERT macro includes check for assignment used instead
  56 * of equality check, but doesn't have unlikely(). Turn this on from time to
  57 * time to make test-builds. This shouldn't be on for production release.
  58 */
  59#define LASSERT_CHECKED (0)
  60
  61
  62#define LASSERTF(cond, fmt, ...)                                        \
  63do {                                                                    \
  64        if (unlikely(!(cond))) {                                        \
  65                LIBCFS_DEBUG_MSG_DATA_DECL(__msg_data, D_EMERG, NULL);  \
  66                libcfs_debug_msg(&__msg_data,                           \
  67                                 "ASSERTION( %s ) failed: " fmt, #cond, \
  68                                 ## __VA_ARGS__);                       \
  69                lbug_with_loc(&__msg_data);                             \
  70        }                                                               \
  71} while (0)
  72
  73#define LASSERT(cond) LASSERTF(cond, "\n")
  74
  75#ifdef CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK
  76/**
  77 * This is for more expensive checks that one doesn't want to be enabled all
  78 * the time. LINVRNT() has to be explicitly enabled by
  79 * CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK option.
  80 */
  81# define LINVRNT(exp) LASSERT(exp)
  82#else
  83# define LINVRNT(exp) ((void)sizeof!!(exp))
  84#endif
  85
  86#define KLASSERT(e) LASSERT(e)
  87
  88void lbug_with_loc(struct libcfs_debug_msg_data *) __attribute__((noreturn));
  89
  90#define LBUG()                                                    \
  91do {                                                                \
  92        LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_EMERG, NULL);          \
  93        lbug_with_loc(&msgdata);                                        \
  94} while(0)
  95
  96extern atomic_t libcfs_kmemory;
  97/*
  98 * Memory
  99 */
 100
 101# define libcfs_kmem_inc(ptr, size)             \
 102do {                                            \
 103        atomic_add(size, &libcfs_kmemory);      \
 104} while (0)
 105
 106# define libcfs_kmem_dec(ptr, size)             \
 107do {                                            \
 108        atomic_sub(size, &libcfs_kmemory);      \
 109} while (0)
 110
 111# define libcfs_kmem_read()                     \
 112        atomic_read(&libcfs_kmemory)
 113
 114
 115#ifndef LIBCFS_VMALLOC_SIZE
 116#define LIBCFS_VMALLOC_SIZE     (2 << PAGE_CACHE_SHIFT) /* 2 pages */
 117#endif
 118
 119#define LIBCFS_ALLOC_PRE(size, mask)                                        \
 120do {                                                                        \
 121        LASSERT(!in_interrupt() ||                                          \
 122                ((size) <= LIBCFS_VMALLOC_SIZE &&                           \
 123                 ((mask) & GFP_ATOMIC)) != 0);                      \
 124} while (0)
 125
 126#define LIBCFS_ALLOC_POST(ptr, size)                                        \
 127do {                                                                        \
 128        if (unlikely((ptr) == NULL)) {                                      \
 129                CERROR("LNET: out of memory at %s:%d (tried to alloc '"     \
 130                       #ptr "' = %d)\n", __FILE__, __LINE__, (int)(size));  \
 131                CERROR("LNET: %d total bytes allocated by lnet\n",          \
 132                       libcfs_kmem_read());                                 \
 133        } else {                                                            \
 134                memset((ptr), 0, (size));                                   \
 135                libcfs_kmem_inc((ptr), (size));                             \
 136                CDEBUG(D_MALLOC, "alloc '" #ptr "': %d at %p (tot %d).\n",  \
 137                       (int)(size), (ptr), libcfs_kmem_read());             \
 138        }                                                                  \
 139} while (0)
 140
 141/**
 142 * allocate memory with GFP flags @mask
 143 */
 144#define LIBCFS_ALLOC_GFP(ptr, size, mask)                                   \
 145do {                                                                        \
 146        LIBCFS_ALLOC_PRE((size), (mask));                                   \
 147        (ptr) = (size) <= LIBCFS_VMALLOC_SIZE ?                             \
 148                kmalloc((size), (mask)) : vmalloc(size);            \
 149        LIBCFS_ALLOC_POST((ptr), (size));                                   \
 150} while (0)
 151
 152/**
 153 * default allocator
 154 */
 155#define LIBCFS_ALLOC(ptr, size) \
 156        LIBCFS_ALLOC_GFP(ptr, size, __GFP_IO)
 157
 158/**
 159 * non-sleeping allocator
 160 */
 161#define LIBCFS_ALLOC_ATOMIC(ptr, size) \
 162        LIBCFS_ALLOC_GFP(ptr, size, GFP_ATOMIC)
 163
 164/**
 165 * allocate memory for specified CPU partition
 166 *   \a cptab != NULL, \a cpt is CPU partition id of \a cptab
 167 *   \a cptab == NULL, \a cpt is HW NUMA node id
 168 */
 169#define LIBCFS_CPT_ALLOC_GFP(ptr, cptab, cpt, size, mask)                   \
 170do {                                                                        \
 171        LIBCFS_ALLOC_PRE((size), (mask));                                   \
 172        (ptr) = (size) <= LIBCFS_VMALLOC_SIZE ?                             \
 173                kmalloc_node((size), (mask), cfs_cpt_spread_node(cptab, cpt)) :\
 174                vmalloc_node(size, cfs_cpt_spread_node(cptab, cpt));        \
 175        LIBCFS_ALLOC_POST((ptr), (size));                                   \
 176} while (0)
 177
 178/** default numa allocator */
 179#define LIBCFS_CPT_ALLOC(ptr, cptab, cpt, size)                             \
 180        LIBCFS_CPT_ALLOC_GFP(ptr, cptab, cpt, size, __GFP_IO)
 181
 182#define LIBCFS_FREE(ptr, size)                                    \
 183do {                                                                \
 184        int s = (size);                                          \
 185        if (unlikely((ptr) == NULL)) {                            \
 186                CERROR("LIBCFS: free NULL '" #ptr "' (%d bytes) at "    \
 187                       "%s:%d\n", s, __FILE__, __LINE__);              \
 188                break;                                            \
 189        }                                                              \
 190        libcfs_kmem_dec((ptr), s);                                    \
 191        CDEBUG(D_MALLOC, "kfreed '" #ptr "': %d at %p (tot %d).\n",     \
 192               s, (ptr), libcfs_kmem_read());                           \
 193        if (unlikely(s > LIBCFS_VMALLOC_SIZE))                    \
 194                vfree(ptr);                                 \
 195        else                                                        \
 196                kfree(ptr);                                       \
 197} while (0)
 198
 199/******************************************************************************/
 200
 201/* htonl hack - either this, or compile with -O2. Stupid byteorder/generic.h */
 202#if defined(__GNUC__) && (__GNUC__ >= 2) && !defined(__OPTIMIZE__)
 203#define ___htonl(x) __cpu_to_be32(x)
 204#define ___htons(x) __cpu_to_be16(x)
 205#define ___ntohl(x) __be32_to_cpu(x)
 206#define ___ntohs(x) __be16_to_cpu(x)
 207#define htonl(x) ___htonl(x)
 208#define ntohl(x) ___ntohl(x)
 209#define htons(x) ___htons(x)
 210#define ntohs(x) ___ntohs(x)
 211#endif
 212
 213void libcfs_debug_dumpstack(task_t *tsk);
 214void libcfs_run_upcall(char **argv);
 215void libcfs_run_lbug_upcall(struct libcfs_debug_msg_data *);
 216void libcfs_debug_dumplog(void);
 217int libcfs_debug_init(unsigned long bufsize);
 218int libcfs_debug_cleanup(void);
 219int libcfs_debug_clear_buffer(void);
 220int libcfs_debug_mark_buffer(const char *text);
 221
 222void libcfs_debug_set_level(unsigned int debug_level);
 223
 224
 225/*
 226 * allocate per-cpu-partition data, returned value is an array of pointers,
 227 * variable can be indexed by CPU ID.
 228 *      cptable != NULL: size of array is number of CPU partitions
 229 *      cptable == NULL: size of array is number of HW cores
 230 */
 231void *cfs_percpt_alloc(struct cfs_cpt_table *cptab, unsigned int size);
 232/*
 233 * destory per-cpu-partition variable
 234 */
 235void  cfs_percpt_free(void *vars);
 236int   cfs_percpt_number(void *vars);
 237void *cfs_percpt_current(void *vars);
 238void *cfs_percpt_index(void *vars, int idx);
 239
 240#define cfs_percpt_for_each(var, i, vars)               \
 241        for (i = 0; i < cfs_percpt_number(vars) &&      \
 242                    ((var) = (vars)[i]) != NULL; i++)
 243
 244/*
 245 * allocate a variable array, returned value is an array of pointers.
 246 * Caller can specify length of array by count.
 247 */
 248void *cfs_array_alloc(int count, unsigned int size);
 249void  cfs_array_free(void *vars);
 250
 251#define LASSERT_ATOMIC_ENABLED    (1)
 252
 253#if LASSERT_ATOMIC_ENABLED
 254
 255/** assert value of @a is equal to @v */
 256#define LASSERT_ATOMIC_EQ(a, v)                          \
 257do {                                                        \
 258        LASSERTF(atomic_read(a) == v,                  \
 259                 "value: %d\n", atomic_read((a)));        \
 260} while (0)
 261
 262/** assert value of @a is unequal to @v */
 263#define LASSERT_ATOMIC_NE(a, v)                          \
 264do {                                                        \
 265        LASSERTF(atomic_read(a) != v,                  \
 266                 "value: %d\n", atomic_read((a)));        \
 267} while (0)
 268
 269/** assert value of @a is little than @v */
 270#define LASSERT_ATOMIC_LT(a, v)                          \
 271do {                                                        \
 272        LASSERTF(atomic_read(a) < v,                    \
 273                 "value: %d\n", atomic_read((a)));        \
 274} while (0)
 275
 276/** assert value of @a is little/equal to @v */
 277#define LASSERT_ATOMIC_LE(a, v)                          \
 278do {                                                        \
 279        LASSERTF(atomic_read(a) <= v,                  \
 280                 "value: %d\n", atomic_read((a)));        \
 281} while (0)
 282
 283/** assert value of @a is great than @v */
 284#define LASSERT_ATOMIC_GT(a, v)                          \
 285do {                                                        \
 286        LASSERTF(atomic_read(a) > v,                    \
 287                 "value: %d\n", atomic_read((a)));        \
 288} while (0)
 289
 290/** assert value of @a is great/equal to @v */
 291#define LASSERT_ATOMIC_GE(a, v)                          \
 292do {                                                        \
 293        LASSERTF(atomic_read(a) >= v,                  \
 294                 "value: %d\n", atomic_read((a)));        \
 295} while (0)
 296
 297/** assert value of @a is great than @v1 and little than @v2 */
 298#define LASSERT_ATOMIC_GT_LT(a, v1, v2)                  \
 299do {                                                        \
 300        int __v = atomic_read(a);                          \
 301        LASSERTF(__v > v1 && __v < v2, "value: %d\n", __v);     \
 302} while (0)
 303
 304/** assert value of @a is great than @v1 and little/equal to @v2 */
 305#define LASSERT_ATOMIC_GT_LE(a, v1, v2)                  \
 306do {                                                        \
 307        int __v = atomic_read(a);                          \
 308        LASSERTF(__v > v1 && __v <= v2, "value: %d\n", __v);    \
 309} while (0)
 310
 311/** assert value of @a is great/equal to @v1 and little than @v2 */
 312#define LASSERT_ATOMIC_GE_LT(a, v1, v2)                  \
 313do {                                                        \
 314        int __v = atomic_read(a);                          \
 315        LASSERTF(__v >= v1 && __v < v2, "value: %d\n", __v);    \
 316} while (0)
 317
 318/** assert value of @a is great/equal to @v1 and little/equal to @v2 */
 319#define LASSERT_ATOMIC_GE_LE(a, v1, v2)                  \
 320do {                                                        \
 321        int __v = atomic_read(a);                          \
 322        LASSERTF(__v >= v1 && __v <= v2, "value: %d\n", __v);   \
 323} while (0)
 324
 325#else /* !LASSERT_ATOMIC_ENABLED */
 326
 327#define LASSERT_ATOMIC_EQ(a, v)          do {} while (0)
 328#define LASSERT_ATOMIC_NE(a, v)          do {} while (0)
 329#define LASSERT_ATOMIC_LT(a, v)          do {} while (0)
 330#define LASSERT_ATOMIC_LE(a, v)          do {} while (0)
 331#define LASSERT_ATOMIC_GT(a, v)          do {} while (0)
 332#define LASSERT_ATOMIC_GE(a, v)          do {} while (0)
 333#define LASSERT_ATOMIC_GT_LT(a, v1, v2)  do {} while (0)
 334#define LASSERT_ATOMIC_GT_LE(a, v1, v2)  do {} while (0)
 335#define LASSERT_ATOMIC_GE_LT(a, v1, v2)  do {} while (0)
 336#define LASSERT_ATOMIC_GE_LE(a, v1, v2)  do {} while (0)
 337
 338#endif /* LASSERT_ATOMIC_ENABLED */
 339
 340#define LASSERT_ATOMIC_ZERO(a)            LASSERT_ATOMIC_EQ(a, 0)
 341#define LASSERT_ATOMIC_POS(a)              LASSERT_ATOMIC_GT(a, 0)
 342
 343#define CFS_ALLOC_PTR(ptr)      LIBCFS_ALLOC(ptr, sizeof (*(ptr)));
 344#define CFS_FREE_PTR(ptr)       LIBCFS_FREE(ptr, sizeof (*(ptr)));
 345
 346/*
 347 * percpu partition lock
 348 *
 349 * There are some use-cases like this in Lustre:
 350 * . each CPU partition has it's own private data which is frequently changed,
 351 *   and mostly by the local CPU partition.
 352 * . all CPU partitions share some global data, these data are rarely changed.
 353 *
 354 * LNet is typical example.
 355 * CPU partition lock is designed for this kind of use-cases:
 356 * . each CPU partition has it's own private lock
 357 * . change on private data just needs to take the private lock
 358 * . read on shared data just needs to take _any_ of private locks
 359 * . change on shared data needs to take _all_ private locks,
 360 *   which is slow and should be really rare.
 361 */
 362
 363enum {
 364        CFS_PERCPT_LOCK_EX      = -1, /* negative */
 365};
 366
 367
 368struct cfs_percpt_lock {
 369        /* cpu-partition-table for this lock */
 370        struct cfs_cpt_table    *pcl_cptab;
 371        /* exclusively locked */
 372        unsigned int            pcl_locked;
 373        /* private lock table */
 374        spinlock_t              **pcl_locks;
 375};
 376
 377/* return number of private locks */
 378static inline int
 379cfs_percpt_lock_num(struct cfs_percpt_lock *pcl)
 380{
 381        return cfs_cpt_number(pcl->pcl_cptab);
 382}
 383
 384
 385/*
 386 * create a cpu-partition lock based on CPU partition table \a cptab,
 387 * each private lock has extra \a psize bytes padding data
 388 */
 389struct cfs_percpt_lock *cfs_percpt_lock_alloc(struct cfs_cpt_table *cptab);
 390/* destroy a cpu-partition lock */
 391void cfs_percpt_lock_free(struct cfs_percpt_lock *pcl);
 392
 393/* lock private lock \a index of \a pcl */
 394void cfs_percpt_lock(struct cfs_percpt_lock *pcl, int index);
 395/* unlock private lock \a index of \a pcl */
 396void cfs_percpt_unlock(struct cfs_percpt_lock *pcl, int index);
 397/* create percpt (atomic) refcount based on @cptab */
 398atomic_t **cfs_percpt_atomic_alloc(struct cfs_cpt_table *cptab, int val);
 399/* destroy percpt refcount */
 400void cfs_percpt_atomic_free(atomic_t **refs);
 401/* return sum of all percpu refs */
 402int cfs_percpt_atomic_summary(atomic_t **refs);
 403
 404
 405/** Compile-time assertion.
 406
 407 * Check an invariant described by a constant expression at compile time by
 408 * forcing a compiler error if it does not hold.  \a cond must be a constant
 409 * expression as defined by the ISO C Standard:
 410 *
 411 *       6.8.4.2  The switch statement
 412 *       ....
 413 *       [#3] The expression of each case label shall be  an  integer
 414 *       constant   expression  and  no  two  of  the  case  constant
 415 *       expressions in the same switch statement shall have the same
 416 *       value  after  conversion...
 417 *
 418 */
 419#define CLASSERT(cond) do {switch(42) {case (cond): case 0: break;}} while (0)
 420
 421/* support decl needed both by kernel and liblustre */
 422int      libcfs_isknown_lnd(int type);
 423char       *libcfs_lnd2modname(int type);
 424char       *libcfs_lnd2str(int type);
 425int      libcfs_str2lnd(const char *str);
 426char       *libcfs_net2str(__u32 net);
 427char       *libcfs_nid2str(lnet_nid_t nid);
 428__u32       libcfs_str2net(const char *str);
 429lnet_nid_t  libcfs_str2nid(const char *str);
 430int      libcfs_str2anynid(lnet_nid_t *nid, const char *str);
 431char       *libcfs_id2str(lnet_process_id_t id);
 432void    cfs_free_nidlist(struct list_head *list);
 433int      cfs_parse_nidlist(char *str, int len, struct list_head *list);
 434int      cfs_match_nid(lnet_nid_t nid, struct list_head *list);
 435
 436/** \addtogroup lnet_addr
 437 * @{ */
 438/* how an LNET NID encodes net:address */
 439/** extract the address part of an lnet_nid_t */
 440#define LNET_NIDADDR(nid)      ((__u32)((nid) & 0xffffffff))
 441/** extract the network part of an lnet_nid_t */
 442#define LNET_NIDNET(nid)       ((__u32)(((nid) >> 32)) & 0xffffffff)
 443/** make an lnet_nid_t from a network part and an address part */
 444#define LNET_MKNID(net,addr)   ((((__u64)(net))<<32)|((__u64)(addr)))
 445/* how net encodes type:number */
 446#define LNET_NETNUM(net)       ((net) & 0xffff)
 447#define LNET_NETTYP(net)       (((net) >> 16) & 0xffff)
 448#define LNET_MKNET(typ,num)    ((((__u32)(typ))<<16)|((__u32)(num)))
 449/** @} lnet_addr */
 450
 451/* max value for numeric network address */
 452#define MAX_NUMERIC_VALUE 0xffffffff
 453
 454/* implication */
 455#define ergo(a, b) (!(a) || (b))
 456/* logical equivalence */
 457#define equi(a, b) (!!(a) == !!(b))
 458
 459#ifndef CFS_CURRENT_TIME
 460# define CFS_CURRENT_TIME time(0)
 461#endif
 462
 463/* --------------------------------------------------------------------
 464 * Light-weight trace
 465 * Support for temporary event tracing with minimal Heisenberg effect.
 466 * All stuff about lwt are put in arch/kp30.h
 467 * -------------------------------------------------------------------- */
 468
 469struct libcfs_device_userstate
 470{
 471        int        ldu_memhog_pages;
 472        struct page   *ldu_memhog_root_page;
 473};
 474
 475/* what used to be in portals_lib.h */
 476#ifndef MIN
 477# define MIN(a,b) (((a)<(b)) ? (a): (b))
 478#endif
 479#ifndef MAX
 480# define MAX(a,b) (((a)>(b)) ? (a): (b))
 481#endif
 482
 483#define MKSTR(ptr) ((ptr))? (ptr) : ""
 484
 485static inline int cfs_size_round4 (int val)
 486{
 487        return (val + 3) & (~0x3);
 488}
 489
 490#ifndef HAVE_CFS_SIZE_ROUND
 491static inline int cfs_size_round (int val)
 492{
 493        return (val + 7) & (~0x7);
 494}
 495#define HAVE_CFS_SIZE_ROUND
 496#endif
 497
 498static inline int cfs_size_round16(int val)
 499{
 500        return (val + 0xf) & (~0xf);
 501}
 502
 503static inline int cfs_size_round32(int val)
 504{
 505        return (val + 0x1f) & (~0x1f);
 506}
 507
 508static inline int cfs_size_round0(int val)
 509{
 510        if (!val)
 511                return 0;
 512        return (val + 1 + 7) & (~0x7);
 513}
 514
 515static inline size_t cfs_round_strlen(char *fset)
 516{
 517        return (size_t)cfs_size_round((int)strlen(fset) + 1);
 518}
 519
 520/* roundup \a val to power2 */
 521static inline unsigned int cfs_power2_roundup(unsigned int val)
 522{
 523        if (val != LOWEST_BIT_SET(val)) { /* not a power of 2 already */
 524                do {
 525                        val &= ~LOWEST_BIT_SET(val);
 526                } while (val != LOWEST_BIT_SET(val));
 527                /* ...and round up */
 528                val <<= 1;
 529        }
 530        return val;
 531}
 532
 533#define LOGL(var,len,ptr)                                      \
 534do {                                                        \
 535        if (var)                                                \
 536                memcpy((char *)ptr, (const char *)var, len);    \
 537        ptr += cfs_size_round(len);                          \
 538} while (0)
 539
 540#define LOGU(var,len,ptr)                                      \
 541do {                                                        \
 542        if (var)                                                \
 543                memcpy((char *)var, (const char *)ptr, len);    \
 544        ptr += cfs_size_round(len);                          \
 545} while (0)
 546
 547#define LOGL0(var,len,ptr)                            \
 548do {                                                \
 549        if (!len)                                      \
 550                break;                            \
 551        memcpy((char *)ptr, (const char *)var, len);    \
 552        *((char *)(ptr) + len) = 0;                  \
 553        ptr += cfs_size_round(len + 1);          \
 554} while (0)
 555
 556/**
 557 *  Lustre Network Driver types.
 558 */
 559enum {
 560        /* Only add to these values (i.e. don't ever change or redefine them):
 561         * network addresses depend on them... */
 562        QSWLND    = 1,
 563        SOCKLND   = 2,
 564        GMLND     = 3, /* obsolete, keep it so that libcfs_nid2str works */
 565        PTLLND    = 4,
 566        O2IBLND   = 5,
 567        CIBLND    = 6,
 568        OPENIBLND = 7,
 569        IIBLND    = 8,
 570        LOLND     = 9,
 571        RALND     = 10,
 572        VIBLND    = 11,
 573        MXLND     = 12,
 574        GNILND    = 13,
 575};
 576
 577#endif
 578