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