940200ee632e09395414bab07cd37bc158754a6b
[linux-2.6-microblaze.git] / drivers / staging / lustre / include / linux / libcfs / libcfs_private.h
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * GPL HEADER START
4  *
5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 only,
9  * as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License version 2 for more details (a copy is included
15  * in the LICENSE file that accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License
18  * version 2 along with this program; If not, see
19  * http://www.gnu.org/licenses/gpl-2.0.html
20  *
21  * GPL HEADER END
22  */
23 /*
24  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
25  * Use is subject to license terms.
26  *
27  * Copyright (c) 2011, 2012, Intel Corporation.
28  */
29 /*
30  * This file is part of Lustre, http://www.lustre.org/
31  * Lustre is a trademark of Sun Microsystems, Inc.
32  *
33  * libcfs/include/libcfs/libcfs_private.h
34  *
35  * Various defines for libcfs.
36  *
37  */
38
39 #ifndef __LIBCFS_PRIVATE_H__
40 #define __LIBCFS_PRIVATE_H__
41
42 #ifndef DEBUG_SUBSYSTEM
43 # define DEBUG_SUBSYSTEM S_UNDEFINED
44 #endif
45
46 #define LASSERTF(cond, fmt, ...)                                        \
47 do {                                                                    \
48         if (unlikely(!(cond))) {                                        \
49                 LIBCFS_DEBUG_MSG_DATA_DECL(__msg_data, D_EMERG, NULL);  \
50                 libcfs_debug_msg(&__msg_data,                           \
51                                  "ASSERTION( %s ) failed: " fmt, #cond, \
52                                  ## __VA_ARGS__);                       \
53                 lbug_with_loc(&__msg_data);                             \
54         }                                                               \
55 } while (0)
56
57 #define LASSERT(cond) LASSERTF(cond, "\n")
58
59 #ifdef CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK
60 /**
61  * This is for more expensive checks that one doesn't want to be enabled all
62  * the time. LINVRNT() has to be explicitly enabled by
63  * CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK option.
64  */
65 # define LINVRNT(exp) LASSERT(exp)
66 #else
67 # define LINVRNT(exp) ((void)sizeof !!(exp))
68 #endif
69
70 void __noreturn lbug_with_loc(struct libcfs_debug_msg_data *msg);
71
72 #define LBUG()                                                    \
73 do {                                                                \
74         LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_EMERG, NULL);          \
75         lbug_with_loc(&msgdata);                                        \
76 } while (0)
77
78 #define LIBCFS_ALLOC_POST(ptr, size)                                        \
79 do {                                                                        \
80         if (unlikely(!(ptr))) {                                             \
81                 CERROR("LNET: out of memory at %s:%d (tried to alloc '"     \
82                        #ptr "' = %d)\n", __FILE__, __LINE__, (int)(size));  \
83         } else {                                                            \
84                 memset((ptr), 0, (size));                                   \
85         }                                                                   \
86 } while (0)
87
88 /**
89  * default allocator
90  */
91 #define LIBCFS_ALLOC(ptr, size)                                             \
92 do {                                                                        \
93         LASSERT(!in_interrupt());                                           \
94         (ptr) = kvmalloc((size), GFP_NOFS);                                 \
95         LIBCFS_ALLOC_POST((ptr), (size));                                   \
96 } while (0)
97
98 /**
99  * non-sleeping allocator
100  */
101 #define LIBCFS_ALLOC_ATOMIC(ptr, size)                                  \
102 do {                                                                    \
103         (ptr) = kmalloc((size), GFP_ATOMIC);                            \
104         LIBCFS_ALLOC_POST(ptr, size);                                   \
105 } while (0)
106
107 /**
108  * allocate memory for specified CPU partition
109  *   \a cptab != NULL, \a cpt is CPU partition id of \a cptab
110  *   \a cptab == NULL, \a cpt is HW NUMA node id
111  */
112 #define LIBCFS_CPT_ALLOC(ptr, cptab, cpt, size)                             \
113 do {                                                                        \
114         LASSERT(!in_interrupt());                                           \
115         (ptr) = kvmalloc_node((size), GFP_NOFS, cfs_cpt_spread_node(cptab, cpt)); \
116         LIBCFS_ALLOC_POST((ptr), (size));                                   \
117 } while (0)
118
119 #define LIBCFS_FREE(ptr, size)                                    \
120 do {                                                                \
121         if (unlikely(!(ptr))) {                                         \
122                 CERROR("LIBCFS: free NULL '" #ptr "' (%d bytes) at "    \
123                        "%s:%d\n", (int)(size), __FILE__, __LINE__);     \
124                 break;                                            \
125         }                                                              \
126         kvfree(ptr);                                      \
127 } while (0)
128
129 /******************************************************************************/
130
131 void libcfs_debug_dumplog(void);
132 int libcfs_debug_init(unsigned long bufsize);
133 int libcfs_debug_cleanup(void);
134 int libcfs_debug_clear_buffer(void);
135 int libcfs_debug_mark_buffer(const char *text);
136
137 /*
138  * allocate a variable array, returned value is an array of pointers.
139  * Caller can specify length of array by count.
140  */
141 void *cfs_array_alloc(int count, unsigned int size);
142 void  cfs_array_free(void *vars);
143
144 #define LASSERT_ATOMIC_ENABLED    (1)
145
146 #if LASSERT_ATOMIC_ENABLED
147
148 /** assert value of @a is equal to @v */
149 #define LASSERT_ATOMIC_EQ(a, v)                 \
150         LASSERTF(atomic_read(a) == v, "value: %d\n", atomic_read((a)))
151
152 /** assert value of @a is unequal to @v */
153 #define LASSERT_ATOMIC_NE(a, v)         \
154         LASSERTF(atomic_read(a) != v, "value: %d\n", atomic_read((a)))
155
156 /** assert value of @a is little than @v */
157 #define LASSERT_ATOMIC_LT(a, v)         \
158         LASSERTF(atomic_read(a) < v, "value: %d\n", atomic_read((a)))
159
160 /** assert value of @a is little/equal to @v */
161 #define LASSERT_ATOMIC_LE(a, v)         \
162         LASSERTF(atomic_read(a) <= v, "value: %d\n", atomic_read((a)))
163
164 /** assert value of @a is great than @v */
165 #define LASSERT_ATOMIC_GT(a, v)         \
166         LASSERTF(atomic_read(a) > v, "value: %d\n", atomic_read((a)))
167
168 /** assert value of @a is great/equal to @v */
169 #define LASSERT_ATOMIC_GE(a, v)         \
170         LASSERTF(atomic_read(a) >= v, "value: %d\n", atomic_read((a)))
171
172 /** assert value of @a is great than @v1 and little than @v2 */
173 #define LASSERT_ATOMIC_GT_LT(a, v1, v2)                  \
174 do {                                                        \
175         int __v = atomic_read(a);                          \
176         LASSERTF(__v > v1 && __v < v2, "value: %d\n", __v);     \
177 } while (0)
178
179 /** assert value of @a is great than @v1 and little/equal to @v2 */
180 #define LASSERT_ATOMIC_GT_LE(a, v1, v2)                  \
181 do {                                                        \
182         int __v = atomic_read(a);                          \
183         LASSERTF(__v > v1 && __v <= v2, "value: %d\n", __v);    \
184 } while (0)
185
186 /** assert value of @a is great/equal to @v1 and little than @v2 */
187 #define LASSERT_ATOMIC_GE_LT(a, v1, v2)                  \
188 do {                                                        \
189         int __v = atomic_read(a);                          \
190         LASSERTF(__v >= v1 && __v < v2, "value: %d\n", __v);    \
191 } while (0)
192
193 /** assert value of @a is great/equal to @v1 and little/equal to @v2 */
194 #define LASSERT_ATOMIC_GE_LE(a, v1, v2)                  \
195 do {                                                        \
196         int __v = atomic_read(a);                          \
197         LASSERTF(__v >= v1 && __v <= v2, "value: %d\n", __v);   \
198 } while (0)
199
200 #else /* !LASSERT_ATOMIC_ENABLED */
201
202 #define LASSERT_ATOMIC_EQ(a, v)          do {} while (0)
203 #define LASSERT_ATOMIC_NE(a, v)          do {} while (0)
204 #define LASSERT_ATOMIC_LT(a, v)          do {} while (0)
205 #define LASSERT_ATOMIC_LE(a, v)          do {} while (0)
206 #define LASSERT_ATOMIC_GT(a, v)          do {} while (0)
207 #define LASSERT_ATOMIC_GE(a, v)          do {} while (0)
208 #define LASSERT_ATOMIC_GT_LT(a, v1, v2)  do {} while (0)
209 #define LASSERT_ATOMIC_GT_LE(a, v1, v2)  do {} while (0)
210 #define LASSERT_ATOMIC_GE_LT(a, v1, v2)  do {} while (0)
211 #define LASSERT_ATOMIC_GE_LE(a, v1, v2)  do {} while (0)
212
213 #endif /* LASSERT_ATOMIC_ENABLED */
214
215 #define LASSERT_ATOMIC_ZERO(a)            LASSERT_ATOMIC_EQ(a, 0)
216 #define LASSERT_ATOMIC_POS(a)              LASSERT_ATOMIC_GT(a, 0)
217
218 #define CFS_ALLOC_PTR(ptr)      LIBCFS_ALLOC(ptr, sizeof(*(ptr)))
219 #define CFS_FREE_PTR(ptr)       LIBCFS_FREE(ptr, sizeof(*(ptr)))
220
221 /* implication */
222 #define ergo(a, b) (!(a) || (b))
223 /* logical equivalence */
224 #define equi(a, b) (!!(a) == !!(b))
225
226 #ifndef HAVE_CFS_SIZE_ROUND
227 static inline size_t cfs_size_round(int val)
228 {
229         return round_up(val, 8);
230 }
231
232 #define HAVE_CFS_SIZE_ROUND
233 #endif
234
235 #endif