xref: /openbmc/linux/fs/pstore/platform.c (revision 521f7288a8126a8ec28e3ab623aacf0590684b80)
1ca01d6ddSTony Luck /*
2ca01d6ddSTony Luck  * Persistent Storage - platform driver interface parts.
3ca01d6ddSTony Luck  *
4f29e5956SAnton Vorontsov  * Copyright (C) 2007-2008 Google, Inc.
5ca01d6ddSTony Luck  * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com>
6ca01d6ddSTony Luck  *
7ca01d6ddSTony Luck  *  This program is free software; you can redistribute it and/or modify
8ca01d6ddSTony Luck  *  it under the terms of the GNU General Public License version 2 as
9ca01d6ddSTony Luck  *  published by the Free Software Foundation.
10ca01d6ddSTony Luck  *
11ca01d6ddSTony Luck  *  This program is distributed in the hope that it will be useful,
12ca01d6ddSTony Luck  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13ca01d6ddSTony Luck  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14ca01d6ddSTony Luck  *  GNU General Public License for more details.
15ca01d6ddSTony Luck  *
16ca01d6ddSTony Luck  *  You should have received a copy of the GNU General Public License
17ca01d6ddSTony Luck  *  along with this program; if not, write to the Free Software
18ca01d6ddSTony Luck  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19ca01d6ddSTony Luck  */
20ca01d6ddSTony Luck 
21ca01d6ddSTony Luck #include <linux/atomic.h>
22ca01d6ddSTony Luck #include <linux/types.h>
23ca01d6ddSTony Luck #include <linux/errno.h>
24ca01d6ddSTony Luck #include <linux/init.h>
25ca01d6ddSTony Luck #include <linux/kmsg_dump.h>
26f29e5956SAnton Vorontsov #include <linux/console.h>
27ca01d6ddSTony Luck #include <linux/module.h>
28ca01d6ddSTony Luck #include <linux/pstore.h>
29ca01d6ddSTony Luck #include <linux/string.h>
306dda9266SLuck, Tony #include <linux/timer.h>
31ca01d6ddSTony Luck #include <linux/slab.h>
32ca01d6ddSTony Luck #include <linux/uaccess.h>
33abd4d558SDon Zickus #include <linux/hardirq.h>
34a3f5f075SAnton Vorontsov #include <linux/jiffies.h>
356dda9266SLuck, Tony #include <linux/workqueue.h>
36ca01d6ddSTony Luck 
37ca01d6ddSTony Luck #include "internal.h"
38ca01d6ddSTony Luck 
39ca01d6ddSTony Luck /*
406dda9266SLuck, Tony  * We defer making "oops" entries appear in pstore - see
416dda9266SLuck, Tony  * whether the system is actually still running well enough
426dda9266SLuck, Tony  * to let someone see the entry
436dda9266SLuck, Tony  */
44*521f7288SAnton Vorontsov static int pstore_update_ms = -1;
45a3f5f075SAnton Vorontsov module_param_named(update_ms, pstore_update_ms, int, 0600);
46a3f5f075SAnton Vorontsov MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content "
47*521f7288SAnton Vorontsov 		 "(default is -1, which means runtime updates are disabled; "
48*521f7288SAnton Vorontsov 		 "enabling this option is not safe, it may lead to further "
49*521f7288SAnton Vorontsov 		 "corruption on Oopses)");
506dda9266SLuck, Tony 
516dda9266SLuck, Tony static int pstore_new_entry;
526dda9266SLuck, Tony 
536dda9266SLuck, Tony static void pstore_timefunc(unsigned long);
546dda9266SLuck, Tony static DEFINE_TIMER(pstore_timer, pstore_timefunc, 0, 0);
556dda9266SLuck, Tony 
566dda9266SLuck, Tony static void pstore_dowork(struct work_struct *);
576dda9266SLuck, Tony static DECLARE_WORK(pstore_work, pstore_dowork);
586dda9266SLuck, Tony 
596dda9266SLuck, Tony /*
60ca01d6ddSTony Luck  * pstore_lock just protects "psinfo" during
61ca01d6ddSTony Luck  * calls to pstore_register()
62ca01d6ddSTony Luck  */
63ca01d6ddSTony Luck static DEFINE_SPINLOCK(pstore_lock);
64ca01d6ddSTony Luck static struct pstore_info *psinfo;
65ca01d6ddSTony Luck 
66dee28e72SMatthew Garrett static char *backend;
67dee28e72SMatthew Garrett 
68366f7e7aSLuck, Tony /* How much of the console log to snapshot */
69ca01d6ddSTony Luck static unsigned long kmsg_bytes = 10240;
70ca01d6ddSTony Luck 
71366f7e7aSLuck, Tony void pstore_set_kmsg_bytes(int bytes)
72ca01d6ddSTony Luck {
73366f7e7aSLuck, Tony 	kmsg_bytes = bytes;
74ca01d6ddSTony Luck }
75ca01d6ddSTony Luck 
76ca01d6ddSTony Luck /* Tag each group of saved records with a sequence number */
77ca01d6ddSTony Luck static int	oopscount;
78ca01d6ddSTony Luck 
79381b872cSSeiji Aguchi static const char *get_reason_str(enum kmsg_dump_reason reason)
80381b872cSSeiji Aguchi {
81381b872cSSeiji Aguchi 	switch (reason) {
82381b872cSSeiji Aguchi 	case KMSG_DUMP_PANIC:
83381b872cSSeiji Aguchi 		return "Panic";
84381b872cSSeiji Aguchi 	case KMSG_DUMP_OOPS:
85381b872cSSeiji Aguchi 		return "Oops";
86381b872cSSeiji Aguchi 	case KMSG_DUMP_EMERG:
87381b872cSSeiji Aguchi 		return "Emergency";
88381b872cSSeiji Aguchi 	case KMSG_DUMP_RESTART:
89381b872cSSeiji Aguchi 		return "Restart";
90381b872cSSeiji Aguchi 	case KMSG_DUMP_HALT:
91381b872cSSeiji Aguchi 		return "Halt";
92381b872cSSeiji Aguchi 	case KMSG_DUMP_POWEROFF:
93381b872cSSeiji Aguchi 		return "Poweroff";
94381b872cSSeiji Aguchi 	default:
95381b872cSSeiji Aguchi 		return "Unknown";
96381b872cSSeiji Aguchi 	}
97381b872cSSeiji Aguchi }
989f6af27fSTony Luck 
99ca01d6ddSTony Luck /*
100ca01d6ddSTony Luck  * callback from kmsg_dump. (s2,l2) has the most recently
101ca01d6ddSTony Luck  * written bytes, older bytes are in (s1,l1). Save as much
102ca01d6ddSTony Luck  * as we can from the end of the buffer.
103ca01d6ddSTony Luck  */
104ca01d6ddSTony Luck static void pstore_dump(struct kmsg_dumper *dumper,
105ca01d6ddSTony Luck 	    enum kmsg_dump_reason reason,
106ca01d6ddSTony Luck 	    const char *s1, unsigned long l1,
107ca01d6ddSTony Luck 	    const char *s2, unsigned long l2)
108ca01d6ddSTony Luck {
109ca01d6ddSTony Luck 	unsigned long	s1_start, s2_start;
110ca01d6ddSTony Luck 	unsigned long	l1_cpy, l2_cpy;
111ca01d6ddSTony Luck 	unsigned long	size, total = 0;
112381b872cSSeiji Aguchi 	char		*dst;
113381b872cSSeiji Aguchi 	const char	*why;
114ca01d6ddSTony Luck 	u64		id;
115b238b8faSChen Gong 	int		hsize, ret;
116b94fdd07SMatthew Garrett 	unsigned int	part = 1;
117abd4d558SDon Zickus 	unsigned long	flags = 0;
118abd4d558SDon Zickus 	int		is_locked = 0;
119ca01d6ddSTony Luck 
120381b872cSSeiji Aguchi 	why = get_reason_str(reason);
1219f6af27fSTony Luck 
122abd4d558SDon Zickus 	if (in_nmi()) {
123abd4d558SDon Zickus 		is_locked = spin_trylock(&psinfo->buf_lock);
124abd4d558SDon Zickus 		if (!is_locked)
125abd4d558SDon Zickus 			pr_err("pstore dump routine blocked in NMI, may corrupt error record\n");
126abd4d558SDon Zickus 	} else
127abd4d558SDon Zickus 		spin_lock_irqsave(&psinfo->buf_lock, flags);
128ca01d6ddSTony Luck 	oopscount++;
129ca01d6ddSTony Luck 	while (total < kmsg_bytes) {
130ca01d6ddSTony Luck 		dst = psinfo->buf;
13156280682SMatthew Garrett 		hsize = sprintf(dst, "%s#%d Part%d\n", why, oopscount, part);
132ca01d6ddSTony Luck 		size = psinfo->bufsize - hsize;
133ca01d6ddSTony Luck 		dst += hsize;
134ca01d6ddSTony Luck 
135ca01d6ddSTony Luck 		l2_cpy = min(l2, size);
136ca01d6ddSTony Luck 		l1_cpy = min(l1, size - l2_cpy);
137ca01d6ddSTony Luck 
138ca01d6ddSTony Luck 		if (l1_cpy + l2_cpy == 0)
139ca01d6ddSTony Luck 			break;
140ca01d6ddSTony Luck 
141ca01d6ddSTony Luck 		s2_start = l2 - l2_cpy;
142ca01d6ddSTony Luck 		s1_start = l1 - l1_cpy;
143ca01d6ddSTony Luck 
144ca01d6ddSTony Luck 		memcpy(dst, s1 + s1_start, l1_cpy);
145ca01d6ddSTony Luck 		memcpy(dst + l1_cpy, s2 + s2_start, l2_cpy);
146ca01d6ddSTony Luck 
1473d6d8d20SKees Cook 		ret = psinfo->write(PSTORE_TYPE_DMESG, reason, &id, part,
14856280682SMatthew Garrett 				   hsize + l1_cpy + l2_cpy, psinfo);
149b238b8faSChen Gong 		if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted())
1506dda9266SLuck, Tony 			pstore_new_entry = 1;
151ca01d6ddSTony Luck 		l1 -= l1_cpy;
152ca01d6ddSTony Luck 		l2 -= l2_cpy;
153ca01d6ddSTony Luck 		total += l1_cpy + l2_cpy;
15456280682SMatthew Garrett 		part++;
155ca01d6ddSTony Luck 	}
156abd4d558SDon Zickus 	if (in_nmi()) {
157abd4d558SDon Zickus 		if (is_locked)
158abd4d558SDon Zickus 			spin_unlock(&psinfo->buf_lock);
159abd4d558SDon Zickus 	} else
160abd4d558SDon Zickus 		spin_unlock_irqrestore(&psinfo->buf_lock, flags);
161ca01d6ddSTony Luck }
162ca01d6ddSTony Luck 
163ca01d6ddSTony Luck static struct kmsg_dumper pstore_dumper = {
164ca01d6ddSTony Luck 	.dump = pstore_dump,
165ca01d6ddSTony Luck };
166ca01d6ddSTony Luck 
167f29e5956SAnton Vorontsov #ifdef CONFIG_PSTORE_CONSOLE
168f29e5956SAnton Vorontsov static void pstore_console_write(struct console *con, const char *s, unsigned c)
169f29e5956SAnton Vorontsov {
170f29e5956SAnton Vorontsov 	const char *e = s + c;
171f29e5956SAnton Vorontsov 
172f29e5956SAnton Vorontsov 	while (s < e) {
173f29e5956SAnton Vorontsov 		unsigned long flags;
174f29e5956SAnton Vorontsov 
175f29e5956SAnton Vorontsov 		if (c > psinfo->bufsize)
176f29e5956SAnton Vorontsov 			c = psinfo->bufsize;
177f29e5956SAnton Vorontsov 		spin_lock_irqsave(&psinfo->buf_lock, flags);
178f29e5956SAnton Vorontsov 		memcpy(psinfo->buf, s, c);
179f29e5956SAnton Vorontsov 		psinfo->write(PSTORE_TYPE_CONSOLE, 0, NULL, 0, c, psinfo);
180f29e5956SAnton Vorontsov 		spin_unlock_irqrestore(&psinfo->buf_lock, flags);
181f29e5956SAnton Vorontsov 		s += c;
182f29e5956SAnton Vorontsov 		c = e - s;
183f29e5956SAnton Vorontsov 	}
184f29e5956SAnton Vorontsov }
185f29e5956SAnton Vorontsov 
186f29e5956SAnton Vorontsov static struct console pstore_console = {
187f29e5956SAnton Vorontsov 	.name	= "pstore",
188f29e5956SAnton Vorontsov 	.write	= pstore_console_write,
189f29e5956SAnton Vorontsov 	.flags	= CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME,
190f29e5956SAnton Vorontsov 	.index	= -1,
191f29e5956SAnton Vorontsov };
192f29e5956SAnton Vorontsov 
193f29e5956SAnton Vorontsov static void pstore_register_console(void)
194f29e5956SAnton Vorontsov {
195f29e5956SAnton Vorontsov 	register_console(&pstore_console);
196f29e5956SAnton Vorontsov }
197f29e5956SAnton Vorontsov #else
198f29e5956SAnton Vorontsov static void pstore_register_console(void) {}
199f29e5956SAnton Vorontsov #endif
200f29e5956SAnton Vorontsov 
201ca01d6ddSTony Luck /*
202ca01d6ddSTony Luck  * platform specific persistent storage driver registers with
203ca01d6ddSTony Luck  * us here. If pstore is already mounted, call the platform
204ca01d6ddSTony Luck  * read function right away to populate the file system. If not
205ca01d6ddSTony Luck  * then the pstore mount code will call us later to fill out
206ca01d6ddSTony Luck  * the file system.
207ca01d6ddSTony Luck  *
208ca01d6ddSTony Luck  * Register with kmsg_dump to save last part of console log on panic.
209ca01d6ddSTony Luck  */
210ca01d6ddSTony Luck int pstore_register(struct pstore_info *psi)
211ca01d6ddSTony Luck {
212ca01d6ddSTony Luck 	struct module *owner = psi->owner;
213ca01d6ddSTony Luck 
214ca01d6ddSTony Luck 	spin_lock(&pstore_lock);
215ca01d6ddSTony Luck 	if (psinfo) {
216ca01d6ddSTony Luck 		spin_unlock(&pstore_lock);
217ca01d6ddSTony Luck 		return -EBUSY;
218ca01d6ddSTony Luck 	}
219dee28e72SMatthew Garrett 
220dee28e72SMatthew Garrett 	if (backend && strcmp(backend, psi->name)) {
221dee28e72SMatthew Garrett 		spin_unlock(&pstore_lock);
222dee28e72SMatthew Garrett 		return -EINVAL;
223dee28e72SMatthew Garrett 	}
224dee28e72SMatthew Garrett 
225ca01d6ddSTony Luck 	psinfo = psi;
226f6f82851SKees Cook 	mutex_init(&psinfo->read_mutex);
227ca01d6ddSTony Luck 	spin_unlock(&pstore_lock);
228ca01d6ddSTony Luck 
229ca01d6ddSTony Luck 	if (owner && !try_module_get(owner)) {
230ca01d6ddSTony Luck 		psinfo = NULL;
231ca01d6ddSTony Luck 		return -EINVAL;
232ca01d6ddSTony Luck 	}
233ca01d6ddSTony Luck 
234ca01d6ddSTony Luck 	if (pstore_is_mounted())
2356dda9266SLuck, Tony 		pstore_get_records(0);
236ca01d6ddSTony Luck 
237ca01d6ddSTony Luck 	kmsg_dump_register(&pstore_dumper);
238f29e5956SAnton Vorontsov 	pstore_register_console();
239ca01d6ddSTony Luck 
240a3f5f075SAnton Vorontsov 	if (pstore_update_ms >= 0) {
241a3f5f075SAnton Vorontsov 		pstore_timer.expires = jiffies +
242a3f5f075SAnton Vorontsov 			msecs_to_jiffies(pstore_update_ms);
2436dda9266SLuck, Tony 		add_timer(&pstore_timer);
244a3f5f075SAnton Vorontsov 	}
2456dda9266SLuck, Tony 
246ca01d6ddSTony Luck 	return 0;
247ca01d6ddSTony Luck }
248ca01d6ddSTony Luck EXPORT_SYMBOL_GPL(pstore_register);
249ca01d6ddSTony Luck 
250ca01d6ddSTony Luck /*
2516dda9266SLuck, Tony  * Read all the records from the persistent store. Create
2526dda9266SLuck, Tony  * files in our filesystem.  Don't warn about -EEXIST errors
2536dda9266SLuck, Tony  * when we are re-scanning the backing store looking to add new
2546dda9266SLuck, Tony  * error records.
255ca01d6ddSTony Luck  */
2566dda9266SLuck, Tony void pstore_get_records(int quiet)
257ca01d6ddSTony Luck {
258ca01d6ddSTony Luck 	struct pstore_info *psi = psinfo;
259f6f82851SKees Cook 	char			*buf = NULL;
2608d38d74bSChen Gong 	ssize_t			size;
261ca01d6ddSTony Luck 	u64			id;
262ca01d6ddSTony Luck 	enum pstore_type_id	type;
263ca01d6ddSTony Luck 	struct timespec		time;
26406cf91b4SChen Gong 	int			failed = 0, rc;
265ca01d6ddSTony Luck 
266ca01d6ddSTony Luck 	if (!psi)
267ca01d6ddSTony Luck 		return;
268ca01d6ddSTony Luck 
269f6f82851SKees Cook 	mutex_lock(&psi->read_mutex);
2702174f6dfSKees Cook 	if (psi->open && psi->open(psi))
27106cf91b4SChen Gong 		goto out;
27206cf91b4SChen Gong 
273f6f82851SKees Cook 	while ((size = psi->read(&id, &type, &time, &buf, psi)) > 0) {
274f6f82851SKees Cook 		rc = pstore_mkfile(type, psi->name, id, buf, (size_t)size,
2756dda9266SLuck, Tony 				  time, psi);
276f6f82851SKees Cook 		kfree(buf);
277f6f82851SKees Cook 		buf = NULL;
2786dda9266SLuck, Tony 		if (rc && (rc != -EEXIST || !quiet))
279ca01d6ddSTony Luck 			failed++;
280ca01d6ddSTony Luck 	}
2812174f6dfSKees Cook 	if (psi->close)
28206cf91b4SChen Gong 		psi->close(psi);
28306cf91b4SChen Gong out:
284f6f82851SKees Cook 	mutex_unlock(&psi->read_mutex);
285ca01d6ddSTony Luck 
286ca01d6ddSTony Luck 	if (failed)
287ca01d6ddSTony Luck 		printk(KERN_WARNING "pstore: failed to load %d record(s) from '%s'\n",
288ca01d6ddSTony Luck 		       failed, psi->name);
289ca01d6ddSTony Luck }
290ca01d6ddSTony Luck 
2916dda9266SLuck, Tony static void pstore_dowork(struct work_struct *work)
2926dda9266SLuck, Tony {
2936dda9266SLuck, Tony 	pstore_get_records(1);
2946dda9266SLuck, Tony }
2956dda9266SLuck, Tony 
2966dda9266SLuck, Tony static void pstore_timefunc(unsigned long dummy)
2976dda9266SLuck, Tony {
2986dda9266SLuck, Tony 	if (pstore_new_entry) {
2996dda9266SLuck, Tony 		pstore_new_entry = 0;
3006dda9266SLuck, Tony 		schedule_work(&pstore_work);
3016dda9266SLuck, Tony 	}
3026dda9266SLuck, Tony 
303a3f5f075SAnton Vorontsov 	mod_timer(&pstore_timer, jiffies + msecs_to_jiffies(pstore_update_ms));
3046dda9266SLuck, Tony }
3056dda9266SLuck, Tony 
306dee28e72SMatthew Garrett module_param(backend, charp, 0444);
307dee28e72SMatthew Garrett MODULE_PARM_DESC(backend, "Pstore backend to use");
308