blob: 34ca3141eb0ac13223126c282b0fed3a580be639 [file] [log] [blame]
Tony Luckca01d6d2010-12-28 14:25:21 -08001/*
2 * Persistent Storage - platform driver interface parts.
3 *
Anton Vorontsovf29e5952012-05-26 06:20:19 -07004 * Copyright (C) 2007-2008 Google, Inc.
Tony Luckca01d6d2010-12-28 14:25:21 -08005 * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include <linux/atomic.h>
22#include <linux/types.h>
23#include <linux/errno.h>
24#include <linux/init.h>
25#include <linux/kmsg_dump.h>
Anton Vorontsovf29e5952012-05-26 06:20:19 -070026#include <linux/console.h>
Tony Luckca01d6d2010-12-28 14:25:21 -080027#include <linux/module.h>
28#include <linux/pstore.h>
29#include <linux/string.h>
Luck, Tony6dda9262011-08-11 15:14:39 -070030#include <linux/timer.h>
Tony Luckca01d6d2010-12-28 14:25:21 -080031#include <linux/slab.h>
32#include <linux/uaccess.h>
Don Zickusabd4d552011-08-12 10:54:51 -070033#include <linux/hardirq.h>
Anton Vorontsova3f5f072012-05-26 06:20:28 -070034#include <linux/jiffies.h>
Luck, Tony6dda9262011-08-11 15:14:39 -070035#include <linux/workqueue.h>
Tony Luckca01d6d2010-12-28 14:25:21 -080036
37#include "internal.h"
38
39/*
Luck, Tony6dda9262011-08-11 15:14:39 -070040 * We defer making "oops" entries appear in pstore - see
41 * whether the system is actually still running well enough
42 * to let someone see the entry
43 */
Anton Vorontsova3f5f072012-05-26 06:20:28 -070044static int pstore_update_ms = 60000;
45module_param_named(update_ms, pstore_update_ms, int, 0600);
46MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content "
47 "(default is 60000; -1 means runtime updates are disabled)");
Luck, Tony6dda9262011-08-11 15:14:39 -070048
49static int pstore_new_entry;
50
51static void pstore_timefunc(unsigned long);
52static DEFINE_TIMER(pstore_timer, pstore_timefunc, 0, 0);
53
54static void pstore_dowork(struct work_struct *);
55static DECLARE_WORK(pstore_work, pstore_dowork);
56
57/*
Tony Luckca01d6d2010-12-28 14:25:21 -080058 * pstore_lock just protects "psinfo" during
59 * calls to pstore_register()
60 */
61static DEFINE_SPINLOCK(pstore_lock);
62static struct pstore_info *psinfo;
63
Matthew Garrettdee28e72011-07-21 16:57:55 -040064static char *backend;
65
Luck, Tony366f7e72011-03-18 15:33:43 -070066/* How much of the console log to snapshot */
Tony Luckca01d6d2010-12-28 14:25:21 -080067static unsigned long kmsg_bytes = 10240;
68
Luck, Tony366f7e72011-03-18 15:33:43 -070069void pstore_set_kmsg_bytes(int bytes)
Tony Luckca01d6d2010-12-28 14:25:21 -080070{
Luck, Tony366f7e72011-03-18 15:33:43 -070071 kmsg_bytes = bytes;
Tony Luckca01d6d2010-12-28 14:25:21 -080072}
73
Tony Luckca01d6d2010-12-28 14:25:21 -080074/* Tag each group of saved records with a sequence number */
75static int oopscount;
76
Seiji Aguchi381b8722012-03-16 15:36:59 -070077static const char *get_reason_str(enum kmsg_dump_reason reason)
78{
79 switch (reason) {
80 case KMSG_DUMP_PANIC:
81 return "Panic";
82 case KMSG_DUMP_OOPS:
83 return "Oops";
84 case KMSG_DUMP_EMERG:
85 return "Emergency";
86 case KMSG_DUMP_RESTART:
87 return "Restart";
88 case KMSG_DUMP_HALT:
89 return "Halt";
90 case KMSG_DUMP_POWEROFF:
91 return "Poweroff";
92 default:
93 return "Unknown";
94 }
95}
Tony Luck9f6af272011-03-22 16:01:49 -070096
Tony Luckca01d6d2010-12-28 14:25:21 -080097/*
98 * callback from kmsg_dump. (s2,l2) has the most recently
99 * written bytes, older bytes are in (s1,l1). Save as much
100 * as we can from the end of the buffer.
101 */
102static void pstore_dump(struct kmsg_dumper *dumper,
103 enum kmsg_dump_reason reason,
104 const char *s1, unsigned long l1,
105 const char *s2, unsigned long l2)
106{
107 unsigned long s1_start, s2_start;
108 unsigned long l1_cpy, l2_cpy;
109 unsigned long size, total = 0;
Seiji Aguchi381b8722012-03-16 15:36:59 -0700110 char *dst;
111 const char *why;
Tony Luckca01d6d2010-12-28 14:25:21 -0800112 u64 id;
Chen Gongb238b8f2011-10-12 09:17:24 -0700113 int hsize, ret;
Matthew Garrettb94fdd02011-07-21 16:57:54 -0400114 unsigned int part = 1;
Don Zickusabd4d552011-08-12 10:54:51 -0700115 unsigned long flags = 0;
116 int is_locked = 0;
Tony Luckca01d6d2010-12-28 14:25:21 -0800117
Seiji Aguchi381b8722012-03-16 15:36:59 -0700118 why = get_reason_str(reason);
Tony Luck9f6af272011-03-22 16:01:49 -0700119
Don Zickusabd4d552011-08-12 10:54:51 -0700120 if (in_nmi()) {
121 is_locked = spin_trylock(&psinfo->buf_lock);
122 if (!is_locked)
123 pr_err("pstore dump routine blocked in NMI, may corrupt error record\n");
124 } else
125 spin_lock_irqsave(&psinfo->buf_lock, flags);
Tony Luckca01d6d2010-12-28 14:25:21 -0800126 oopscount++;
127 while (total < kmsg_bytes) {
128 dst = psinfo->buf;
Matthew Garrett56280682011-07-21 16:57:53 -0400129 hsize = sprintf(dst, "%s#%d Part%d\n", why, oopscount, part);
Tony Luckca01d6d2010-12-28 14:25:21 -0800130 size = psinfo->bufsize - hsize;
131 dst += hsize;
132
133 l2_cpy = min(l2, size);
134 l1_cpy = min(l1, size - l2_cpy);
135
136 if (l1_cpy + l2_cpy == 0)
137 break;
138
139 s2_start = l2 - l2_cpy;
140 s1_start = l1 - l1_cpy;
141
142 memcpy(dst, s1 + s1_start, l1_cpy);
143 memcpy(dst + l1_cpy, s2 + s2_start, l2_cpy);
144
Kees Cook3d6d8d22011-11-17 13:13:29 -0800145 ret = psinfo->write(PSTORE_TYPE_DMESG, reason, &id, part,
Matthew Garrett56280682011-07-21 16:57:53 -0400146 hsize + l1_cpy + l2_cpy, psinfo);
Chen Gongb238b8f2011-10-12 09:17:24 -0700147 if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted())
Luck, Tony6dda9262011-08-11 15:14:39 -0700148 pstore_new_entry = 1;
Tony Luckca01d6d2010-12-28 14:25:21 -0800149 l1 -= l1_cpy;
150 l2 -= l2_cpy;
151 total += l1_cpy + l2_cpy;
Matthew Garrett56280682011-07-21 16:57:53 -0400152 part++;
Tony Luckca01d6d2010-12-28 14:25:21 -0800153 }
Don Zickusabd4d552011-08-12 10:54:51 -0700154 if (in_nmi()) {
155 if (is_locked)
156 spin_unlock(&psinfo->buf_lock);
157 } else
158 spin_unlock_irqrestore(&psinfo->buf_lock, flags);
Tony Luckca01d6d2010-12-28 14:25:21 -0800159}
160
161static struct kmsg_dumper pstore_dumper = {
162 .dump = pstore_dump,
163};
164
Anton Vorontsovf29e5952012-05-26 06:20:19 -0700165#ifdef CONFIG_PSTORE_CONSOLE
166static void pstore_console_write(struct console *con, const char *s, unsigned c)
167{
168 const char *e = s + c;
169
170 while (s < e) {
171 unsigned long flags;
172
173 if (c > psinfo->bufsize)
174 c = psinfo->bufsize;
175 spin_lock_irqsave(&psinfo->buf_lock, flags);
176 memcpy(psinfo->buf, s, c);
177 psinfo->write(PSTORE_TYPE_CONSOLE, 0, NULL, 0, c, psinfo);
178 spin_unlock_irqrestore(&psinfo->buf_lock, flags);
179 s += c;
180 c = e - s;
181 }
182}
183
184static struct console pstore_console = {
185 .name = "pstore",
186 .write = pstore_console_write,
187 .flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME,
188 .index = -1,
189};
190
191static void pstore_register_console(void)
192{
193 register_console(&pstore_console);
194}
195#else
196static void pstore_register_console(void) {}
197#endif
198
Tony Luckca01d6d2010-12-28 14:25:21 -0800199/*
200 * platform specific persistent storage driver registers with
201 * us here. If pstore is already mounted, call the platform
202 * read function right away to populate the file system. If not
203 * then the pstore mount code will call us later to fill out
204 * the file system.
205 *
206 * Register with kmsg_dump to save last part of console log on panic.
207 */
208int pstore_register(struct pstore_info *psi)
209{
210 struct module *owner = psi->owner;
211
212 spin_lock(&pstore_lock);
213 if (psinfo) {
214 spin_unlock(&pstore_lock);
215 return -EBUSY;
216 }
Matthew Garrettdee28e72011-07-21 16:57:55 -0400217
218 if (backend && strcmp(backend, psi->name)) {
219 spin_unlock(&pstore_lock);
220 return -EINVAL;
221 }
222
Tony Luckca01d6d2010-12-28 14:25:21 -0800223 psinfo = psi;
Kees Cookf6f82852011-11-17 12:58:07 -0800224 mutex_init(&psinfo->read_mutex);
Tony Luckca01d6d2010-12-28 14:25:21 -0800225 spin_unlock(&pstore_lock);
226
227 if (owner && !try_module_get(owner)) {
228 psinfo = NULL;
229 return -EINVAL;
230 }
231
232 if (pstore_is_mounted())
Luck, Tony6dda9262011-08-11 15:14:39 -0700233 pstore_get_records(0);
Tony Luckca01d6d2010-12-28 14:25:21 -0800234
235 kmsg_dump_register(&pstore_dumper);
Anton Vorontsovf29e5952012-05-26 06:20:19 -0700236 pstore_register_console();
Tony Luckca01d6d2010-12-28 14:25:21 -0800237
Anton Vorontsova3f5f072012-05-26 06:20:28 -0700238 if (pstore_update_ms >= 0) {
239 pstore_timer.expires = jiffies +
240 msecs_to_jiffies(pstore_update_ms);
241 add_timer(&pstore_timer);
242 }
Luck, Tony6dda9262011-08-11 15:14:39 -0700243
Tony Luckca01d6d2010-12-28 14:25:21 -0800244 return 0;
245}
246EXPORT_SYMBOL_GPL(pstore_register);
247
248/*
Luck, Tony6dda9262011-08-11 15:14:39 -0700249 * Read all the records from the persistent store. Create
250 * files in our filesystem. Don't warn about -EEXIST errors
251 * when we are re-scanning the backing store looking to add new
252 * error records.
Tony Luckca01d6d2010-12-28 14:25:21 -0800253 */
Luck, Tony6dda9262011-08-11 15:14:39 -0700254void pstore_get_records(int quiet)
Tony Luckca01d6d2010-12-28 14:25:21 -0800255{
256 struct pstore_info *psi = psinfo;
Kees Cookf6f82852011-11-17 12:58:07 -0800257 char *buf = NULL;
Chen Gong8d38d742011-05-16 10:58:57 -0700258 ssize_t size;
Tony Luckca01d6d2010-12-28 14:25:21 -0800259 u64 id;
260 enum pstore_type_id type;
261 struct timespec time;
Chen Gong06cf91b2011-05-16 11:00:27 -0700262 int failed = 0, rc;
Tony Luckca01d6d2010-12-28 14:25:21 -0800263
264 if (!psi)
265 return;
266
Kees Cookf6f82852011-11-17 12:58:07 -0800267 mutex_lock(&psi->read_mutex);
Kees Cook2174f6d2011-11-18 13:49:00 -0800268 if (psi->open && psi->open(psi))
Chen Gong06cf91b2011-05-16 11:00:27 -0700269 goto out;
270
Kees Cookf6f82852011-11-17 12:58:07 -0800271 while ((size = psi->read(&id, &type, &time, &buf, psi)) > 0) {
272 rc = pstore_mkfile(type, psi->name, id, buf, (size_t)size,
Luck, Tony6dda9262011-08-11 15:14:39 -0700273 time, psi);
Kees Cookf6f82852011-11-17 12:58:07 -0800274 kfree(buf);
275 buf = NULL;
Luck, Tony6dda9262011-08-11 15:14:39 -0700276 if (rc && (rc != -EEXIST || !quiet))
Tony Luckca01d6d2010-12-28 14:25:21 -0800277 failed++;
278 }
Kees Cook2174f6d2011-11-18 13:49:00 -0800279 if (psi->close)
280 psi->close(psi);
Chen Gong06cf91b2011-05-16 11:00:27 -0700281out:
Kees Cookf6f82852011-11-17 12:58:07 -0800282 mutex_unlock(&psi->read_mutex);
Tony Luckca01d6d2010-12-28 14:25:21 -0800283
284 if (failed)
285 printk(KERN_WARNING "pstore: failed to load %d record(s) from '%s'\n",
286 failed, psi->name);
287}
288
Luck, Tony6dda9262011-08-11 15:14:39 -0700289static void pstore_dowork(struct work_struct *work)
290{
291 pstore_get_records(1);
292}
293
294static void pstore_timefunc(unsigned long dummy)
295{
296 if (pstore_new_entry) {
297 pstore_new_entry = 0;
298 schedule_work(&pstore_work);
299 }
300
Anton Vorontsova3f5f072012-05-26 06:20:28 -0700301 mod_timer(&pstore_timer, jiffies + msecs_to_jiffies(pstore_update_ms));
Luck, Tony6dda9262011-08-11 15:14:39 -0700302}
303
Matthew Garrettdee28e72011-07-21 16:57:55 -0400304module_param(backend, charp, 0444);
305MODULE_PARM_DESC(backend, "Pstore backend to use");