string: factorize skip_spaces and export it to be generally available
[safe/jmp/linux-2.6] / drivers / mtd / mtdoops.c
index 34681bc..1060337 100644 (file)
 #include <linux/workqueue.h>
 #include <linux/sched.h>
 #include <linux/wait.h>
+#include <linux/delay.h>
 #include <linux/spinlock.h>
+#include <linux/interrupt.h>
 #include <linux/mtd/mtd.h>
 
+#define MTDOOPS_KERNMSG_MAGIC 0x5d005d00
 #define OOPS_PAGE_SIZE 4096
 
-struct mtdoops_context {
+static struct mtdoops_context {
        int mtd_index;
        struct work_struct work_erase;
        struct work_struct work_write;
@@ -41,6 +44,7 @@ struct mtdoops_context {
        int oops_pages;
        int nextpage;
        int nextcount;
+       char *name;
 
        void *oops_buf;
 
@@ -77,9 +81,9 @@ static int mtdoops_erase_block(struct mtd_info *mtd, int offset)
        if (ret) {
                set_current_state(TASK_RUNNING);
                remove_wait_queue(&wait_q, &wait);
-               printk (KERN_WARNING "mtdoops: erase of region [0x%x, 0x%x] "
+               printk (KERN_WARNING "mtdoops: erase of region [0x%llx, 0x%llx] "
                                     "on \"%s\" failed\n",
-                       erase.addr, erase.len, mtd->name);
+                       (unsigned long long)erase.addr, (unsigned long long)erase.len, mtd->name);
                return ret;
        }
 
@@ -97,7 +101,7 @@ static void mtdoops_inc_counter(struct mtdoops_context *cxt)
        int ret;
 
        cxt->nextpage++;
-       if (cxt->nextpage > cxt->oops_pages)
+       if (cxt->nextpage >= cxt->oops_pages)
                cxt->nextpage = 0;
        cxt->nextcount++;
        if (cxt->nextcount == 0xffffffff)
@@ -139,7 +143,7 @@ static void mtdoops_workfunc_erase(struct work_struct *work)
        mod = (cxt->nextpage * OOPS_PAGE_SIZE) % mtd->erasesize;
        if (mod != 0) {
                cxt->nextpage = cxt->nextpage + ((mtd->erasesize - mod) / OOPS_PAGE_SIZE);
-               if (cxt->nextpage > cxt->oops_pages)
+               if (cxt->nextpage >= cxt->oops_pages)
                        cxt->nextpage = 0;
        }
 
@@ -156,7 +160,7 @@ badblock:
                                cxt->nextpage * OOPS_PAGE_SIZE);
                i++;
                cxt->nextpage = cxt->nextpage + (mtd->erasesize / OOPS_PAGE_SIZE);
-               if (cxt->nextpage > cxt->oops_pages)
+               if (cxt->nextpage >= cxt->oops_pages)
                        cxt->nextpage = 0;
                if (i == (cxt->oops_pages / (mtd->erasesize / OOPS_PAGE_SIZE))) {
                        printk(KERN_ERR "mtdoops: All blocks bad!\n");
@@ -183,10 +187,8 @@ badblock:
        goto badblock;
 }
 
-static void mtdoops_workfunc_write(struct work_struct *work)
+static void mtdoops_write(struct mtdoops_context *cxt, int panic)
 {
-       struct mtdoops_context *cxt =
-                       container_of(work, struct mtdoops_context, work_write);
        struct mtd_info *mtd = cxt->mtd;
        size_t retlen;
        int ret;
@@ -195,7 +197,11 @@ static void mtdoops_workfunc_write(struct work_struct *work)
                memset(cxt->oops_buf + cxt->writecount, 0xff,
                                        OOPS_PAGE_SIZE - cxt->writecount);
 
-       ret = mtd->write(mtd, cxt->nextpage * OOPS_PAGE_SIZE,
+       if (panic)
+               ret = mtd->panic_write(mtd, cxt->nextpage * OOPS_PAGE_SIZE,
+                                       OOPS_PAGE_SIZE, &retlen, cxt->oops_buf);
+       else
+               ret = mtd->write(mtd, cxt->nextpage * OOPS_PAGE_SIZE,
                                        OOPS_PAGE_SIZE, &retlen, cxt->oops_buf);
 
        cxt->writecount = 0;
@@ -205,46 +211,55 @@ static void mtdoops_workfunc_write(struct work_struct *work)
                        cxt->nextpage * OOPS_PAGE_SIZE, retlen, OOPS_PAGE_SIZE, ret);
 
        mtdoops_inc_counter(cxt);
+}
+
+
+static void mtdoops_workfunc_write(struct work_struct *work)
+{
+       struct mtdoops_context *cxt =
+                       container_of(work, struct mtdoops_context, work_write);
+
+       mtdoops_write(cxt, 0);
 }                                      
 
 static void find_next_position(struct mtdoops_context *cxt)
 {
        struct mtd_info *mtd = cxt->mtd;
        int ret, page, maxpos = 0;
-       u32 count, maxcount = 0xffffffff;
+       u32 count[2], maxcount = 0xffffffff;
        size_t retlen;
 
        for (page = 0; page < cxt->oops_pages; page++) {
-               ret = mtd->read(mtd, page * OOPS_PAGE_SIZE, 4, &retlen, (u_char *) &count);
-               if ((retlen != 4) || ((ret < 0) && (ret != -EUCLEAN))) {
-                       printk(KERN_ERR "mtdoops: Read failure at %d (%td of 4 read)"
+               ret = mtd->read(mtd, page * OOPS_PAGE_SIZE, 8, &retlen, (u_char *) &count[0]);
+               if ((retlen != 8) || ((ret < 0) && (ret != -EUCLEAN))) {
+                       printk(KERN_ERR "mtdoops: Read failure at %d (%td of 8 read)"
                                ", err %d.\n", page * OOPS_PAGE_SIZE, retlen, ret);
                        continue;
                }
 
-               if (count == 0xffffffff)
+               if (count[1] != MTDOOPS_KERNMSG_MAGIC)
+                       continue;
+               if (count[0] == 0xffffffff)
                        continue;
                if (maxcount == 0xffffffff) {
-                       maxcount = count;
+                       maxcount = count[0];
                        maxpos = page;
-               } else if ((count < 0x40000000) && (maxcount > 0xc0000000)) {
-                       maxcount = count;
+               } else if ((count[0] < 0x40000000) && (maxcount > 0xc0000000)) {
+                       maxcount = count[0];
                        maxpos = page;
-               } else if ((count > maxcount) && (count < 0xc0000000)) {
-                       maxcount = count;
+               } else if ((count[0] > maxcount) && (count[0] < 0xc0000000)) {
+                       maxcount = count[0];
                        maxpos = page;
-               } else if ((count > maxcount) && (count > 0xc0000000)
+               } else if ((count[0] > maxcount) && (count[0] > 0xc0000000)
                                        && (maxcount > 0x80000000)) {
-                       maxcount = count;
+                       maxcount = count[0];
                        maxpos = page;
                }
        }
        if (maxcount == 0xffffffff) {
                cxt->nextpage = 0;
                cxt->nextcount = 1;
-               cxt->ready = 1;
-               printk(KERN_DEBUG "mtdoops: Ready %d, %d (first init)\n",
-                               cxt->nextpage, cxt->nextcount);
+               schedule_work(&cxt->work_erase);
                return;
        }
 
@@ -259,6 +274,9 @@ static void mtdoops_notify_add(struct mtd_info *mtd)
 {
        struct mtdoops_context *cxt = &oops_cxt;
 
+       if (cxt->name && !strcmp(mtd->name, cxt->name))
+               cxt->mtd_index = mtd->index;
+
        if ((mtd->index != cxt->mtd_index) || cxt->mtd_index < 0)
                return;
 
@@ -275,7 +293,10 @@ static void mtdoops_notify_add(struct mtd_info *mtd)
        }
 
        cxt->mtd = mtd;
-       cxt->oops_pages = mtd->size / OOPS_PAGE_SIZE;
+       if (mtd->size > INT_MAX)
+               cxt->oops_pages = INT_MAX / OOPS_PAGE_SIZE;
+       else
+               cxt->oops_pages = (int)mtd->size / OOPS_PAGE_SIZE;
 
        find_next_position(cxt);
 
@@ -314,7 +335,11 @@ static void mtdoops_console_sync(void)
        cxt->ready = 0;
        spin_unlock_irqrestore(&cxt->writecount_lock, flags);
 
-       schedule_work(&cxt->work_write);
+       if (mtd->panic_write && in_interrupt())
+               /* Interrupt context, we're going to panic so try and log */
+               mtdoops_write(cxt, 1);
+       else
+               schedule_work(&cxt->work_write);
 }
 
 static void
@@ -336,13 +361,16 @@ mtdoops_console_write(struct console *co, const char *s, unsigned int count)
        spin_lock_irqsave(&cxt->writecount_lock, flags);
 
        /* Check ready status didn't change whilst waiting for the lock */
-       if (!cxt->ready)
+       if (!cxt->ready) {
+               spin_unlock_irqrestore(&cxt->writecount_lock, flags);
                return;
+       }
 
        if (cxt->writecount == 0) {
                u32 *stamp = cxt->oops_buf;
-               *stamp = cxt->nextcount;
-               cxt->writecount = 4;
+               *stamp++ = cxt->nextcount;
+               *stamp = MTDOOPS_KERNMSG_MAGIC;
+               cxt->writecount = 8;
        }
 
        if ((count + cxt->writecount) > OOPS_PAGE_SIZE)
@@ -361,8 +389,12 @@ static int __init mtdoops_console_setup(struct console *co, char *options)
 {
        struct mtdoops_context *cxt = co->data;
 
-       if (cxt->mtd_index != -1)
+       if (cxt->mtd_index != -1 || cxt->name)
                return -EBUSY;
+       if (options) {
+               cxt->name = kstrdup(options, GFP_KERNEL);
+               return 0;
+       }
        if (co->index == -1)
                return -EINVAL;
 
@@ -390,6 +422,7 @@ static int __init mtdoops_console_init(void)
 
        cxt->mtd_index = -1;
        cxt->oops_buf = vmalloc(OOPS_PAGE_SIZE);
+       spin_lock_init(&cxt->writecount_lock);
 
        if (!cxt->oops_buf) {
                printk(KERN_ERR "Failed to allocate mtdoops buffer workspace\n");
@@ -410,6 +443,7 @@ static void __exit mtdoops_console_exit(void)
 
        unregister_mtd_user(&mtdoops_notifier);
        unregister_console(&mtdoops_console);
+       kfree(cxt->name);
        vfree(cxt->oops_buf);
 }