Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1 | /* |
Allan Stephens | f5e7526 | 2010-12-31 18:59:24 +0000 | [diff] [blame] | 2 | * net/tipc/log.c: TIPC print buffer routines for debugging |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 3 | * |
Per Liden | 593a5f2 | 2006-01-11 19:14:19 +0100 | [diff] [blame] | 4 | * Copyright (c) 1996-2006, Ericsson AB |
Allan Stephens | fb98ec7 | 2008-05-05 01:20:42 -0700 | [diff] [blame] | 5 | * Copyright (c) 2005-2007, Wind River Systems |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 6 | * All rights reserved. |
| 7 | * |
Per Liden | 9ea1fd3 | 2006-01-11 13:30:43 +0100 | [diff] [blame] | 8 | * Redistribution and use in source and binary forms, with or without |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 9 | * modification, are permitted provided that the following conditions are met: |
| 10 | * |
Per Liden | 9ea1fd3 | 2006-01-11 13:30:43 +0100 | [diff] [blame] | 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in the |
| 15 | * documentation and/or other materials provided with the distribution. |
| 16 | * 3. Neither the names of the copyright holders nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 19 | * |
Per Liden | 9ea1fd3 | 2006-01-11 13:30:43 +0100 | [diff] [blame] | 20 | * Alternatively, this software may be distributed under the terms of the |
| 21 | * GNU General Public License ("GPL") version 2 as published by the Free |
| 22 | * Software Foundation. |
| 23 | * |
| 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 28 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 29 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 30 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 32 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 34 | * POSSIBILITY OF SUCH DAMAGE. |
| 35 | */ |
| 36 | |
| 37 | #include "core.h" |
| 38 | #include "config.h" |
Allan Stephens | f5e7526 | 2010-12-31 18:59:24 +0000 | [diff] [blame] | 39 | #include "log.h" |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 40 | |
Allan Stephens | c890398 | 2008-05-05 01:22:30 -0700 | [diff] [blame] | 41 | /* |
| 42 | * TIPC pre-defines the following print buffers: |
| 43 | * |
| 44 | * TIPC_NULL : null buffer (i.e. print nowhere) |
| 45 | * TIPC_CONS : system console |
| 46 | * TIPC_LOG : TIPC log buffer |
| 47 | * |
| 48 | * Additional user-defined print buffers are also permitted. |
| 49 | */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 50 | |
Allan Stephens | c890398 | 2008-05-05 01:22:30 -0700 | [diff] [blame] | 51 | static struct print_buf null_buf = { NULL, 0, NULL, 0 }; |
Allan Stephens | 7d3aa71 | 2008-05-05 01:22:59 -0700 | [diff] [blame] | 52 | struct print_buf *const TIPC_NULL = &null_buf; |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 53 | |
Allan Stephens | c890398 | 2008-05-05 01:22:30 -0700 | [diff] [blame] | 54 | static struct print_buf cons_buf = { NULL, 0, NULL, 1 }; |
Allan Stephens | 6e7e309 | 2010-12-31 18:59:28 +0000 | [diff] [blame] | 55 | struct print_buf *const TIPC_CONS = &cons_buf; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 56 | |
Allan Stephens | c890398 | 2008-05-05 01:22:30 -0700 | [diff] [blame] | 57 | static struct print_buf log_buf = { NULL, 0, NULL, 1 }; |
Allan Stephens | 7d3aa71 | 2008-05-05 01:22:59 -0700 | [diff] [blame] | 58 | struct print_buf *const TIPC_LOG = &log_buf; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 59 | |
Allan Stephens | c890398 | 2008-05-05 01:22:30 -0700 | [diff] [blame] | 60 | /* |
| 61 | * Locking policy when using print buffers. |
| 62 | * |
| 63 | * 1) tipc_printf() uses 'print_lock' to protect against concurrent access to |
| 64 | * 'print_string' when writing to a print buffer. This also protects against |
| 65 | * concurrent writes to the print buffer being written to. |
| 66 | * |
Allan Stephens | 7ced689 | 2010-12-31 18:59:26 +0000 | [diff] [blame] | 67 | * 2) tipc_log_XXX() leverages the aforementioned use of 'print_lock' to |
| 68 | * protect against all types of concurrent operations on their associated |
| 69 | * print buffer (not just write operations). |
Allan Stephens | c890398 | 2008-05-05 01:22:30 -0700 | [diff] [blame] | 70 | * |
| 71 | * Note: All routines of the form tipc_printbuf_XXX() are lock-free, and rely |
| 72 | * on the caller to prevent simultaneous use of the print buffer(s) being |
| 73 | * manipulated. |
| 74 | */ |
| 75 | |
| 76 | static char print_string[TIPC_PB_MAX_STR]; |
| 77 | static DEFINE_SPINLOCK(print_lock); |
| 78 | |
stephen hemminger | 31e3c3f | 2010-10-13 13:20:35 +0000 | [diff] [blame] | 79 | static void tipc_printbuf_move(struct print_buf *pb_to, |
| 80 | struct print_buf *pb_from); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 81 | |
Allan Stephens | f5e7526 | 2010-12-31 18:59:24 +0000 | [diff] [blame] | 82 | #define FORMAT(PTR, LEN, FMT) \ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 83 | {\ |
Allan Stephens | f5e7526 | 2010-12-31 18:59:24 +0000 | [diff] [blame] | 84 | va_list args;\ |
| 85 | va_start(args, FMT);\ |
| 86 | LEN = vsprintf(PTR, FMT, args);\ |
| 87 | va_end(args);\ |
| 88 | *(PTR + LEN) = '\0';\ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 89 | } |
| 90 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 91 | /** |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 92 | * tipc_printbuf_init - initialize print buffer to empty |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 93 | * @pb: pointer to print buffer structure |
| 94 | * @raw: pointer to character array used by print buffer |
| 95 | * @size: size of character array |
| 96 | * |
Allan Stephens | c890398 | 2008-05-05 01:22:30 -0700 | [diff] [blame] | 97 | * Note: If the character array is too small (or absent), the print buffer |
| 98 | * becomes a null device that discards anything written to it. |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 99 | */ |
| 100 | |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 101 | void tipc_printbuf_init(struct print_buf *pb, char *raw, u32 size) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 102 | { |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 103 | pb->buf = raw; |
| 104 | pb->crs = raw; |
| 105 | pb->size = size; |
Allan Stephens | c890398 | 2008-05-05 01:22:30 -0700 | [diff] [blame] | 106 | pb->echo = 0; |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 107 | |
| 108 | if (size < TIPC_PB_MIN_SIZE) { |
| 109 | pb->buf = NULL; |
| 110 | } else if (raw) { |
| 111 | pb->buf[0] = 0; |
Allan Stephens | 7d3aa71 | 2008-05-05 01:22:59 -0700 | [diff] [blame] | 112 | pb->buf[size - 1] = ~0; |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 113 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /** |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 117 | * tipc_printbuf_reset - reinitialize print buffer to empty state |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 118 | * @pb: pointer to print buffer structure |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 119 | */ |
| 120 | |
stephen hemminger | 31e3c3f | 2010-10-13 13:20:35 +0000 | [diff] [blame] | 121 | static void tipc_printbuf_reset(struct print_buf *pb) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 122 | { |
Allan Stephens | 7d3aa71 | 2008-05-05 01:22:59 -0700 | [diff] [blame] | 123 | if (pb->buf) { |
Allan Stephens | c890398 | 2008-05-05 01:22:30 -0700 | [diff] [blame] | 124 | pb->crs = pb->buf; |
| 125 | pb->buf[0] = 0; |
| 126 | pb->buf[pb->size - 1] = ~0; |
| 127 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /** |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 131 | * tipc_printbuf_empty - test if print buffer is in empty state |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 132 | * @pb: pointer to print buffer structure |
| 133 | * |
| 134 | * Returns non-zero if print buffer is empty. |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 135 | */ |
| 136 | |
stephen hemminger | 31e3c3f | 2010-10-13 13:20:35 +0000 | [diff] [blame] | 137 | static int tipc_printbuf_empty(struct print_buf *pb) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 138 | { |
Eric Dumazet | a02cec2 | 2010-09-22 20:43:57 +0000 | [diff] [blame] | 139 | return !pb->buf || (pb->crs == pb->buf); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /** |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 143 | * tipc_printbuf_validate - check for print buffer overflow |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 144 | * @pb: pointer to print buffer structure |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 145 | * |
| 146 | * Verifies that a print buffer has captured all data written to it. |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 147 | * If data has been lost, linearize buffer and prepend an error message |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 148 | * |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 149 | * Returns length of print buffer data string (including trailing NUL) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 150 | */ |
| 151 | |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 152 | int tipc_printbuf_validate(struct print_buf *pb) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 153 | { |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 154 | char *err = "\n\n*** PRINT BUFFER OVERFLOW ***\n\n"; |
| 155 | char *cp_buf; |
| 156 | struct print_buf cb; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 157 | |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 158 | if (!pb->buf) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 159 | return 0; |
| 160 | |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 161 | if (pb->buf[pb->size - 1] == 0) { |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 162 | cp_buf = kmalloc(pb->size, GFP_ATOMIC); |
Allan Stephens | 7d3aa71 | 2008-05-05 01:22:59 -0700 | [diff] [blame] | 163 | if (cp_buf) { |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 164 | tipc_printbuf_init(&cb, cp_buf, pb->size); |
| 165 | tipc_printbuf_move(&cb, pb); |
| 166 | tipc_printbuf_move(pb, &cb); |
| 167 | kfree(cp_buf); |
| 168 | memcpy(pb->buf, err, strlen(err)); |
| 169 | } else { |
| 170 | tipc_printbuf_reset(pb); |
| 171 | tipc_printf(pb, err); |
| 172 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 173 | } |
Eric Dumazet | a02cec2 | 2010-09-22 20:43:57 +0000 | [diff] [blame] | 174 | return pb->crs - pb->buf + 1; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /** |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 178 | * tipc_printbuf_move - move print buffer contents to another print buffer |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 179 | * @pb_to: pointer to destination print buffer structure |
| 180 | * @pb_from: pointer to source print buffer structure |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 181 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 182 | * Current contents of destination print buffer (if any) are discarded. |
| 183 | * Source print buffer becomes empty if a successful move occurs. |
| 184 | */ |
| 185 | |
stephen hemminger | 31e3c3f | 2010-10-13 13:20:35 +0000 | [diff] [blame] | 186 | static void tipc_printbuf_move(struct print_buf *pb_to, |
| 187 | struct print_buf *pb_from) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 188 | { |
| 189 | int len; |
| 190 | |
| 191 | /* Handle the cases where contents can't be moved */ |
| 192 | |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 193 | if (!pb_to->buf) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 194 | return; |
| 195 | |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 196 | if (!pb_from->buf) { |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 197 | tipc_printbuf_reset(pb_to); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 198 | return; |
| 199 | } |
| 200 | |
| 201 | if (pb_to->size < pb_from->size) { |
Allan Stephens | 93758c3 | 2008-05-05 01:21:12 -0700 | [diff] [blame] | 202 | strcpy(pb_to->buf, "*** PRINT BUFFER MOVE ERROR ***"); |
| 203 | pb_to->buf[pb_to->size - 1] = ~0; |
| 204 | pb_to->crs = strchr(pb_to->buf, 0); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 205 | return; |
| 206 | } |
| 207 | |
| 208 | /* Copy data from char after cursor to end (if used) */ |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 209 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 210 | len = pb_from->buf + pb_from->size - pb_from->crs - 2; |
Allan Stephens | 7d3aa71 | 2008-05-05 01:22:59 -0700 | [diff] [blame] | 211 | if ((pb_from->buf[pb_from->size - 1] == 0) && (len > 0)) { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 212 | strcpy(pb_to->buf, pb_from->crs + 1); |
| 213 | pb_to->crs = pb_to->buf + len; |
| 214 | } else |
| 215 | pb_to->crs = pb_to->buf; |
| 216 | |
| 217 | /* Copy data from start to cursor (always) */ |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 218 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 219 | len = pb_from->crs - pb_from->buf; |
| 220 | strcpy(pb_to->crs, pb_from->buf); |
| 221 | pb_to->crs += len; |
| 222 | |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 223 | tipc_printbuf_reset(pb_from); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /** |
Allan Stephens | c890398 | 2008-05-05 01:22:30 -0700 | [diff] [blame] | 227 | * tipc_printf - append formatted output to print buffer |
| 228 | * @pb: pointer to print buffer |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 229 | * @fmt: formatted info to be printed |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 230 | */ |
| 231 | |
| 232 | void tipc_printf(struct print_buf *pb, const char *fmt, ...) |
| 233 | { |
| 234 | int chars_to_add; |
| 235 | int chars_left; |
| 236 | char save_char; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 237 | |
| 238 | spin_lock_bh(&print_lock); |
Allan Stephens | c890398 | 2008-05-05 01:22:30 -0700 | [diff] [blame] | 239 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 240 | FORMAT(print_string, chars_to_add, fmt); |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 241 | if (chars_to_add >= TIPC_PB_MAX_STR) |
| 242 | strcpy(print_string, "*** PRINT BUFFER STRING TOO LONG ***"); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 243 | |
Allan Stephens | c890398 | 2008-05-05 01:22:30 -0700 | [diff] [blame] | 244 | if (pb->buf) { |
| 245 | chars_left = pb->buf + pb->size - pb->crs - 1; |
| 246 | if (chars_to_add <= chars_left) { |
| 247 | strcpy(pb->crs, print_string); |
| 248 | pb->crs += chars_to_add; |
| 249 | } else if (chars_to_add >= (pb->size - 1)) { |
| 250 | strcpy(pb->buf, print_string + chars_to_add + 1 |
| 251 | - pb->size); |
| 252 | pb->crs = pb->buf + pb->size - 1; |
| 253 | } else { |
| 254 | strcpy(pb->buf, print_string + chars_left); |
| 255 | save_char = print_string[chars_left]; |
| 256 | print_string[chars_left] = 0; |
| 257 | strcpy(pb->crs, print_string); |
| 258 | print_string[chars_left] = save_char; |
| 259 | pb->crs = pb->buf + chars_to_add - chars_left; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 260 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 261 | } |
Allan Stephens | c890398 | 2008-05-05 01:22:30 -0700 | [diff] [blame] | 262 | |
| 263 | if (pb->echo) |
Stephen Hemminger | 4b704d5 | 2009-03-18 19:11:29 -0700 | [diff] [blame] | 264 | printk("%s", print_string); |
Allan Stephens | c890398 | 2008-05-05 01:22:30 -0700 | [diff] [blame] | 265 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 266 | spin_unlock_bh(&print_lock); |
| 267 | } |
| 268 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 269 | /** |
Allan Stephens | 025adbe | 2008-05-05 01:20:04 -0700 | [diff] [blame] | 270 | * tipc_log_resize - change the size of the TIPC log buffer |
| 271 | * @log_size: print buffer size to use |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 272 | */ |
| 273 | |
Allan Stephens | fb98ec7 | 2008-05-05 01:20:42 -0700 | [diff] [blame] | 274 | int tipc_log_resize(int log_size) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 275 | { |
Allan Stephens | fb98ec7 | 2008-05-05 01:20:42 -0700 | [diff] [blame] | 276 | int res = 0; |
| 277 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 278 | spin_lock_bh(&print_lock); |
Allan Stephens | f5e7526 | 2010-12-31 18:59:24 +0000 | [diff] [blame] | 279 | kfree(TIPC_LOG->buf); |
| 280 | TIPC_LOG->buf = NULL; |
Allan Stephens | 025adbe | 2008-05-05 01:20:04 -0700 | [diff] [blame] | 281 | if (log_size) { |
| 282 | if (log_size < TIPC_PB_MIN_SIZE) |
| 283 | log_size = TIPC_PB_MIN_SIZE; |
Allan Stephens | c890398 | 2008-05-05 01:22:30 -0700 | [diff] [blame] | 284 | res = TIPC_LOG->echo; |
Allan Stephens | 025adbe | 2008-05-05 01:20:04 -0700 | [diff] [blame] | 285 | tipc_printbuf_init(TIPC_LOG, kmalloc(log_size, GFP_ATOMIC), |
| 286 | log_size); |
Allan Stephens | c890398 | 2008-05-05 01:22:30 -0700 | [diff] [blame] | 287 | TIPC_LOG->echo = res; |
Allan Stephens | fb98ec7 | 2008-05-05 01:20:42 -0700 | [diff] [blame] | 288 | res = !TIPC_LOG->buf; |
Allan Stephens | 025adbe | 2008-05-05 01:20:04 -0700 | [diff] [blame] | 289 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 290 | spin_unlock_bh(&print_lock); |
Allan Stephens | fb98ec7 | 2008-05-05 01:20:42 -0700 | [diff] [blame] | 291 | |
| 292 | return res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | /** |
Allan Stephens | 025adbe | 2008-05-05 01:20:04 -0700 | [diff] [blame] | 296 | * tipc_log_resize_cmd - reconfigure size of TIPC log buffer |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 297 | */ |
| 298 | |
Allan Stephens | 025adbe | 2008-05-05 01:20:04 -0700 | [diff] [blame] | 299 | struct sk_buff *tipc_log_resize_cmd(const void *req_tlv_area, int req_tlv_space) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 300 | { |
| 301 | u32 value; |
| 302 | |
| 303 | if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_UNSIGNED)) |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 304 | return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 305 | |
Al Viro | 3e6c8cd | 2006-11-08 00:19:09 -0800 | [diff] [blame] | 306 | value = ntohl(*(__be32 *)TLV_DATA(req_tlv_area)); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 307 | if (value != delimit(value, 0, 32768)) |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 308 | return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE |
| 309 | " (log size must be 0-32768)"); |
Allan Stephens | fb98ec7 | 2008-05-05 01:20:42 -0700 | [diff] [blame] | 310 | if (tipc_log_resize(value)) |
| 311 | return tipc_cfg_reply_error_string( |
| 312 | "unable to create specified log (log size is now 0)"); |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 313 | return tipc_cfg_reply_none(); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | /** |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 317 | * tipc_log_dump - capture TIPC log buffer contents in configuration message |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 318 | */ |
| 319 | |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 320 | struct sk_buff *tipc_log_dump(void) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 321 | { |
| 322 | struct sk_buff *reply; |
| 323 | |
| 324 | spin_lock_bh(&print_lock); |
Allan Stephens | 93758c3 | 2008-05-05 01:21:12 -0700 | [diff] [blame] | 325 | if (!TIPC_LOG->buf) { |
| 326 | spin_unlock_bh(&print_lock); |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 327 | reply = tipc_cfg_reply_ultra_string("log not activated\n"); |
Allan Stephens | 93758c3 | 2008-05-05 01:21:12 -0700 | [diff] [blame] | 328 | } else if (tipc_printbuf_empty(TIPC_LOG)) { |
| 329 | spin_unlock_bh(&print_lock); |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 330 | reply = tipc_cfg_reply_ultra_string("log is empty\n"); |
Allan Stephens | f5e7526 | 2010-12-31 18:59:24 +0000 | [diff] [blame] | 331 | } else { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 332 | struct tlv_desc *rep_tlv; |
| 333 | struct print_buf pb; |
| 334 | int str_len; |
| 335 | |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 336 | str_len = min(TIPC_LOG->size, 32768u); |
Allan Stephens | 93758c3 | 2008-05-05 01:21:12 -0700 | [diff] [blame] | 337 | spin_unlock_bh(&print_lock); |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 338 | reply = tipc_cfg_reply_alloc(TLV_SPACE(str_len)); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 339 | if (reply) { |
| 340 | rep_tlv = (struct tlv_desc *)reply->data; |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 341 | tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), str_len); |
Allan Stephens | 93758c3 | 2008-05-05 01:21:12 -0700 | [diff] [blame] | 342 | spin_lock_bh(&print_lock); |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 343 | tipc_printbuf_move(&pb, TIPC_LOG); |
Allan Stephens | 93758c3 | 2008-05-05 01:21:12 -0700 | [diff] [blame] | 344 | spin_unlock_bh(&print_lock); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 345 | str_len = strlen(TLV_DATA(rep_tlv)) + 1; |
| 346 | skb_put(reply, TLV_SPACE(str_len)); |
| 347 | TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len); |
| 348 | } |
| 349 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 350 | return reply; |
| 351 | } |