2 * drivers/misc/logger.c
6 * Copyright (C) 2007-2008 Google, Inc.
8 * Robert Love <rlove@google.com>
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.
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.
20 #include <linux/sched.h>
21 #include <linux/module.h>
23 #include <linux/miscdevice.h>
24 #include <linux/uaccess.h>
25 #include <linux/poll.h>
26 #include <linux/slab.h>
27 #include <linux/time.h>
30 #include <asm/ioctls.h>
33 * struct logger_log - represents a specific log, such as 'main' or 'radio'
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
40 unsigned char *buffer;/* the ring buffer itself */
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 */
51 * struct logger_reader - a logging device open for reading
53 * This object lives from open to release, so we don't need additional
54 * reference counting. The structure is protected by log->mutex.
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 */
62 /* logger_offset - returns index 'n' into the log via (optimized) modulus */
63 size_t logger_offset(struct logger_log *log, size_t n)
65 return n & (log->size-1);
70 * file_get_log - Given a file structure, return the associated log
72 * This isn't aesthetic. We have several goals:
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)
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.
83 static inline struct logger_log *file_get_log(struct file *file)
85 if (file->f_mode & FMODE_READ) {
86 struct logger_reader *reader = file->private_data;
89 return file->private_data;
93 * get_entry_len - Grabs the length of the payload of the next entry starting
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.
100 * Caller needs to hold log->mutex.
102 static __u32 get_entry_len(struct logger_log *log, size_t off)
106 /* copy 2 bytes from buffer, in memcpy order, */
107 /* handling possible wrap at end of buffer */
109 ((__u8 *)&val)[0] = log->buffer[off];
110 if (likely(off+1 < log->size))
111 ((__u8 *)&val)[1] = log->buffer[off+1];
113 ((__u8 *)&val)[1] = log->buffer[0];
115 return sizeof(struct logger_entry) + val;
119 * do_read_log_to_user - reads exactly 'count' bytes from 'log' into the
120 * user-space buffer 'buf'. Returns 'count' on success.
122 * Caller must hold log->mutex.
124 static ssize_t do_read_log_to_user(struct logger_log *log,
125 struct logger_reader *reader,
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.
136 len = min(count, log->size - reader->r_off);
137 if (copy_to_user(buf, log->buffer + reader->r_off, len))
141 * Second, we read any remaining bytes, starting back at the head of
145 if (copy_to_user(buf + len, log->buffer, count - len))
148 reader->r_off = logger_offset(log, reader->r_off + count);
154 * logger_read - our log's read() method
159 * - If there are no log entries to read, blocks until log is written to
160 * - Atomically reads exactly one log entry
162 * Optimal read size is LOGGER_ENTRY_MAX_LEN. Will set errno to EINVAL if read
163 * buffer is insufficient to hold next entry.
165 static ssize_t logger_read(struct file *file, char __user *buf,
166 size_t count, loff_t *pos)
168 struct logger_reader *reader = file->private_data;
169 struct logger_log *log = reader->log;
175 mutex_lock(&log->mutex);
177 prepare_to_wait(&log->wq, &wait, TASK_INTERRUPTIBLE);
179 ret = (log->w_off == reader->r_off);
180 mutex_unlock(&log->mutex);
184 if (file->f_flags & O_NONBLOCK) {
189 if (signal_pending(current)) {
197 finish_wait(&log->wq, &wait);
201 mutex_lock(&log->mutex);
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);
209 /* get the size of the next entry */
210 ret = get_entry_len(log, reader->r_off);
216 /* get exactly one entry from the log */
217 ret = do_read_log_to_user(log, reader, buf, ret);
220 mutex_unlock(&log->mutex);
226 * get_next_entry - return the offset of the first valid entry at least 'len'
229 * Caller must hold log->mutex.
231 static size_t get_next_entry(struct logger_log *log, size_t off, size_t len)
236 size_t nr = get_entry_len(log, off);
237 off = logger_offset(log, off + nr);
239 } while (count < len);
245 * is_between - is a < c < b, accounting for wrapping of a, b, and c
246 * positions in the buffer
248 * That is, if a<b, check for c between a and b
249 * and if a>b, check for c outside (not between) a and b
251 * |------- a xxxxxxxx b --------|
254 * |xxxxx b --------- a xxxxxxxxx|
258 static inline int is_between(size_t a, size_t b, size_t c)
261 /* is c between a and b? */
265 /* is c outside of b through a? */
274 * fix_up_readers - walk the list of all readers and "fix up" any who were
275 * lapped by the writer; also do the same for the default "start head".
276 * We do this by "pulling forward" the readers and start head to the first
277 * entry after the new write head.
279 * The caller needs to hold log->mutex.
281 static void fix_up_readers(struct logger_log *log, size_t len)
283 size_t old = log->w_off;
284 size_t new = logger_offset(log, old + len);
285 struct logger_reader *reader;
287 if (is_between(old, new, log->head))
288 log->head = get_next_entry(log, log->head, len);
290 list_for_each_entry(reader, &log->readers, list)
291 if (is_between(old, new, reader->r_off))
292 reader->r_off = get_next_entry(log, reader->r_off, len);
296 * do_write_log - writes 'len' bytes from 'buf' to 'log'
298 * The caller needs to hold log->mutex.
300 static void do_write_log(struct logger_log *log, const void *buf, size_t count)
304 len = min(count, log->size - log->w_off);
305 memcpy(log->buffer + log->w_off, buf, len);
308 memcpy(log->buffer, buf + len, count - len);
310 log->w_off = logger_offset(log, log->w_off + count);
315 * do_write_log_user - writes 'len' bytes from the user-space buffer 'buf' to
318 * The caller needs to hold log->mutex.
320 * Returns 'count' on success, negative error code on failure.
322 static ssize_t do_write_log_from_user(struct logger_log *log,
323 const void __user *buf, size_t count)
327 len = min(count, log->size - log->w_off);
328 if (len && copy_from_user(log->buffer + log->w_off, buf, len))
332 if (copy_from_user(log->buffer, buf + len, count - len))
334 * Note that by not updating w_off, this abandons the
335 * portion of the new entry that *was* successfully
336 * copied, just above. This is intentional to avoid
337 * message corruption from missing fragments.
341 log->w_off = logger_offset(log, log->w_off + count);
347 * logger_aio_write - our write method, implementing support for write(),
348 * writev(), and aio_write(). Writes are our fast path, and we try to optimize
349 * them above all else.
351 ssize_t logger_aio_write(struct kiocb *iocb, const struct iovec *iov,
352 unsigned long nr_segs, loff_t ppos)
354 struct logger_log *log = file_get_log(iocb->ki_filp);
355 size_t orig = log->w_off;
356 struct logger_entry header;
360 now = current_kernel_time();
362 header.pid = current->tgid;
363 header.tid = current->pid;
364 header.sec = now.tv_sec;
365 header.nsec = now.tv_nsec;
366 header.len = min_t(size_t, iocb->ki_left, LOGGER_ENTRY_MAX_PAYLOAD);
368 /* null writes succeed, return zero */
369 if (unlikely(!header.len))
372 mutex_lock(&log->mutex);
375 * Fix up any readers, pulling them forward to the first readable
376 * entry after (what will be) the new write offset. We do this now
377 * because if we partially fail, we can end up with clobbered log
378 * entries that encroach on readable buffer.
380 fix_up_readers(log, sizeof(struct logger_entry) + header.len);
382 do_write_log(log, &header, sizeof(struct logger_entry));
384 while (nr_segs-- > 0) {
388 /* figure out how much of this vector we can keep */
389 len = min_t(size_t, iov->iov_len, header.len - ret);
391 /* write out this segment's payload */
392 nr = do_write_log_from_user(log, iov->iov_base, len);
393 if (unlikely(nr < 0)) {
395 mutex_unlock(&log->mutex);
403 mutex_unlock(&log->mutex);
405 /* wake up any blocked readers */
406 wake_up_interruptible(&log->wq);
411 static struct logger_log *get_log_from_minor(int);
414 * logger_open - the log's open() file operation
416 * Note how near a no-op this is in the write-only case. Keep it that way!
418 static int logger_open(struct inode *inode, struct file *file)
420 struct logger_log *log;
423 ret = nonseekable_open(inode, file);
427 log = get_log_from_minor(MINOR(inode->i_rdev));
431 if (file->f_mode & FMODE_READ) {
432 struct logger_reader *reader;
434 reader = kmalloc(sizeof(struct logger_reader), GFP_KERNEL);
439 INIT_LIST_HEAD(&reader->list);
441 mutex_lock(&log->mutex);
442 reader->r_off = log->head;
443 list_add_tail(&reader->list, &log->readers);
444 mutex_unlock(&log->mutex);
446 file->private_data = reader;
448 file->private_data = log;
454 * logger_release - the log's release file operation
456 * Note this is a total no-op in the write-only case. Keep it that way!
458 static int logger_release(struct inode *ignored, struct file *file)
460 if (file->f_mode & FMODE_READ) {
461 struct logger_reader *reader = file->private_data;
462 struct logger_log *log = reader->log;
464 mutex_lock(&log->mutex);
465 list_del(&reader->list);
466 mutex_unlock(&log->mutex);
475 * logger_poll - the log's poll file operation, for poll/select/epoll
477 * Note we always return POLLOUT, because you can always write() to the log.
478 * Note also that, strictly speaking, a return value of POLLIN does not
479 * guarantee that the log is readable without blocking, as there is a small
480 * chance that the writer can lap the reader in the interim between poll()
481 * returning and the read() request.
483 static unsigned int logger_poll(struct file *file, poll_table *wait)
485 struct logger_reader *reader;
486 struct logger_log *log;
487 unsigned int ret = POLLOUT | POLLWRNORM;
489 if (!(file->f_mode & FMODE_READ))
492 reader = file->private_data;
495 poll_wait(file, &log->wq, wait);
497 mutex_lock(&log->mutex);
498 if (log->w_off != reader->r_off)
499 ret |= POLLIN | POLLRDNORM;
500 mutex_unlock(&log->mutex);
505 static long logger_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
507 struct logger_log *log = file_get_log(file);
508 struct logger_reader *reader;
511 mutex_lock(&log->mutex);
514 case LOGGER_GET_LOG_BUF_SIZE:
517 case LOGGER_GET_LOG_LEN:
518 if (!(file->f_mode & FMODE_READ)) {
522 reader = file->private_data;
523 if (log->w_off >= reader->r_off)
524 ret = log->w_off - reader->r_off;
526 ret = (log->size - reader->r_off) + log->w_off;
528 case LOGGER_GET_NEXT_ENTRY_LEN:
529 if (!(file->f_mode & FMODE_READ)) {
533 reader = file->private_data;
534 if (log->w_off != reader->r_off)
535 ret = get_entry_len(log, reader->r_off);
539 case LOGGER_FLUSH_LOG:
540 if (!(file->f_mode & FMODE_WRITE)) {
544 list_for_each_entry(reader, &log->readers, list)
545 reader->r_off = log->w_off;
546 log->head = log->w_off;
551 mutex_unlock(&log->mutex);
556 static const struct file_operations logger_fops = {
557 .owner = THIS_MODULE,
559 .aio_write = logger_aio_write,
561 .unlocked_ioctl = logger_ioctl,
562 .compat_ioctl = logger_ioctl,
564 .release = logger_release,
568 * Defines a log structure with name 'NAME' and a size of 'SIZE' bytes, which
569 * must be a power of two, greater than LOGGER_ENTRY_MAX_LEN, and less than
570 * LONG_MAX minus LOGGER_ENTRY_MAX_LEN.
572 #define DEFINE_LOGGER_DEVICE(VAR, NAME, SIZE) \
573 static unsigned char _buf_ ## VAR[SIZE]; \
574 static struct logger_log VAR = { \
575 .buffer = _buf_ ## VAR, \
577 .minor = MISC_DYNAMIC_MINOR, \
579 .fops = &logger_fops, \
582 .wq = __WAIT_QUEUE_HEAD_INITIALIZER(VAR .wq), \
583 .readers = LIST_HEAD_INIT(VAR .readers), \
584 .mutex = __MUTEX_INITIALIZER(VAR .mutex), \
590 DEFINE_LOGGER_DEVICE(log_main, LOGGER_LOG_MAIN, 256*1024)
591 DEFINE_LOGGER_DEVICE(log_events, LOGGER_LOG_EVENTS, 256*1024)
592 DEFINE_LOGGER_DEVICE(log_radio, LOGGER_LOG_RADIO, 256*1024)
593 DEFINE_LOGGER_DEVICE(log_system, LOGGER_LOG_SYSTEM, 256*1024)
595 static struct logger_log *get_log_from_minor(int minor)
597 if (log_main.misc.minor == minor)
599 if (log_events.misc.minor == minor)
601 if (log_radio.misc.minor == minor)
603 if (log_system.misc.minor == minor)
608 static int __init init_log(struct logger_log *log)
612 ret = misc_register(&log->misc);
614 printk(KERN_ERR "logger: failed to register misc "
615 "device for log '%s'!\n", log->misc.name);
619 printk(KERN_INFO "logger: created %luK log '%s'\n",
620 (unsigned long) log->size >> 10, log->misc.name);
625 static int __init logger_init(void)
629 ret = init_log(&log_main);
633 ret = init_log(&log_events);
637 ret = init_log(&log_radio);
641 ret = init_log(&log_system);
648 device_initcall(logger_init);