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#include "core.h"
38#include "config.h"
39#include "dbg.h"
40
41
42
43
44
45
46
47
48
49
50
51static struct print_buf null_buf = { NULL, 0, NULL, 0 };
52struct print_buf *const TIPC_NULL = &null_buf;
53
54static struct print_buf cons_buf = { NULL, 0, NULL, 1 };
55struct print_buf *const TIPC_CONS = &cons_buf;
56
57static struct print_buf log_buf = { NULL, 0, NULL, 1 };
58struct print_buf *const TIPC_LOG = &log_buf;
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76static char print_string[TIPC_PB_MAX_STR];
77static DEFINE_SPINLOCK(print_lock);
78
79
80#define FORMAT(PTR,LEN,FMT) \
81{\
82 va_list args;\
83 va_start(args, FMT);\
84 LEN = vsprintf(PTR, FMT, args);\
85 va_end(args);\
86 *(PTR + LEN) = '\0';\
87}
88
89
90
91
92
93
94
95
96
97
98
99void tipc_printbuf_init(struct print_buf *pb, char *raw, u32 size)
100{
101 pb->buf = raw;
102 pb->crs = raw;
103 pb->size = size;
104 pb->echo = 0;
105
106 if (size < TIPC_PB_MIN_SIZE) {
107 pb->buf = NULL;
108 } else if (raw) {
109 pb->buf[0] = 0;
110 pb->buf[size - 1] = ~0;
111 }
112}
113
114
115
116
117
118
119void tipc_printbuf_reset(struct print_buf *pb)
120{
121 if (pb->buf) {
122 pb->crs = pb->buf;
123 pb->buf[0] = 0;
124 pb->buf[pb->size - 1] = ~0;
125 }
126}
127
128
129
130
131
132
133
134
135int tipc_printbuf_empty(struct print_buf *pb)
136{
137 return (!pb->buf || (pb->crs == pb->buf));
138}
139
140
141
142
143
144
145
146
147
148
149
150int tipc_printbuf_validate(struct print_buf *pb)
151{
152 char *err = "\n\n*** PRINT BUFFER OVERFLOW ***\n\n";
153 char *cp_buf;
154 struct print_buf cb;
155
156 if (!pb->buf)
157 return 0;
158
159 if (pb->buf[pb->size - 1] == 0) {
160 cp_buf = kmalloc(pb->size, GFP_ATOMIC);
161 if (cp_buf) {
162 tipc_printbuf_init(&cb, cp_buf, pb->size);
163 tipc_printbuf_move(&cb, pb);
164 tipc_printbuf_move(pb, &cb);
165 kfree(cp_buf);
166 memcpy(pb->buf, err, strlen(err));
167 } else {
168 tipc_printbuf_reset(pb);
169 tipc_printf(pb, err);
170 }
171 }
172 return (pb->crs - pb->buf + 1);
173}
174
175
176
177
178
179
180
181
182
183
184void tipc_printbuf_move(struct print_buf *pb_to, struct print_buf *pb_from)
185{
186 int len;
187
188
189
190 if (!pb_to->buf)
191 return;
192
193 if (!pb_from->buf) {
194 tipc_printbuf_reset(pb_to);
195 return;
196 }
197
198 if (pb_to->size < pb_from->size) {
199 strcpy(pb_to->buf, "*** PRINT BUFFER MOVE ERROR ***");
200 pb_to->buf[pb_to->size - 1] = ~0;
201 pb_to->crs = strchr(pb_to->buf, 0);
202 return;
203 }
204
205
206
207 len = pb_from->buf + pb_from->size - pb_from->crs - 2;
208 if ((pb_from->buf[pb_from->size - 1] == 0) && (len > 0)) {
209 strcpy(pb_to->buf, pb_from->crs + 1);
210 pb_to->crs = pb_to->buf + len;
211 } else
212 pb_to->crs = pb_to->buf;
213
214
215
216 len = pb_from->crs - pb_from->buf;
217 strcpy(pb_to->crs, pb_from->buf);
218 pb_to->crs += len;
219
220 tipc_printbuf_reset(pb_from);
221}
222
223
224
225
226
227
228
229void tipc_printf(struct print_buf *pb, const char *fmt, ...)
230{
231 int chars_to_add;
232 int chars_left;
233 char save_char;
234
235 spin_lock_bh(&print_lock);
236
237 FORMAT(print_string, chars_to_add, fmt);
238 if (chars_to_add >= TIPC_PB_MAX_STR)
239 strcpy(print_string, "*** PRINT BUFFER STRING TOO LONG ***");
240
241 if (pb->buf) {
242 chars_left = pb->buf + pb->size - pb->crs - 1;
243 if (chars_to_add <= chars_left) {
244 strcpy(pb->crs, print_string);
245 pb->crs += chars_to_add;
246 } else if (chars_to_add >= (pb->size - 1)) {
247 strcpy(pb->buf, print_string + chars_to_add + 1
248 - pb->size);
249 pb->crs = pb->buf + pb->size - 1;
250 } else {
251 strcpy(pb->buf, print_string + chars_left);
252 save_char = print_string[chars_left];
253 print_string[chars_left] = 0;
254 strcpy(pb->crs, print_string);
255 print_string[chars_left] = save_char;
256 pb->crs = pb->buf + chars_to_add - chars_left;
257 }
258 }
259
260 if (pb->echo)
261 printk(print_string);
262
263 spin_unlock_bh(&print_lock);
264}
265
266#ifdef CONFIG_TIPC_DEBUG
267
268
269
270
271
272static void print_to_console(char *crs, int len)
273{
274 int rest = len;
275
276 while (rest > 0) {
277 int sz = rest < TIPC_PB_MAX_STR ? rest : TIPC_PB_MAX_STR;
278 char c = crs[sz];
279
280 crs[sz] = 0;
281 printk((const char *)crs);
282 crs[sz] = c;
283 rest -= sz;
284 crs += sz;
285 }
286}
287
288
289
290
291
292static void printbuf_dump(struct print_buf *pb)
293{
294 int len;
295
296 if (!pb->buf) {
297 printk("*** PRINT BUFFER NOT ALLOCATED ***");
298 return;
299 }
300
301
302
303 len = pb->buf + pb->size - pb->crs - 2;
304 if ((pb->buf[pb->size - 1] == 0) && (len > 0))
305 print_to_console(pb->crs + 1, len);
306
307
308
309 len = pb->crs - pb->buf;
310 print_to_console(pb->buf, len);
311}
312
313
314
315
316
317
318void tipc_dump_dbg(struct print_buf *pb, const char *fmt, ...)
319{
320 int len;
321
322 if (pb == TIPC_CONS)
323 return;
324
325 spin_lock_bh(&print_lock);
326
327 FORMAT(print_string, len, fmt);
328 printk(print_string);
329
330 printk("\n---- Start of %s log dump ----\n\n",
331 (pb == TIPC_LOG) ? "global" : "local");
332 printbuf_dump(pb);
333 tipc_printbuf_reset(pb);
334 printk("\n---- End of dump ----\n");
335
336 spin_unlock_bh(&print_lock);
337}
338
339#endif
340
341
342
343
344
345
346int tipc_log_resize(int log_size)
347{
348 int res = 0;
349
350 spin_lock_bh(&print_lock);
351 if (TIPC_LOG->buf) {
352 kfree(TIPC_LOG->buf);
353 TIPC_LOG->buf = NULL;
354 }
355 if (log_size) {
356 if (log_size < TIPC_PB_MIN_SIZE)
357 log_size = TIPC_PB_MIN_SIZE;
358 res = TIPC_LOG->echo;
359 tipc_printbuf_init(TIPC_LOG, kmalloc(log_size, GFP_ATOMIC),
360 log_size);
361 TIPC_LOG->echo = res;
362 res = !TIPC_LOG->buf;
363 }
364 spin_unlock_bh(&print_lock);
365
366 return res;
367}
368
369
370
371
372
373struct sk_buff *tipc_log_resize_cmd(const void *req_tlv_area, int req_tlv_space)
374{
375 u32 value;
376
377 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_UNSIGNED))
378 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
379
380 value = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
381 if (value != delimit(value, 0, 32768))
382 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
383 " (log size must be 0-32768)");
384 if (tipc_log_resize(value))
385 return tipc_cfg_reply_error_string(
386 "unable to create specified log (log size is now 0)");
387 return tipc_cfg_reply_none();
388}
389
390
391
392
393
394struct sk_buff *tipc_log_dump(void)
395{
396 struct sk_buff *reply;
397
398 spin_lock_bh(&print_lock);
399 if (!TIPC_LOG->buf) {
400 spin_unlock_bh(&print_lock);
401 reply = tipc_cfg_reply_ultra_string("log not activated\n");
402 } else if (tipc_printbuf_empty(TIPC_LOG)) {
403 spin_unlock_bh(&print_lock);
404 reply = tipc_cfg_reply_ultra_string("log is empty\n");
405 }
406 else {
407 struct tlv_desc *rep_tlv;
408 struct print_buf pb;
409 int str_len;
410
411 str_len = min(TIPC_LOG->size, 32768u);
412 spin_unlock_bh(&print_lock);
413 reply = tipc_cfg_reply_alloc(TLV_SPACE(str_len));
414 if (reply) {
415 rep_tlv = (struct tlv_desc *)reply->data;
416 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), str_len);
417 spin_lock_bh(&print_lock);
418 tipc_printbuf_move(&pb, TIPC_LOG);
419 spin_unlock_bh(&print_lock);
420 str_len = strlen(TLV_DATA(rep_tlv)) + 1;
421 skb_put(reply, TLV_SPACE(str_len));
422 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
423 }
424 }
425 return reply;
426}
427
428