xref: /openbmc/linux/fs/pstore/platform.c (revision 2174f6df7891fa331800beb72634c969f017900b)
1ca01d6ddSTony Luck /*
2ca01d6ddSTony Luck  * Persistent Storage - platform driver interface parts.
3ca01d6ddSTony Luck  *
4ca01d6ddSTony Luck  * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com>
5ca01d6ddSTony Luck  *
6ca01d6ddSTony Luck  *  This program is free software; you can redistribute it and/or modify
7ca01d6ddSTony Luck  *  it under the terms of the GNU General Public License version 2 as
8ca01d6ddSTony Luck  *  published by the Free Software Foundation.
9ca01d6ddSTony Luck  *
10ca01d6ddSTony Luck  *  This program is distributed in the hope that it will be useful,
11ca01d6ddSTony Luck  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12ca01d6ddSTony Luck  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13ca01d6ddSTony Luck  *  GNU General Public License for more details.
14ca01d6ddSTony Luck  *
15ca01d6ddSTony Luck  *  You should have received a copy of the GNU General Public License
16ca01d6ddSTony Luck  *  along with this program; if not, write to the Free Software
17ca01d6ddSTony Luck  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18ca01d6ddSTony Luck  */
19ca01d6ddSTony Luck 
20ca01d6ddSTony Luck #include <linux/atomic.h>
21ca01d6ddSTony Luck #include <linux/types.h>
22ca01d6ddSTony Luck #include <linux/errno.h>
23ca01d6ddSTony Luck #include <linux/init.h>
24ca01d6ddSTony Luck #include <linux/kmsg_dump.h>
25ca01d6ddSTony Luck #include <linux/module.h>
26ca01d6ddSTony Luck #include <linux/pstore.h>
27ca01d6ddSTony Luck #include <linux/string.h>
286dda9266SLuck, Tony #include <linux/timer.h>
29ca01d6ddSTony Luck #include <linux/slab.h>
30ca01d6ddSTony Luck #include <linux/uaccess.h>
31abd4d558SDon Zickus #include <linux/hardirq.h>
326dda9266SLuck, Tony #include <linux/workqueue.h>
33ca01d6ddSTony Luck 
34ca01d6ddSTony Luck #include "internal.h"
35ca01d6ddSTony Luck 
36ca01d6ddSTony Luck /*
376dda9266SLuck, Tony  * We defer making "oops" entries appear in pstore - see
386dda9266SLuck, Tony  * whether the system is actually still running well enough
396dda9266SLuck, Tony  * to let someone see the entry
406dda9266SLuck, Tony  */
416dda9266SLuck, Tony #define	PSTORE_INTERVAL	(60 * HZ)
426dda9266SLuck, Tony 
436dda9266SLuck, Tony static int pstore_new_entry;
446dda9266SLuck, Tony 
456dda9266SLuck, Tony static void pstore_timefunc(unsigned long);
466dda9266SLuck, Tony static DEFINE_TIMER(pstore_timer, pstore_timefunc, 0, 0);
476dda9266SLuck, Tony 
486dda9266SLuck, Tony static void pstore_dowork(struct work_struct *);
496dda9266SLuck, Tony static DECLARE_WORK(pstore_work, pstore_dowork);
506dda9266SLuck, Tony 
516dda9266SLuck, Tony /*
52ca01d6ddSTony Luck  * pstore_lock just protects "psinfo" during
53ca01d6ddSTony Luck  * calls to pstore_register()
54ca01d6ddSTony Luck  */
55ca01d6ddSTony Luck static DEFINE_SPINLOCK(pstore_lock);
56ca01d6ddSTony Luck static struct pstore_info *psinfo;
57ca01d6ddSTony Luck 
58dee28e72SMatthew Garrett static char *backend;
59dee28e72SMatthew Garrett 
60366f7e7aSLuck, Tony /* How much of the console log to snapshot */
61ca01d6ddSTony Luck static unsigned long kmsg_bytes = 10240;
62ca01d6ddSTony Luck 
63366f7e7aSLuck, Tony void pstore_set_kmsg_bytes(int bytes)
64ca01d6ddSTony Luck {
65366f7e7aSLuck, Tony 	kmsg_bytes = bytes;
66ca01d6ddSTony Luck }
67ca01d6ddSTony Luck 
68ca01d6ddSTony Luck /* Tag each group of saved records with a sequence number */
69ca01d6ddSTony Luck static int	oopscount;
70ca01d6ddSTony Luck 
719f6af27fSTony Luck static char *reason_str[] = {
729f6af27fSTony Luck 	"Oops", "Panic", "Kexec", "Restart", "Halt", "Poweroff", "Emergency"
739f6af27fSTony Luck };
749f6af27fSTony Luck 
75ca01d6ddSTony Luck /*
76ca01d6ddSTony Luck  * callback from kmsg_dump. (s2,l2) has the most recently
77ca01d6ddSTony Luck  * written bytes, older bytes are in (s1,l1). Save as much
78ca01d6ddSTony Luck  * as we can from the end of the buffer.
79ca01d6ddSTony Luck  */
80ca01d6ddSTony Luck static void pstore_dump(struct kmsg_dumper *dumper,
81ca01d6ddSTony Luck 	    enum kmsg_dump_reason reason,
82ca01d6ddSTony Luck 	    const char *s1, unsigned long l1,
83ca01d6ddSTony Luck 	    const char *s2, unsigned long l2)
84ca01d6ddSTony Luck {
85ca01d6ddSTony Luck 	unsigned long	s1_start, s2_start;
86ca01d6ddSTony Luck 	unsigned long	l1_cpy, l2_cpy;
87ca01d6ddSTony Luck 	unsigned long	size, total = 0;
889f6af27fSTony Luck 	char		*dst, *why;
89ca01d6ddSTony Luck 	u64		id;
90b238b8faSChen Gong 	int		hsize, ret;
91b94fdd07SMatthew Garrett 	unsigned int	part = 1;
92abd4d558SDon Zickus 	unsigned long	flags = 0;
93abd4d558SDon Zickus 	int		is_locked = 0;
94ca01d6ddSTony Luck 
959f6af27fSTony Luck 	if (reason < ARRAY_SIZE(reason_str))
969f6af27fSTony Luck 		why = reason_str[reason];
979f6af27fSTony Luck 	else
989f6af27fSTony Luck 		why = "Unknown";
999f6af27fSTony Luck 
100abd4d558SDon Zickus 	if (in_nmi()) {
101abd4d558SDon Zickus 		is_locked = spin_trylock(&psinfo->buf_lock);
102abd4d558SDon Zickus 		if (!is_locked)
103abd4d558SDon Zickus 			pr_err("pstore dump routine blocked in NMI, may corrupt error record\n");
104abd4d558SDon Zickus 	} else
105abd4d558SDon Zickus 		spin_lock_irqsave(&psinfo->buf_lock, flags);
106ca01d6ddSTony Luck 	oopscount++;
107ca01d6ddSTony Luck 	while (total < kmsg_bytes) {
108ca01d6ddSTony Luck 		dst = psinfo->buf;
10956280682SMatthew Garrett 		hsize = sprintf(dst, "%s#%d Part%d\n", why, oopscount, part);
110ca01d6ddSTony Luck 		size = psinfo->bufsize - hsize;
111ca01d6ddSTony Luck 		dst += hsize;
112ca01d6ddSTony Luck 
113ca01d6ddSTony Luck 		l2_cpy = min(l2, size);
114ca01d6ddSTony Luck 		l1_cpy = min(l1, size - l2_cpy);
115ca01d6ddSTony Luck 
116ca01d6ddSTony Luck 		if (l1_cpy + l2_cpy == 0)
117ca01d6ddSTony Luck 			break;
118ca01d6ddSTony Luck 
119ca01d6ddSTony Luck 		s2_start = l2 - l2_cpy;
120ca01d6ddSTony Luck 		s1_start = l1 - l1_cpy;
121ca01d6ddSTony Luck 
122ca01d6ddSTony Luck 		memcpy(dst, s1 + s1_start, l1_cpy);
123ca01d6ddSTony Luck 		memcpy(dst + l1_cpy, s2 + s2_start, l2_cpy);
124ca01d6ddSTony Luck 
1253d6d8d20SKees Cook 		ret = psinfo->write(PSTORE_TYPE_DMESG, reason, &id, part,
12656280682SMatthew Garrett 				   hsize + l1_cpy + l2_cpy, psinfo);
127b238b8faSChen Gong 		if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted())
1286dda9266SLuck, Tony 			pstore_new_entry = 1;
129ca01d6ddSTony Luck 		l1 -= l1_cpy;
130ca01d6ddSTony Luck 		l2 -= l2_cpy;
131ca01d6ddSTony Luck 		total += l1_cpy + l2_cpy;
13256280682SMatthew Garrett 		part++;
133ca01d6ddSTony Luck 	}
134abd4d558SDon Zickus 	if (in_nmi()) {
135abd4d558SDon Zickus 		if (is_locked)
136abd4d558SDon Zickus 			spin_unlock(&psinfo->buf_lock);
137abd4d558SDon Zickus 	} else
138abd4d558SDon Zickus 		spin_unlock_irqrestore(&psinfo->buf_lock, flags);
139ca01d6ddSTony Luck }
140ca01d6ddSTony Luck 
141ca01d6ddSTony Luck static struct kmsg_dumper pstore_dumper = {
142ca01d6ddSTony Luck 	.dump = pstore_dump,
143ca01d6ddSTony Luck };
144ca01d6ddSTony Luck 
145ca01d6ddSTony Luck /*
146ca01d6ddSTony Luck  * platform specific persistent storage driver registers with
147ca01d6ddSTony Luck  * us here. If pstore is already mounted, call the platform
148ca01d6ddSTony Luck  * read function right away to populate the file system. If not
149ca01d6ddSTony Luck  * then the pstore mount code will call us later to fill out
150ca01d6ddSTony Luck  * the file system.
151ca01d6ddSTony Luck  *
152ca01d6ddSTony Luck  * Register with kmsg_dump to save last part of console log on panic.
153ca01d6ddSTony Luck  */
154ca01d6ddSTony Luck int pstore_register(struct pstore_info *psi)
155ca01d6ddSTony Luck {
156ca01d6ddSTony Luck 	struct module *owner = psi->owner;
157ca01d6ddSTony Luck 
158ca01d6ddSTony Luck 	spin_lock(&pstore_lock);
159ca01d6ddSTony Luck 	if (psinfo) {
160ca01d6ddSTony Luck 		spin_unlock(&pstore_lock);
161ca01d6ddSTony Luck 		return -EBUSY;
162ca01d6ddSTony Luck 	}
163dee28e72SMatthew Garrett 
164dee28e72SMatthew Garrett 	if (backend && strcmp(backend, psi->name)) {
165dee28e72SMatthew Garrett 		spin_unlock(&pstore_lock);
166dee28e72SMatthew Garrett 		return -EINVAL;
167dee28e72SMatthew Garrett 	}
168dee28e72SMatthew Garrett 
169ca01d6ddSTony Luck 	psinfo = psi;
170f6f82851SKees Cook 	mutex_init(&psinfo->read_mutex);
171ca01d6ddSTony Luck 	spin_unlock(&pstore_lock);
172ca01d6ddSTony Luck 
173ca01d6ddSTony Luck 	if (owner && !try_module_get(owner)) {
174ca01d6ddSTony Luck 		psinfo = NULL;
175ca01d6ddSTony Luck 		return -EINVAL;
176ca01d6ddSTony Luck 	}
177ca01d6ddSTony Luck 
178ca01d6ddSTony Luck 	if (pstore_is_mounted())
1796dda9266SLuck, Tony 		pstore_get_records(0);
180ca01d6ddSTony Luck 
181ca01d6ddSTony Luck 	kmsg_dump_register(&pstore_dumper);
182ca01d6ddSTony Luck 
1836dda9266SLuck, Tony 	pstore_timer.expires = jiffies + PSTORE_INTERVAL;
1846dda9266SLuck, Tony 	add_timer(&pstore_timer);
1856dda9266SLuck, Tony 
186ca01d6ddSTony Luck 	return 0;
187ca01d6ddSTony Luck }
188ca01d6ddSTony Luck EXPORT_SYMBOL_GPL(pstore_register);
189ca01d6ddSTony Luck 
190ca01d6ddSTony Luck /*
1916dda9266SLuck, Tony  * Read all the records from the persistent store. Create
1926dda9266SLuck, Tony  * files in our filesystem.  Don't warn about -EEXIST errors
1936dda9266SLuck, Tony  * when we are re-scanning the backing store looking to add new
1946dda9266SLuck, Tony  * error records.
195ca01d6ddSTony Luck  */
1966dda9266SLuck, Tony void pstore_get_records(int quiet)
197ca01d6ddSTony Luck {
198ca01d6ddSTony Luck 	struct pstore_info *psi = psinfo;
199f6f82851SKees Cook 	char			*buf = NULL;
2008d38d74bSChen Gong 	ssize_t			size;
201ca01d6ddSTony Luck 	u64			id;
202ca01d6ddSTony Luck 	enum pstore_type_id	type;
203ca01d6ddSTony Luck 	struct timespec		time;
20406cf91b4SChen Gong 	int			failed = 0, rc;
205ca01d6ddSTony Luck 
206ca01d6ddSTony Luck 	if (!psi)
207ca01d6ddSTony Luck 		return;
208ca01d6ddSTony Luck 
209f6f82851SKees Cook 	mutex_lock(&psi->read_mutex);
210*2174f6dfSKees Cook 	if (psi->open && psi->open(psi))
21106cf91b4SChen Gong 		goto out;
21206cf91b4SChen Gong 
213f6f82851SKees Cook 	while ((size = psi->read(&id, &type, &time, &buf, psi)) > 0) {
214f6f82851SKees Cook 		rc = pstore_mkfile(type, psi->name, id, buf, (size_t)size,
2156dda9266SLuck, Tony 				  time, psi);
216f6f82851SKees Cook 		kfree(buf);
217f6f82851SKees Cook 		buf = NULL;
2186dda9266SLuck, Tony 		if (rc && (rc != -EEXIST || !quiet))
219ca01d6ddSTony Luck 			failed++;
220ca01d6ddSTony Luck 	}
221*2174f6dfSKees Cook 	if (psi->close)
22206cf91b4SChen Gong 		psi->close(psi);
22306cf91b4SChen Gong out:
224f6f82851SKees Cook 	mutex_unlock(&psi->read_mutex);
225ca01d6ddSTony Luck 
226ca01d6ddSTony Luck 	if (failed)
227ca01d6ddSTony Luck 		printk(KERN_WARNING "pstore: failed to load %d record(s) from '%s'\n",
228ca01d6ddSTony Luck 		       failed, psi->name);
229ca01d6ddSTony Luck }
230ca01d6ddSTony Luck 
2316dda9266SLuck, Tony static void pstore_dowork(struct work_struct *work)
2326dda9266SLuck, Tony {
2336dda9266SLuck, Tony 	pstore_get_records(1);
2346dda9266SLuck, Tony }
2356dda9266SLuck, Tony 
2366dda9266SLuck, Tony static void pstore_timefunc(unsigned long dummy)
2376dda9266SLuck, Tony {
2386dda9266SLuck, Tony 	if (pstore_new_entry) {
2396dda9266SLuck, Tony 		pstore_new_entry = 0;
2406dda9266SLuck, Tony 		schedule_work(&pstore_work);
2416dda9266SLuck, Tony 	}
2426dda9266SLuck, Tony 
2436dda9266SLuck, Tony 	mod_timer(&pstore_timer, jiffies + PSTORE_INTERVAL);
2446dda9266SLuck, Tony }
2456dda9266SLuck, Tony 
246dee28e72SMatthew Garrett module_param(backend, charp, 0444);
247dee28e72SMatthew Garrett MODULE_PARM_DESC(backend, "Pstore backend to use");
248