]> Pileus Git - ~andy/linux/blob - drivers/staging/android/logger.c
Merge branch 'omap-serial' of git://git.linaro.org/people/rmk/linux-arm
[~andy/linux] / drivers / staging / android / logger.c
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
20 #define pr_fmt(fmt) "logger: " fmt
21
22 #include <linux/sched.h>
23 #include <linux/module.h>
24 #include <linux/fs.h>
25 #include <linux/miscdevice.h>
26 #include <linux/uaccess.h>
27 #include <linux/poll.h>
28 #include <linux/slab.h>
29 #include <linux/time.h>
30 #include <linux/vmalloc.h>
31 #include "logger.h"
32
33 #include <asm/ioctls.h>
34
35 /**
36  * struct logger_log - represents a specific log, such as 'main' or 'radio'
37  * @buffer:     The actual ring buffer
38  * @misc:       The "misc" device representing the log
39  * @wq:         The wait queue for @readers
40  * @readers:    This log's readers
41  * @mutex:      The mutex that protects the @buffer
42  * @w_off:      The current write head offset
43  * @head:       The head, or location that readers start reading at.
44  * @size:       The size of the log
45  * @logs:       The list of log channels
46  *
47  * This structure lives from module insertion until module removal, so it does
48  * not need additional reference counting. The structure is protected by the
49  * mutex 'mutex'.
50  */
51 struct logger_log {
52         unsigned char           *buffer;
53         struct miscdevice       misc;
54         wait_queue_head_t       wq;
55         struct list_head        readers;
56         struct mutex            mutex;
57         size_t                  w_off;
58         size_t                  head;
59         size_t                  size;
60         struct list_head        logs;
61 };
62
63 static LIST_HEAD(log_list);
64
65
66 /**
67  * struct logger_reader - a logging device open for reading
68  * @log:        The associated log
69  * @list:       The associated entry in @logger_log's list
70  * @r_off:      The current read head offset.
71  *
72  * This object lives from open to release, so we don't need additional
73  * reference counting. The structure is protected by log->mutex.
74  */
75 struct logger_reader {
76         struct logger_log       *log;
77         struct list_head        list;
78         size_t                  r_off;
79 };
80
81 /* logger_offset - returns index 'n' into the log via (optimized) modulus */
82 static size_t logger_offset(struct logger_log *log, size_t n)
83 {
84         return n & (log->size - 1);
85 }
86
87
88 /*
89  * file_get_log - Given a file structure, return the associated log
90  *
91  * This isn't aesthetic. We have several goals:
92  *
93  *      1) Need to quickly obtain the associated log during an I/O operation
94  *      2) Readers need to maintain state (logger_reader)
95  *      3) Writers need to be very fast (open() should be a near no-op)
96  *
97  * In the reader case, we can trivially go file->logger_reader->logger_log.
98  * For a writer, we don't want to maintain a logger_reader, so we just go
99  * file->logger_log. Thus what file->private_data points at depends on whether
100  * or not the file was opened for reading. This function hides that dirtiness.
101  */
102 static inline struct logger_log *file_get_log(struct file *file)
103 {
104         if (file->f_mode & FMODE_READ) {
105                 struct logger_reader *reader = file->private_data;
106                 return reader->log;
107         } else
108                 return file->private_data;
109 }
110
111 /*
112  * get_entry_len - Grabs the length of the payload of the next entry starting
113  * from 'off'.
114  *
115  * An entry length is 2 bytes (16 bits) in host endian order.
116  * In the log, the length does not include the size of the log entry structure.
117  * This function returns the size including the log entry structure.
118  *
119  * Caller needs to hold log->mutex.
120  */
121 static __u32 get_entry_len(struct logger_log *log, size_t off)
122 {
123         __u16 val;
124
125         /* copy 2 bytes from buffer, in memcpy order, */
126         /* handling possible wrap at end of buffer */
127
128         ((__u8 *)&val)[0] = log->buffer[off];
129         if (likely(off+1 < log->size))
130                 ((__u8 *)&val)[1] = log->buffer[off+1];
131         else
132                 ((__u8 *)&val)[1] = log->buffer[0];
133
134         return sizeof(struct logger_entry) + val;
135 }
136
137 /*
138  * do_read_log_to_user - reads exactly 'count' bytes from 'log' into the
139  * user-space buffer 'buf'. Returns 'count' on success.
140  *
141  * Caller must hold log->mutex.
142  */
143 static ssize_t do_read_log_to_user(struct logger_log *log,
144                                    struct logger_reader *reader,
145                                    char __user *buf,
146                                    size_t count)
147 {
148         size_t len;
149
150         /*
151          * We read from the log in two disjoint operations. First, we read from
152          * the current read head offset up to 'count' bytes or to the end of
153          * the log, whichever comes first.
154          */
155         len = min(count, log->size - reader->r_off);
156         if (copy_to_user(buf, log->buffer + reader->r_off, len))
157                 return -EFAULT;
158
159         /*
160          * Second, we read any remaining bytes, starting back at the head of
161          * the log.
162          */
163         if (count != len)
164                 if (copy_to_user(buf + len, log->buffer, count - len))
165                         return -EFAULT;
166
167         reader->r_off = logger_offset(log, reader->r_off + count);
168
169         return count;
170 }
171
172 /*
173  * logger_read - our log's read() method
174  *
175  * Behavior:
176  *
177  *      - O_NONBLOCK works
178  *      - If there are no log entries to read, blocks until log is written to
179  *      - Atomically reads exactly one log entry
180  *
181  * Optimal read size is LOGGER_ENTRY_MAX_LEN. Will set errno to EINVAL if read
182  * buffer is insufficient to hold next entry.
183  */
184 static ssize_t logger_read(struct file *file, char __user *buf,
185                            size_t count, loff_t *pos)
186 {
187         struct logger_reader *reader = file->private_data;
188         struct logger_log *log = reader->log;
189         ssize_t ret;
190         DEFINE_WAIT(wait);
191
192 start:
193         while (1) {
194                 mutex_lock(&log->mutex);
195
196                 prepare_to_wait(&log->wq, &wait, TASK_INTERRUPTIBLE);
197
198                 ret = (log->w_off == reader->r_off);
199                 mutex_unlock(&log->mutex);
200                 if (!ret)
201                         break;
202
203                 if (file->f_flags & O_NONBLOCK) {
204                         ret = -EAGAIN;
205                         break;
206                 }
207
208                 if (signal_pending(current)) {
209                         ret = -EINTR;
210                         break;
211                 }
212
213                 schedule();
214         }
215
216         finish_wait(&log->wq, &wait);
217         if (ret)
218                 return ret;
219
220         mutex_lock(&log->mutex);
221
222         /* is there still something to read or did we race? */
223         if (unlikely(log->w_off == reader->r_off)) {
224                 mutex_unlock(&log->mutex);
225                 goto start;
226         }
227
228         /* get the size of the next entry */
229         ret = get_entry_len(log, reader->r_off);
230         if (count < ret) {
231                 ret = -EINVAL;
232                 goto out;
233         }
234
235         /* get exactly one entry from the log */
236         ret = do_read_log_to_user(log, reader, buf, ret);
237
238 out:
239         mutex_unlock(&log->mutex);
240
241         return ret;
242 }
243
244 /*
245  * get_next_entry - return the offset of the first valid entry at least 'len'
246  * bytes after 'off'.
247  *
248  * Caller must hold log->mutex.
249  */
250 static size_t get_next_entry(struct logger_log *log, size_t off, size_t len)
251 {
252         size_t count = 0;
253
254         do {
255                 size_t nr = get_entry_len(log, off);
256                 off = logger_offset(log, off + nr);
257                 count += nr;
258         } while (count < len);
259
260         return off;
261 }
262
263 /*
264  * is_between - is a < c < b, accounting for wrapping of a, b, and c
265  *    positions in the buffer
266  *
267  * That is, if a<b, check for c between a and b
268  * and if a>b, check for c outside (not between) a and b
269  *
270  * |------- a xxxxxxxx b --------|
271  *               c^
272  *
273  * |xxxxx b --------- a xxxxxxxxx|
274  *    c^
275  *  or                    c^
276  */
277 static inline int is_between(size_t a, size_t b, size_t c)
278 {
279         if (a < b) {
280                 /* is c between a and b? */
281                 if (a < c && c <= b)
282                         return 1;
283         } else {
284                 /* is c outside of b through a? */
285                 if (c <= b || a < c)
286                         return 1;
287         }
288
289         return 0;
290 }
291
292 /*
293  * fix_up_readers - walk the list of all readers and "fix up" any who were
294  * lapped by the writer; also do the same for the default "start head".
295  * We do this by "pulling forward" the readers and start head to the first
296  * entry after the new write head.
297  *
298  * The caller needs to hold log->mutex.
299  */
300 static void fix_up_readers(struct logger_log *log, size_t len)
301 {
302         size_t old = log->w_off;
303         size_t new = logger_offset(log, old + len);
304         struct logger_reader *reader;
305
306         if (is_between(old, new, log->head))
307                 log->head = get_next_entry(log, log->head, len);
308
309         list_for_each_entry(reader, &log->readers, list)
310                 if (is_between(old, new, reader->r_off))
311                         reader->r_off = get_next_entry(log, reader->r_off, len);
312 }
313
314 /*
315  * do_write_log - writes 'len' bytes from 'buf' to 'log'
316  *
317  * The caller needs to hold log->mutex.
318  */
319 static void do_write_log(struct logger_log *log, const void *buf, size_t count)
320 {
321         size_t len;
322
323         len = min(count, log->size - log->w_off);
324         memcpy(log->buffer + log->w_off, buf, len);
325
326         if (count != len)
327                 memcpy(log->buffer, buf + len, count - len);
328
329         log->w_off = logger_offset(log, log->w_off + count);
330
331 }
332
333 /*
334  * do_write_log_user - writes 'len' bytes from the user-space buffer 'buf' to
335  * the log 'log'
336  *
337  * The caller needs to hold log->mutex.
338  *
339  * Returns 'count' on success, negative error code on failure.
340  */
341 static ssize_t do_write_log_from_user(struct logger_log *log,
342                                       const void __user *buf, size_t count)
343 {
344         size_t len;
345
346         len = min(count, log->size - log->w_off);
347         if (len && copy_from_user(log->buffer + log->w_off, buf, len))
348                 return -EFAULT;
349
350         if (count != len)
351                 if (copy_from_user(log->buffer, buf + len, count - len))
352                         /*
353                          * Note that by not updating w_off, this abandons the
354                          * portion of the new entry that *was* successfully
355                          * copied, just above.  This is intentional to avoid
356                          * message corruption from missing fragments.
357                          */
358                         return -EFAULT;
359
360         log->w_off = logger_offset(log, log->w_off + count);
361
362         return count;
363 }
364
365 /*
366  * logger_aio_write - our write method, implementing support for write(),
367  * writev(), and aio_write(). Writes are our fast path, and we try to optimize
368  * them above all else.
369  */
370 static ssize_t logger_aio_write(struct kiocb *iocb, const struct iovec *iov,
371                          unsigned long nr_segs, loff_t ppos)
372 {
373         struct logger_log *log = file_get_log(iocb->ki_filp);
374         size_t orig = log->w_off;
375         struct logger_entry header;
376         struct timespec now;
377         ssize_t ret = 0;
378
379         now = current_kernel_time();
380
381         header.pid = current->tgid;
382         header.tid = current->pid;
383         header.sec = now.tv_sec;
384         header.nsec = now.tv_nsec;
385         header.len = min_t(size_t, iocb->ki_left, LOGGER_ENTRY_MAX_PAYLOAD);
386
387         /* null writes succeed, return zero */
388         if (unlikely(!header.len))
389                 return 0;
390
391         mutex_lock(&log->mutex);
392
393         /*
394          * Fix up any readers, pulling them forward to the first readable
395          * entry after (what will be) the new write offset. We do this now
396          * because if we partially fail, we can end up with clobbered log
397          * entries that encroach on readable buffer.
398          */
399         fix_up_readers(log, sizeof(struct logger_entry) + header.len);
400
401         do_write_log(log, &header, sizeof(struct logger_entry));
402
403         while (nr_segs-- > 0) {
404                 size_t len;
405                 ssize_t nr;
406
407                 /* figure out how much of this vector we can keep */
408                 len = min_t(size_t, iov->iov_len, header.len - ret);
409
410                 /* write out this segment's payload */
411                 nr = do_write_log_from_user(log, iov->iov_base, len);
412                 if (unlikely(nr < 0)) {
413                         log->w_off = orig;
414                         mutex_unlock(&log->mutex);
415                         return nr;
416                 }
417
418                 iov++;
419                 ret += nr;
420         }
421
422         mutex_unlock(&log->mutex);
423
424         /* wake up any blocked readers */
425         wake_up_interruptible(&log->wq);
426
427         return ret;
428 }
429
430 static struct logger_log *get_log_from_minor(int minor)
431 {
432         struct logger_log *log;
433
434         list_for_each_entry(log, &log_list, logs)
435                 if (log->misc.minor == minor)
436                         return log;
437         return NULL;
438 }
439
440 /*
441  * logger_open - the log's open() file operation
442  *
443  * Note how near a no-op this is in the write-only case. Keep it that way!
444  */
445 static int logger_open(struct inode *inode, struct file *file)
446 {
447         struct logger_log *log;
448         int ret;
449
450         ret = nonseekable_open(inode, file);
451         if (ret)
452                 return ret;
453
454         log = get_log_from_minor(MINOR(inode->i_rdev));
455         if (!log)
456                 return -ENODEV;
457
458         if (file->f_mode & FMODE_READ) {
459                 struct logger_reader *reader;
460
461                 reader = kmalloc(sizeof(struct logger_reader), GFP_KERNEL);
462                 if (!reader)
463                         return -ENOMEM;
464
465                 reader->log = log;
466                 INIT_LIST_HEAD(&reader->list);
467
468                 mutex_lock(&log->mutex);
469                 reader->r_off = log->head;
470                 list_add_tail(&reader->list, &log->readers);
471                 mutex_unlock(&log->mutex);
472
473                 file->private_data = reader;
474         } else
475                 file->private_data = log;
476
477         return 0;
478 }
479
480 /*
481  * logger_release - the log's release file operation
482  *
483  * Note this is a total no-op in the write-only case. Keep it that way!
484  */
485 static int logger_release(struct inode *ignored, struct file *file)
486 {
487         if (file->f_mode & FMODE_READ) {
488                 struct logger_reader *reader = file->private_data;
489                 struct logger_log *log = reader->log;
490
491                 mutex_lock(&log->mutex);
492                 list_del(&reader->list);
493                 mutex_unlock(&log->mutex);
494
495                 kfree(reader);
496         }
497
498         return 0;
499 }
500
501 /*
502  * logger_poll - the log's poll file operation, for poll/select/epoll
503  *
504  * Note we always return POLLOUT, because you can always write() to the log.
505  * Note also that, strictly speaking, a return value of POLLIN does not
506  * guarantee that the log is readable without blocking, as there is a small
507  * chance that the writer can lap the reader in the interim between poll()
508  * returning and the read() request.
509  */
510 static unsigned int logger_poll(struct file *file, poll_table *wait)
511 {
512         struct logger_reader *reader;
513         struct logger_log *log;
514         unsigned int ret = POLLOUT | POLLWRNORM;
515
516         if (!(file->f_mode & FMODE_READ))
517                 return ret;
518
519         reader = file->private_data;
520         log = reader->log;
521
522         poll_wait(file, &log->wq, wait);
523
524         mutex_lock(&log->mutex);
525         if (log->w_off != reader->r_off)
526                 ret |= POLLIN | POLLRDNORM;
527         mutex_unlock(&log->mutex);
528
529         return ret;
530 }
531
532 static long logger_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
533 {
534         struct logger_log *log = file_get_log(file);
535         struct logger_reader *reader;
536         long ret = -ENOTTY;
537
538         mutex_lock(&log->mutex);
539
540         switch (cmd) {
541         case LOGGER_GET_LOG_BUF_SIZE:
542                 ret = log->size;
543                 break;
544         case LOGGER_GET_LOG_LEN:
545                 if (!(file->f_mode & FMODE_READ)) {
546                         ret = -EBADF;
547                         break;
548                 }
549                 reader = file->private_data;
550                 if (log->w_off >= reader->r_off)
551                         ret = log->w_off - reader->r_off;
552                 else
553                         ret = (log->size - reader->r_off) + log->w_off;
554                 break;
555         case LOGGER_GET_NEXT_ENTRY_LEN:
556                 if (!(file->f_mode & FMODE_READ)) {
557                         ret = -EBADF;
558                         break;
559                 }
560                 reader = file->private_data;
561                 if (log->w_off != reader->r_off)
562                         ret = get_entry_len(log, reader->r_off);
563                 else
564                         ret = 0;
565                 break;
566         case LOGGER_FLUSH_LOG:
567                 if (!(file->f_mode & FMODE_WRITE)) {
568                         ret = -EBADF;
569                         break;
570                 }
571                 list_for_each_entry(reader, &log->readers, list)
572                         reader->r_off = log->w_off;
573                 log->head = log->w_off;
574                 ret = 0;
575                 break;
576         }
577
578         mutex_unlock(&log->mutex);
579
580         return ret;
581 }
582
583 static const struct file_operations logger_fops = {
584         .owner = THIS_MODULE,
585         .read = logger_read,
586         .aio_write = logger_aio_write,
587         .poll = logger_poll,
588         .unlocked_ioctl = logger_ioctl,
589         .compat_ioctl = logger_ioctl,
590         .open = logger_open,
591         .release = logger_release,
592 };
593
594 /*
595  * Log size must be a power of two, greater than LOGGER_ENTRY_MAX_LEN,
596  * and less than LONG_MAX minus LOGGER_ENTRY_MAX_LEN.
597  */
598 static int __init create_log(char *log_name, int size)
599 {
600         int ret = 0;
601         struct logger_log *log;
602         unsigned char *buffer;
603
604         buffer = vmalloc(size);
605         if (buffer == NULL)
606                 return -ENOMEM;
607
608         log = kzalloc(sizeof(struct logger_log), GFP_KERNEL);
609         if (log == NULL) {
610                 ret = -ENOMEM;
611                 goto out_free_buffer;
612         }
613         log->buffer = buffer;
614
615         log->misc.minor = MISC_DYNAMIC_MINOR;
616         log->misc.name = kstrdup(log_name, GFP_KERNEL);
617         if (log->misc.name == NULL) {
618                 ret = -ENOMEM;
619                 goto out_free_log;
620         }
621
622         log->misc.fops = &logger_fops;
623         log->misc.parent = NULL;
624
625         init_waitqueue_head(&log->wq);
626         INIT_LIST_HEAD(&log->readers);
627         mutex_init(&log->mutex);
628         log->w_off = 0;
629         log->head = 0;
630         log->size = size;
631
632         INIT_LIST_HEAD(&log->logs);
633         list_add_tail(&log->logs, &log_list);
634
635         /* finally, initialize the misc device for this log */
636         ret = misc_register(&log->misc);
637         if (unlikely(ret)) {
638                 pr_err("failed to register misc device for log '%s'!\n",
639                                 log->misc.name);
640                 goto out_free_log;
641         }
642
643         pr_info("created %luK log '%s'\n",
644                 (unsigned long) log->size >> 10, log->misc.name);
645
646         return 0;
647
648 out_free_log:
649         kfree(log);
650
651 out_free_buffer:
652         vfree(buffer);
653         return ret;
654 }
655
656 static int __init logger_init(void)
657 {
658         int ret;
659
660         ret = create_log(LOGGER_LOG_MAIN, 256*1024);
661         if (unlikely(ret))
662                 goto out;
663
664         ret = create_log(LOGGER_LOG_EVENTS, 256*1024);
665         if (unlikely(ret))
666                 goto out;
667
668         ret = create_log(LOGGER_LOG_RADIO, 256*1024);
669         if (unlikely(ret))
670                 goto out;
671
672         ret = create_log(LOGGER_LOG_SYSTEM, 256*1024);
673         if (unlikely(ret))
674                 goto out;
675
676 out:
677         return ret;
678 }
679
680 static void __exit logger_exit(void)
681 {
682         struct logger_log *current_log, *next_log;
683
684         list_for_each_entry_safe(current_log, next_log, &log_list, logs) {
685                 /* we have to delete all the entry inside log_list */
686                 misc_deregister(&current_log->misc);
687                 vfree(current_log->buffer);
688                 kfree(current_log->misc.name);
689                 list_del(&current_log->logs);
690                 kfree(current_log);
691         }
692 }
693
694
695 device_initcall(logger_init);
696 module_exit(logger_exit);
697
698 MODULE_LICENSE("GPL");
699 MODULE_AUTHOR("Robert Love, <rlove@google.com>");
700 MODULE_DESCRIPTION("Android Logger");