pstore: defer inserting OOPS entries into pstore
[linux-2.6-block.git] / fs / pstore / platform.c
1 /*
2  * Persistent Storage - platform driver interface parts.
3  *
4  * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License version 2 as
8  *  published by the Free Software Foundation.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include <linux/atomic.h>
21 #include <linux/types.h>
22 #include <linux/errno.h>
23 #include <linux/init.h>
24 #include <linux/kmsg_dump.h>
25 #include <linux/module.h>
26 #include <linux/pstore.h>
27 #include <linux/string.h>
28 #include <linux/timer.h>
29 #include <linux/slab.h>
30 #include <linux/uaccess.h>
31 #include <linux/workqueue.h>
32
33 #include "internal.h"
34
35 /*
36  * We defer making "oops" entries appear in pstore - see
37  * whether the system is actually still running well enough
38  * to let someone see the entry
39  */
40 #define PSTORE_INTERVAL (60 * HZ)
41
42 static int pstore_new_entry;
43
44 static void pstore_timefunc(unsigned long);
45 static DEFINE_TIMER(pstore_timer, pstore_timefunc, 0, 0);
46
47 static void pstore_dowork(struct work_struct *);
48 static DECLARE_WORK(pstore_work, pstore_dowork);
49
50 /*
51  * pstore_lock just protects "psinfo" during
52  * calls to pstore_register()
53  */
54 static DEFINE_SPINLOCK(pstore_lock);
55 static struct pstore_info *psinfo;
56
57 static char *backend;
58
59 /* How much of the console log to snapshot */
60 static unsigned long kmsg_bytes = 10240;
61
62 void pstore_set_kmsg_bytes(int bytes)
63 {
64         kmsg_bytes = bytes;
65 }
66
67 /* Tag each group of saved records with a sequence number */
68 static int      oopscount;
69
70 static char *reason_str[] = {
71         "Oops", "Panic", "Kexec", "Restart", "Halt", "Poweroff", "Emergency"
72 };
73
74 /*
75  * callback from kmsg_dump. (s2,l2) has the most recently
76  * written bytes, older bytes are in (s1,l1). Save as much
77  * as we can from the end of the buffer.
78  */
79 static void pstore_dump(struct kmsg_dumper *dumper,
80             enum kmsg_dump_reason reason,
81             const char *s1, unsigned long l1,
82             const char *s2, unsigned long l2)
83 {
84         unsigned long   s1_start, s2_start;
85         unsigned long   l1_cpy, l2_cpy;
86         unsigned long   size, total = 0;
87         char            *dst, *why;
88         u64             id;
89         int             hsize;
90         unsigned int    part = 1;
91
92         if (reason < ARRAY_SIZE(reason_str))
93                 why = reason_str[reason];
94         else
95                 why = "Unknown";
96
97         mutex_lock(&psinfo->buf_mutex);
98         oopscount++;
99         while (total < kmsg_bytes) {
100                 dst = psinfo->buf;
101                 hsize = sprintf(dst, "%s#%d Part%d\n", why, oopscount, part);
102                 size = psinfo->bufsize - hsize;
103                 dst += hsize;
104
105                 l2_cpy = min(l2, size);
106                 l1_cpy = min(l1, size - l2_cpy);
107
108                 if (l1_cpy + l2_cpy == 0)
109                         break;
110
111                 s2_start = l2 - l2_cpy;
112                 s1_start = l1 - l1_cpy;
113
114                 memcpy(dst, s1 + s1_start, l1_cpy);
115                 memcpy(dst + l1_cpy, s2 + s2_start, l2_cpy);
116
117                 id = psinfo->write(PSTORE_TYPE_DMESG, part,
118                                    hsize + l1_cpy + l2_cpy, psinfo);
119                 if (reason == KMSG_DUMP_OOPS && pstore_is_mounted())
120                         pstore_new_entry = 1;
121                 l1 -= l1_cpy;
122                 l2 -= l2_cpy;
123                 total += l1_cpy + l2_cpy;
124                 part++;
125         }
126         mutex_unlock(&psinfo->buf_mutex);
127 }
128
129 static struct kmsg_dumper pstore_dumper = {
130         .dump = pstore_dump,
131 };
132
133 /*
134  * platform specific persistent storage driver registers with
135  * us here. If pstore is already mounted, call the platform
136  * read function right away to populate the file system. If not
137  * then the pstore mount code will call us later to fill out
138  * the file system.
139  *
140  * Register with kmsg_dump to save last part of console log on panic.
141  */
142 int pstore_register(struct pstore_info *psi)
143 {
144         struct module *owner = psi->owner;
145
146         spin_lock(&pstore_lock);
147         if (psinfo) {
148                 spin_unlock(&pstore_lock);
149                 return -EBUSY;
150         }
151
152         if (backend && strcmp(backend, psi->name)) {
153                 spin_unlock(&pstore_lock);
154                 return -EINVAL;
155         }
156
157         psinfo = psi;
158         spin_unlock(&pstore_lock);
159
160         if (owner && !try_module_get(owner)) {
161                 psinfo = NULL;
162                 return -EINVAL;
163         }
164
165         if (pstore_is_mounted())
166                 pstore_get_records(0);
167
168         kmsg_dump_register(&pstore_dumper);
169
170         pstore_timer.expires = jiffies + PSTORE_INTERVAL;
171         add_timer(&pstore_timer);
172
173         return 0;
174 }
175 EXPORT_SYMBOL_GPL(pstore_register);
176
177 /*
178  * Read all the records from the persistent store. Create
179  * files in our filesystem.  Don't warn about -EEXIST errors
180  * when we are re-scanning the backing store looking to add new
181  * error records.
182  */
183 void pstore_get_records(int quiet)
184 {
185         struct pstore_info *psi = psinfo;
186         ssize_t                 size;
187         u64                     id;
188         enum pstore_type_id     type;
189         struct timespec         time;
190         int                     failed = 0, rc;
191
192         if (!psi)
193                 return;
194
195         mutex_lock(&psinfo->buf_mutex);
196         rc = psi->open(psi);
197         if (rc)
198                 goto out;
199
200         while ((size = psi->read(&id, &type, &time, psi)) > 0) {
201                 rc = pstore_mkfile(type, psi->name, id, psi->buf, (size_t)size,
202                                   time, psi);
203                 if (rc && (rc != -EEXIST || !quiet))
204                         failed++;
205         }
206         psi->close(psi);
207 out:
208         mutex_unlock(&psinfo->buf_mutex);
209
210         if (failed)
211                 printk(KERN_WARNING "pstore: failed to load %d record(s) from '%s'\n",
212                        failed, psi->name);
213 }
214
215 static void pstore_dowork(struct work_struct *work)
216 {
217         pstore_get_records(1);
218 }
219
220 static void pstore_timefunc(unsigned long dummy)
221 {
222         if (pstore_new_entry) {
223                 pstore_new_entry = 0;
224                 schedule_work(&pstore_work);
225         }
226
227         mod_timer(&pstore_timer, jiffies + PSTORE_INTERVAL);
228 }
229
230 /*
231  * Call platform driver to write a record to the
232  * persistent store.
233  */
234 int pstore_write(enum pstore_type_id type, char *buf, size_t size)
235 {
236         u64     id;
237
238         if (!psinfo)
239                 return -ENODEV;
240
241         if (size > psinfo->bufsize)
242                 return -EFBIG;
243
244         mutex_lock(&psinfo->buf_mutex);
245         memcpy(psinfo->buf, buf, size);
246         id = psinfo->write(type, 0, size, psinfo);
247         if (pstore_is_mounted())
248                 pstore_mkfile(PSTORE_TYPE_DMESG, psinfo->name, id, psinfo->buf,
249                               size, CURRENT_TIME, psinfo);
250         mutex_unlock(&psinfo->buf_mutex);
251
252         return 0;
253 }
254 EXPORT_SYMBOL_GPL(pstore_write);
255
256 module_param(backend, charp, 0444);
257 MODULE_PARM_DESC(backend, "Pstore backend to use");