Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1 | /* |
| 2 | * drivers/misc/logger.c |
| 3 | * |
| 4 | * A Logging Subsystem |
| 5 | * |
| 6 | * Copyright (C) 2007-2008 Google, Inc. |
| 7 | * |
| 8 | * Robert Love <rlove@google.com> |
| 9 | * |
| 10 | * This software is licensed under the terms of the GNU General Public |
| 11 | * License version 2, as published by the Free Software Foundation, and |
| 12 | * may be copied, distributed, and modified under those terms. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | */ |
| 19 | |
Corentin Chary | 23687af | 2009-11-28 09:45:14 +0100 | [diff] [blame] | 20 | #include <linux/sched.h> |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 21 | #include <linux/module.h> |
| 22 | #include <linux/fs.h> |
| 23 | #include <linux/miscdevice.h> |
| 24 | #include <linux/uaccess.h> |
| 25 | #include <linux/poll.h> |
Colin Cross | c11a166 | 2010-04-15 15:21:51 -0700 | [diff] [blame] | 26 | #include <linux/slab.h> |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 27 | #include <linux/time.h> |
| 28 | #include "logger.h" |
| 29 | |
| 30 | #include <asm/ioctls.h> |
| 31 | |
| 32 | /* |
| 33 | * struct logger_log - represents a specific log, such as 'main' or 'radio' |
| 34 | * |
| 35 | * This structure lives from module insertion until module removal, so it does |
| 36 | * not need additional reference counting. The structure is protected by the |
| 37 | * mutex 'mutex'. |
| 38 | */ |
| 39 | struct logger_log { |
Marco Navarra | 277cdd0 | 2011-12-15 17:57:48 +0100 | [diff] [blame] | 40 | unsigned char *buffer;/* the ring buffer itself */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 41 | struct miscdevice misc; /* misc device representing the log */ |
| 42 | wait_queue_head_t wq; /* wait queue for readers */ |
| 43 | struct list_head readers; /* this log's readers */ |
| 44 | struct mutex mutex; /* mutex protecting buffer */ |
| 45 | size_t w_off; /* current write head offset */ |
| 46 | size_t head; /* new readers start here */ |
| 47 | size_t size; /* size of the log */ |
| 48 | }; |
| 49 | |
| 50 | /* |
| 51 | * struct logger_reader - a logging device open for reading |
| 52 | * |
| 53 | * This object lives from open to release, so we don't need additional |
| 54 | * reference counting. The structure is protected by log->mutex. |
| 55 | */ |
| 56 | struct logger_reader { |
| 57 | struct logger_log *log; /* associated log */ |
| 58 | struct list_head list; /* entry in logger_log's list */ |
| 59 | size_t r_off; /* current read head offset */ |
| 60 | }; |
| 61 | |
| 62 | /* logger_offset - returns index 'n' into the log via (optimized) modulus */ |
Tim Bird | c626224 | 2012-02-07 18:26:38 -0800 | [diff] [blame] | 63 | size_t logger_offset(struct logger_log *log, size_t n) |
| 64 | { |
| 65 | return n & (log->size-1); |
| 66 | } |
| 67 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 68 | |
| 69 | /* |
| 70 | * file_get_log - Given a file structure, return the associated log |
| 71 | * |
| 72 | * This isn't aesthetic. We have several goals: |
| 73 | * |
Marco Navarra | 277cdd0 | 2011-12-15 17:57:48 +0100 | [diff] [blame] | 74 | * 1) Need to quickly obtain the associated log during an I/O operation |
| 75 | * 2) Readers need to maintain state (logger_reader) |
| 76 | * 3) Writers need to be very fast (open() should be a near no-op) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 77 | * |
| 78 | * In the reader case, we can trivially go file->logger_reader->logger_log. |
| 79 | * For a writer, we don't want to maintain a logger_reader, so we just go |
| 80 | * file->logger_log. Thus what file->private_data points at depends on whether |
| 81 | * or not the file was opened for reading. This function hides that dirtiness. |
| 82 | */ |
| 83 | static inline struct logger_log *file_get_log(struct file *file) |
| 84 | { |
| 85 | if (file->f_mode & FMODE_READ) { |
| 86 | struct logger_reader *reader = file->private_data; |
| 87 | return reader->log; |
| 88 | } else |
| 89 | return file->private_data; |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * get_entry_len - Grabs the length of the payload of the next entry starting |
| 94 | * from 'off'. |
| 95 | * |
Tim Bird | 3bcfa43 | 2012-02-08 10:37:57 -0800 | [diff] [blame] | 96 | * An entry length is 2 bytes (16 bits) in host endian order. |
| 97 | * In the log, the length does not include the size of the log entry structure. |
| 98 | * This function returns the size including the log entry structure. |
| 99 | * |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 100 | * Caller needs to hold log->mutex. |
| 101 | */ |
| 102 | static __u32 get_entry_len(struct logger_log *log, size_t off) |
| 103 | { |
| 104 | __u16 val; |
| 105 | |
Tim Bird | 3bcfa43 | 2012-02-08 10:37:57 -0800 | [diff] [blame] | 106 | /* copy 2 bytes from buffer, in memcpy order, */ |
| 107 | /* handling possible wrap at end of buffer */ |
| 108 | |
| 109 | ((__u8 *)&val)[0] = log->buffer[off]; |
| 110 | if (likely(off+1 < log->size)) |
| 111 | ((__u8 *)&val)[1] = log->buffer[off+1]; |
| 112 | else |
| 113 | ((__u8 *)&val)[1] = log->buffer[0]; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 114 | |
| 115 | return sizeof(struct logger_entry) + val; |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * do_read_log_to_user - reads exactly 'count' bytes from 'log' into the |
| 120 | * user-space buffer 'buf'. Returns 'count' on success. |
| 121 | * |
| 122 | * Caller must hold log->mutex. |
| 123 | */ |
| 124 | static ssize_t do_read_log_to_user(struct logger_log *log, |
| 125 | struct logger_reader *reader, |
| 126 | char __user *buf, |
| 127 | size_t count) |
| 128 | { |
| 129 | size_t len; |
| 130 | |
| 131 | /* |
| 132 | * We read from the log in two disjoint operations. First, we read from |
| 133 | * the current read head offset up to 'count' bytes or to the end of |
| 134 | * the log, whichever comes first. |
| 135 | */ |
| 136 | len = min(count, log->size - reader->r_off); |
| 137 | if (copy_to_user(buf, log->buffer + reader->r_off, len)) |
| 138 | return -EFAULT; |
| 139 | |
| 140 | /* |
| 141 | * Second, we read any remaining bytes, starting back at the head of |
| 142 | * the log. |
| 143 | */ |
| 144 | if (count != len) |
| 145 | if (copy_to_user(buf + len, log->buffer, count - len)) |
| 146 | return -EFAULT; |
| 147 | |
Tim Bird | c626224 | 2012-02-07 18:26:38 -0800 | [diff] [blame] | 148 | reader->r_off = logger_offset(log, reader->r_off + count); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 149 | |
| 150 | return count; |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * logger_read - our log's read() method |
| 155 | * |
| 156 | * Behavior: |
| 157 | * |
Marco Navarra | 277cdd0 | 2011-12-15 17:57:48 +0100 | [diff] [blame] | 158 | * - O_NONBLOCK works |
| 159 | * - If there are no log entries to read, blocks until log is written to |
| 160 | * - Atomically reads exactly one log entry |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 161 | * |
| 162 | * Optimal read size is LOGGER_ENTRY_MAX_LEN. Will set errno to EINVAL if read |
| 163 | * buffer is insufficient to hold next entry. |
| 164 | */ |
| 165 | static ssize_t logger_read(struct file *file, char __user *buf, |
| 166 | size_t count, loff_t *pos) |
| 167 | { |
| 168 | struct logger_reader *reader = file->private_data; |
| 169 | struct logger_log *log = reader->log; |
| 170 | ssize_t ret; |
| 171 | DEFINE_WAIT(wait); |
| 172 | |
| 173 | start: |
| 174 | while (1) { |
Tim Bird | c76c7ca | 2012-02-07 18:30:09 -0800 | [diff] [blame^] | 175 | mutex_lock(&log->mutex); |
| 176 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 177 | prepare_to_wait(&log->wq, &wait, TASK_INTERRUPTIBLE); |
| 178 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 179 | ret = (log->w_off == reader->r_off); |
| 180 | mutex_unlock(&log->mutex); |
| 181 | if (!ret) |
| 182 | break; |
| 183 | |
| 184 | if (file->f_flags & O_NONBLOCK) { |
| 185 | ret = -EAGAIN; |
| 186 | break; |
| 187 | } |
| 188 | |
| 189 | if (signal_pending(current)) { |
| 190 | ret = -EINTR; |
| 191 | break; |
| 192 | } |
| 193 | |
| 194 | schedule(); |
| 195 | } |
| 196 | |
| 197 | finish_wait(&log->wq, &wait); |
| 198 | if (ret) |
| 199 | return ret; |
| 200 | |
| 201 | mutex_lock(&log->mutex); |
| 202 | |
| 203 | /* is there still something to read or did we race? */ |
| 204 | if (unlikely(log->w_off == reader->r_off)) { |
| 205 | mutex_unlock(&log->mutex); |
| 206 | goto start; |
| 207 | } |
| 208 | |
| 209 | /* get the size of the next entry */ |
| 210 | ret = get_entry_len(log, reader->r_off); |
| 211 | if (count < ret) { |
| 212 | ret = -EINVAL; |
| 213 | goto out; |
| 214 | } |
| 215 | |
| 216 | /* get exactly one entry from the log */ |
| 217 | ret = do_read_log_to_user(log, reader, buf, ret); |
| 218 | |
| 219 | out: |
| 220 | mutex_unlock(&log->mutex); |
| 221 | |
| 222 | return ret; |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * get_next_entry - return the offset of the first valid entry at least 'len' |
| 227 | * bytes after 'off'. |
| 228 | * |
| 229 | * Caller must hold log->mutex. |
| 230 | */ |
| 231 | static size_t get_next_entry(struct logger_log *log, size_t off, size_t len) |
| 232 | { |
| 233 | size_t count = 0; |
| 234 | |
| 235 | do { |
| 236 | size_t nr = get_entry_len(log, off); |
Tim Bird | c626224 | 2012-02-07 18:26:38 -0800 | [diff] [blame] | 237 | off = logger_offset(log, off + nr); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 238 | count += nr; |
| 239 | } while (count < len); |
| 240 | |
| 241 | return off; |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * clock_interval - is a < c < b in mod-space? Put another way, does the line |
| 246 | * from a to b cross c? |
| 247 | */ |
| 248 | static inline int clock_interval(size_t a, size_t b, size_t c) |
| 249 | { |
| 250 | if (b < a) { |
| 251 | if (a < c || b >= c) |
| 252 | return 1; |
| 253 | } else { |
| 254 | if (a < c && b >= c) |
| 255 | return 1; |
| 256 | } |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | /* |
| 262 | * fix_up_readers - walk the list of all readers and "fix up" any who were |
| 263 | * lapped by the writer; also do the same for the default "start head". |
| 264 | * We do this by "pulling forward" the readers and start head to the first |
| 265 | * entry after the new write head. |
| 266 | * |
| 267 | * The caller needs to hold log->mutex. |
| 268 | */ |
| 269 | static void fix_up_readers(struct logger_log *log, size_t len) |
| 270 | { |
| 271 | size_t old = log->w_off; |
Tim Bird | c626224 | 2012-02-07 18:26:38 -0800 | [diff] [blame] | 272 | size_t new = logger_offset(log, old + len); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 273 | struct logger_reader *reader; |
| 274 | |
| 275 | if (clock_interval(old, new, log->head)) |
| 276 | log->head = get_next_entry(log, log->head, len); |
| 277 | |
| 278 | list_for_each_entry(reader, &log->readers, list) |
| 279 | if (clock_interval(old, new, reader->r_off)) |
| 280 | reader->r_off = get_next_entry(log, reader->r_off, len); |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | * do_write_log - writes 'len' bytes from 'buf' to 'log' |
| 285 | * |
| 286 | * The caller needs to hold log->mutex. |
| 287 | */ |
| 288 | static void do_write_log(struct logger_log *log, const void *buf, size_t count) |
| 289 | { |
| 290 | size_t len; |
| 291 | |
| 292 | len = min(count, log->size - log->w_off); |
| 293 | memcpy(log->buffer + log->w_off, buf, len); |
| 294 | |
| 295 | if (count != len) |
| 296 | memcpy(log->buffer, buf + len, count - len); |
| 297 | |
Tim Bird | c626224 | 2012-02-07 18:26:38 -0800 | [diff] [blame] | 298 | log->w_off = logger_offset(log, log->w_off + count); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 299 | |
| 300 | } |
| 301 | |
| 302 | /* |
| 303 | * do_write_log_user - writes 'len' bytes from the user-space buffer 'buf' to |
| 304 | * the log 'log' |
| 305 | * |
| 306 | * The caller needs to hold log->mutex. |
| 307 | * |
| 308 | * Returns 'count' on success, negative error code on failure. |
| 309 | */ |
| 310 | static ssize_t do_write_log_from_user(struct logger_log *log, |
| 311 | const void __user *buf, size_t count) |
| 312 | { |
| 313 | size_t len; |
| 314 | |
| 315 | len = min(count, log->size - log->w_off); |
| 316 | if (len && copy_from_user(log->buffer + log->w_off, buf, len)) |
| 317 | return -EFAULT; |
| 318 | |
| 319 | if (count != len) |
| 320 | if (copy_from_user(log->buffer, buf + len, count - len)) |
| 321 | return -EFAULT; |
| 322 | |
Tim Bird | c626224 | 2012-02-07 18:26:38 -0800 | [diff] [blame] | 323 | log->w_off = logger_offset(log, log->w_off + count); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 324 | |
| 325 | return count; |
| 326 | } |
| 327 | |
| 328 | /* |
| 329 | * logger_aio_write - our write method, implementing support for write(), |
| 330 | * writev(), and aio_write(). Writes are our fast path, and we try to optimize |
| 331 | * them above all else. |
| 332 | */ |
| 333 | ssize_t logger_aio_write(struct kiocb *iocb, const struct iovec *iov, |
| 334 | unsigned long nr_segs, loff_t ppos) |
| 335 | { |
| 336 | struct logger_log *log = file_get_log(iocb->ki_filp); |
| 337 | size_t orig = log->w_off; |
| 338 | struct logger_entry header; |
| 339 | struct timespec now; |
| 340 | ssize_t ret = 0; |
| 341 | |
| 342 | now = current_kernel_time(); |
| 343 | |
| 344 | header.pid = current->tgid; |
| 345 | header.tid = current->pid; |
| 346 | header.sec = now.tv_sec; |
| 347 | header.nsec = now.tv_nsec; |
| 348 | header.len = min_t(size_t, iocb->ki_left, LOGGER_ENTRY_MAX_PAYLOAD); |
| 349 | |
| 350 | /* null writes succeed, return zero */ |
| 351 | if (unlikely(!header.len)) |
| 352 | return 0; |
| 353 | |
| 354 | mutex_lock(&log->mutex); |
| 355 | |
| 356 | /* |
| 357 | * Fix up any readers, pulling them forward to the first readable |
| 358 | * entry after (what will be) the new write offset. We do this now |
| 359 | * because if we partially fail, we can end up with clobbered log |
| 360 | * entries that encroach on readable buffer. |
| 361 | */ |
| 362 | fix_up_readers(log, sizeof(struct logger_entry) + header.len); |
| 363 | |
| 364 | do_write_log(log, &header, sizeof(struct logger_entry)); |
| 365 | |
| 366 | while (nr_segs-- > 0) { |
| 367 | size_t len; |
| 368 | ssize_t nr; |
| 369 | |
| 370 | /* figure out how much of this vector we can keep */ |
| 371 | len = min_t(size_t, iov->iov_len, header.len - ret); |
| 372 | |
| 373 | /* write out this segment's payload */ |
| 374 | nr = do_write_log_from_user(log, iov->iov_base, len); |
| 375 | if (unlikely(nr < 0)) { |
| 376 | log->w_off = orig; |
| 377 | mutex_unlock(&log->mutex); |
| 378 | return nr; |
| 379 | } |
| 380 | |
| 381 | iov++; |
| 382 | ret += nr; |
| 383 | } |
| 384 | |
| 385 | mutex_unlock(&log->mutex); |
| 386 | |
| 387 | /* wake up any blocked readers */ |
| 388 | wake_up_interruptible(&log->wq); |
| 389 | |
| 390 | return ret; |
| 391 | } |
| 392 | |
| 393 | static struct logger_log *get_log_from_minor(int); |
| 394 | |
| 395 | /* |
| 396 | * logger_open - the log's open() file operation |
| 397 | * |
| 398 | * Note how near a no-op this is in the write-only case. Keep it that way! |
| 399 | */ |
| 400 | static int logger_open(struct inode *inode, struct file *file) |
| 401 | { |
| 402 | struct logger_log *log; |
| 403 | int ret; |
| 404 | |
| 405 | ret = nonseekable_open(inode, file); |
| 406 | if (ret) |
| 407 | return ret; |
| 408 | |
| 409 | log = get_log_from_minor(MINOR(inode->i_rdev)); |
| 410 | if (!log) |
| 411 | return -ENODEV; |
| 412 | |
| 413 | if (file->f_mode & FMODE_READ) { |
| 414 | struct logger_reader *reader; |
| 415 | |
| 416 | reader = kmalloc(sizeof(struct logger_reader), GFP_KERNEL); |
| 417 | if (!reader) |
| 418 | return -ENOMEM; |
| 419 | |
| 420 | reader->log = log; |
| 421 | INIT_LIST_HEAD(&reader->list); |
| 422 | |
| 423 | mutex_lock(&log->mutex); |
| 424 | reader->r_off = log->head; |
| 425 | list_add_tail(&reader->list, &log->readers); |
| 426 | mutex_unlock(&log->mutex); |
| 427 | |
| 428 | file->private_data = reader; |
| 429 | } else |
| 430 | file->private_data = log; |
| 431 | |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | /* |
| 436 | * logger_release - the log's release file operation |
| 437 | * |
| 438 | * Note this is a total no-op in the write-only case. Keep it that way! |
| 439 | */ |
| 440 | static int logger_release(struct inode *ignored, struct file *file) |
| 441 | { |
| 442 | if (file->f_mode & FMODE_READ) { |
| 443 | struct logger_reader *reader = file->private_data; |
| 444 | list_del(&reader->list); |
| 445 | kfree(reader); |
| 446 | } |
| 447 | |
| 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | /* |
| 452 | * logger_poll - the log's poll file operation, for poll/select/epoll |
| 453 | * |
| 454 | * Note we always return POLLOUT, because you can always write() to the log. |
| 455 | * Note also that, strictly speaking, a return value of POLLIN does not |
| 456 | * guarantee that the log is readable without blocking, as there is a small |
| 457 | * chance that the writer can lap the reader in the interim between poll() |
| 458 | * returning and the read() request. |
| 459 | */ |
| 460 | static unsigned int logger_poll(struct file *file, poll_table *wait) |
| 461 | { |
| 462 | struct logger_reader *reader; |
| 463 | struct logger_log *log; |
| 464 | unsigned int ret = POLLOUT | POLLWRNORM; |
| 465 | |
| 466 | if (!(file->f_mode & FMODE_READ)) |
| 467 | return ret; |
| 468 | |
| 469 | reader = file->private_data; |
| 470 | log = reader->log; |
| 471 | |
| 472 | poll_wait(file, &log->wq, wait); |
| 473 | |
| 474 | mutex_lock(&log->mutex); |
| 475 | if (log->w_off != reader->r_off) |
| 476 | ret |= POLLIN | POLLRDNORM; |
| 477 | mutex_unlock(&log->mutex); |
| 478 | |
| 479 | return ret; |
| 480 | } |
| 481 | |
| 482 | static long logger_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 483 | { |
| 484 | struct logger_log *log = file_get_log(file); |
| 485 | struct logger_reader *reader; |
| 486 | long ret = -ENOTTY; |
| 487 | |
| 488 | mutex_lock(&log->mutex); |
| 489 | |
| 490 | switch (cmd) { |
| 491 | case LOGGER_GET_LOG_BUF_SIZE: |
| 492 | ret = log->size; |
| 493 | break; |
| 494 | case LOGGER_GET_LOG_LEN: |
| 495 | if (!(file->f_mode & FMODE_READ)) { |
| 496 | ret = -EBADF; |
| 497 | break; |
| 498 | } |
| 499 | reader = file->private_data; |
| 500 | if (log->w_off >= reader->r_off) |
| 501 | ret = log->w_off - reader->r_off; |
| 502 | else |
| 503 | ret = (log->size - reader->r_off) + log->w_off; |
| 504 | break; |
| 505 | case LOGGER_GET_NEXT_ENTRY_LEN: |
| 506 | if (!(file->f_mode & FMODE_READ)) { |
| 507 | ret = -EBADF; |
| 508 | break; |
| 509 | } |
| 510 | reader = file->private_data; |
| 511 | if (log->w_off != reader->r_off) |
| 512 | ret = get_entry_len(log, reader->r_off); |
| 513 | else |
| 514 | ret = 0; |
| 515 | break; |
| 516 | case LOGGER_FLUSH_LOG: |
| 517 | if (!(file->f_mode & FMODE_WRITE)) { |
| 518 | ret = -EBADF; |
| 519 | break; |
| 520 | } |
| 521 | list_for_each_entry(reader, &log->readers, list) |
| 522 | reader->r_off = log->w_off; |
| 523 | log->head = log->w_off; |
| 524 | ret = 0; |
| 525 | break; |
| 526 | } |
| 527 | |
| 528 | mutex_unlock(&log->mutex); |
| 529 | |
| 530 | return ret; |
| 531 | } |
| 532 | |
| 533 | static const struct file_operations logger_fops = { |
| 534 | .owner = THIS_MODULE, |
| 535 | .read = logger_read, |
| 536 | .aio_write = logger_aio_write, |
| 537 | .poll = logger_poll, |
| 538 | .unlocked_ioctl = logger_ioctl, |
| 539 | .compat_ioctl = logger_ioctl, |
| 540 | .open = logger_open, |
| 541 | .release = logger_release, |
| 542 | }; |
| 543 | |
| 544 | /* |
| 545 | * Defines a log structure with name 'NAME' and a size of 'SIZE' bytes, which |
| 546 | * must be a power of two, greater than LOGGER_ENTRY_MAX_LEN, and less than |
| 547 | * LONG_MAX minus LOGGER_ENTRY_MAX_LEN. |
| 548 | */ |
| 549 | #define DEFINE_LOGGER_DEVICE(VAR, NAME, SIZE) \ |
| 550 | static unsigned char _buf_ ## VAR[SIZE]; \ |
| 551 | static struct logger_log VAR = { \ |
| 552 | .buffer = _buf_ ## VAR, \ |
| 553 | .misc = { \ |
| 554 | .minor = MISC_DYNAMIC_MINOR, \ |
| 555 | .name = NAME, \ |
| 556 | .fops = &logger_fops, \ |
| 557 | .parent = NULL, \ |
| 558 | }, \ |
| 559 | .wq = __WAIT_QUEUE_HEAD_INITIALIZER(VAR .wq), \ |
| 560 | .readers = LIST_HEAD_INIT(VAR .readers), \ |
| 561 | .mutex = __MUTEX_INITIALIZER(VAR .mutex), \ |
| 562 | .w_off = 0, \ |
| 563 | .head = 0, \ |
| 564 | .size = SIZE, \ |
| 565 | }; |
| 566 | |
JP Abgrall | 2b37495 | 2011-08-11 21:33:35 -0700 | [diff] [blame] | 567 | DEFINE_LOGGER_DEVICE(log_main, LOGGER_LOG_MAIN, 256*1024) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 568 | DEFINE_LOGGER_DEVICE(log_events, LOGGER_LOG_EVENTS, 256*1024) |
JP Abgrall | 2b37495 | 2011-08-11 21:33:35 -0700 | [diff] [blame] | 569 | DEFINE_LOGGER_DEVICE(log_radio, LOGGER_LOG_RADIO, 256*1024) |
| 570 | DEFINE_LOGGER_DEVICE(log_system, LOGGER_LOG_SYSTEM, 256*1024) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 571 | |
| 572 | static struct logger_log *get_log_from_minor(int minor) |
| 573 | { |
| 574 | if (log_main.misc.minor == minor) |
| 575 | return &log_main; |
| 576 | if (log_events.misc.minor == minor) |
| 577 | return &log_events; |
| 578 | if (log_radio.misc.minor == minor) |
| 579 | return &log_radio; |
San Mehat | 3537cda | 2010-02-23 16:09:47 -0800 | [diff] [blame] | 580 | if (log_system.misc.minor == minor) |
| 581 | return &log_system; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 582 | return NULL; |
| 583 | } |
| 584 | |
| 585 | static int __init init_log(struct logger_log *log) |
| 586 | { |
| 587 | int ret; |
| 588 | |
| 589 | ret = misc_register(&log->misc); |
| 590 | if (unlikely(ret)) { |
| 591 | printk(KERN_ERR "logger: failed to register misc " |
| 592 | "device for log '%s'!\n", log->misc.name); |
| 593 | return ret; |
| 594 | } |
| 595 | |
| 596 | printk(KERN_INFO "logger: created %luK log '%s'\n", |
| 597 | (unsigned long) log->size >> 10, log->misc.name); |
| 598 | |
| 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | static int __init logger_init(void) |
| 603 | { |
| 604 | int ret; |
| 605 | |
| 606 | ret = init_log(&log_main); |
| 607 | if (unlikely(ret)) |
| 608 | goto out; |
| 609 | |
| 610 | ret = init_log(&log_events); |
| 611 | if (unlikely(ret)) |
| 612 | goto out; |
| 613 | |
| 614 | ret = init_log(&log_radio); |
| 615 | if (unlikely(ret)) |
| 616 | goto out; |
| 617 | |
San Mehat | 3537cda | 2010-02-23 16:09:47 -0800 | [diff] [blame] | 618 | ret = init_log(&log_system); |
| 619 | if (unlikely(ret)) |
| 620 | goto out; |
| 621 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 622 | out: |
| 623 | return ret; |
| 624 | } |
| 625 | device_initcall(logger_init); |