xref: /openbmc/linux/fs/pstore/platform.c (revision 6dda9266913ad57e09afc1a10d6473f10c806a63)
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>
28*6dda9266SLuck, Tony #include <linux/timer.h>
29ca01d6ddSTony Luck #include <linux/slab.h>
30ca01d6ddSTony Luck #include <linux/uaccess.h>
31*6dda9266SLuck, Tony #include <linux/workqueue.h>
32ca01d6ddSTony Luck 
33ca01d6ddSTony Luck #include "internal.h"
34ca01d6ddSTony Luck 
35ca01d6ddSTony Luck /*
36*6dda9266SLuck, Tony  * We defer making "oops" entries appear in pstore - see
37*6dda9266SLuck, Tony  * whether the system is actually still running well enough
38*6dda9266SLuck, Tony  * to let someone see the entry
39*6dda9266SLuck, Tony  */
40*6dda9266SLuck, Tony #define	PSTORE_INTERVAL	(60 * HZ)
41*6dda9266SLuck, Tony 
42*6dda9266SLuck, Tony static int pstore_new_entry;
43*6dda9266SLuck, Tony 
44*6dda9266SLuck, Tony static void pstore_timefunc(unsigned long);
45*6dda9266SLuck, Tony static DEFINE_TIMER(pstore_timer, pstore_timefunc, 0, 0);
46*6dda9266SLuck, Tony 
47*6dda9266SLuck, Tony static void pstore_dowork(struct work_struct *);
48*6dda9266SLuck, Tony static DECLARE_WORK(pstore_work, pstore_dowork);
49*6dda9266SLuck, Tony 
50*6dda9266SLuck, Tony /*
51ca01d6ddSTony Luck  * pstore_lock just protects "psinfo" during
52ca01d6ddSTony Luck  * calls to pstore_register()
53ca01d6ddSTony Luck  */
54ca01d6ddSTony Luck static DEFINE_SPINLOCK(pstore_lock);
55ca01d6ddSTony Luck static struct pstore_info *psinfo;
56ca01d6ddSTony Luck 
57dee28e72SMatthew Garrett static char *backend;
58dee28e72SMatthew Garrett 
59366f7e7aSLuck, Tony /* How much of the console log to snapshot */
60ca01d6ddSTony Luck static unsigned long kmsg_bytes = 10240;
61ca01d6ddSTony Luck 
62366f7e7aSLuck, Tony void pstore_set_kmsg_bytes(int bytes)
63ca01d6ddSTony Luck {
64366f7e7aSLuck, Tony 	kmsg_bytes = bytes;
65ca01d6ddSTony Luck }
66ca01d6ddSTony Luck 
67ca01d6ddSTony Luck /* Tag each group of saved records with a sequence number */
68ca01d6ddSTony Luck static int	oopscount;
69ca01d6ddSTony Luck 
709f6af27fSTony Luck static char *reason_str[] = {
719f6af27fSTony Luck 	"Oops", "Panic", "Kexec", "Restart", "Halt", "Poweroff", "Emergency"
729f6af27fSTony Luck };
739f6af27fSTony Luck 
74ca01d6ddSTony Luck /*
75ca01d6ddSTony Luck  * callback from kmsg_dump. (s2,l2) has the most recently
76ca01d6ddSTony Luck  * written bytes, older bytes are in (s1,l1). Save as much
77ca01d6ddSTony Luck  * as we can from the end of the buffer.
78ca01d6ddSTony Luck  */
79ca01d6ddSTony Luck static void pstore_dump(struct kmsg_dumper *dumper,
80ca01d6ddSTony Luck 	    enum kmsg_dump_reason reason,
81ca01d6ddSTony Luck 	    const char *s1, unsigned long l1,
82ca01d6ddSTony Luck 	    const char *s2, unsigned long l2)
83ca01d6ddSTony Luck {
84ca01d6ddSTony Luck 	unsigned long	s1_start, s2_start;
85ca01d6ddSTony Luck 	unsigned long	l1_cpy, l2_cpy;
86ca01d6ddSTony Luck 	unsigned long	size, total = 0;
879f6af27fSTony Luck 	char		*dst, *why;
88ca01d6ddSTony Luck 	u64		id;
89b94fdd07SMatthew Garrett 	int		hsize;
90b94fdd07SMatthew Garrett 	unsigned int	part = 1;
91ca01d6ddSTony Luck 
929f6af27fSTony Luck 	if (reason < ARRAY_SIZE(reason_str))
939f6af27fSTony Luck 		why = reason_str[reason];
949f6af27fSTony Luck 	else
959f6af27fSTony Luck 		why = "Unknown";
969f6af27fSTony Luck 
97ca01d6ddSTony Luck 	mutex_lock(&psinfo->buf_mutex);
98ca01d6ddSTony Luck 	oopscount++;
99ca01d6ddSTony Luck 	while (total < kmsg_bytes) {
100ca01d6ddSTony Luck 		dst = psinfo->buf;
10156280682SMatthew Garrett 		hsize = sprintf(dst, "%s#%d Part%d\n", why, oopscount, part);
102ca01d6ddSTony Luck 		size = psinfo->bufsize - hsize;
103ca01d6ddSTony Luck 		dst += hsize;
104ca01d6ddSTony Luck 
105ca01d6ddSTony Luck 		l2_cpy = min(l2, size);
106ca01d6ddSTony Luck 		l1_cpy = min(l1, size - l2_cpy);
107ca01d6ddSTony Luck 
108ca01d6ddSTony Luck 		if (l1_cpy + l2_cpy == 0)
109ca01d6ddSTony Luck 			break;
110ca01d6ddSTony Luck 
111ca01d6ddSTony Luck 		s2_start = l2 - l2_cpy;
112ca01d6ddSTony Luck 		s1_start = l1 - l1_cpy;
113ca01d6ddSTony Luck 
114ca01d6ddSTony Luck 		memcpy(dst, s1 + s1_start, l1_cpy);
115ca01d6ddSTony Luck 		memcpy(dst + l1_cpy, s2 + s2_start, l2_cpy);
116ca01d6ddSTony Luck 
11756280682SMatthew Garrett 		id = psinfo->write(PSTORE_TYPE_DMESG, part,
11856280682SMatthew Garrett 				   hsize + l1_cpy + l2_cpy, psinfo);
1199f6af27fSTony Luck 		if (reason == KMSG_DUMP_OOPS && pstore_is_mounted())
120*6dda9266SLuck, Tony 			pstore_new_entry = 1;
121ca01d6ddSTony Luck 		l1 -= l1_cpy;
122ca01d6ddSTony Luck 		l2 -= l2_cpy;
123ca01d6ddSTony Luck 		total += l1_cpy + l2_cpy;
12456280682SMatthew Garrett 		part++;
125ca01d6ddSTony Luck 	}
126ca01d6ddSTony Luck 	mutex_unlock(&psinfo->buf_mutex);
127ca01d6ddSTony Luck }
128ca01d6ddSTony Luck 
129ca01d6ddSTony Luck static struct kmsg_dumper pstore_dumper = {
130ca01d6ddSTony Luck 	.dump = pstore_dump,
131ca01d6ddSTony Luck };
132ca01d6ddSTony Luck 
133ca01d6ddSTony Luck /*
134ca01d6ddSTony Luck  * platform specific persistent storage driver registers with
135ca01d6ddSTony Luck  * us here. If pstore is already mounted, call the platform
136ca01d6ddSTony Luck  * read function right away to populate the file system. If not
137ca01d6ddSTony Luck  * then the pstore mount code will call us later to fill out
138ca01d6ddSTony Luck  * the file system.
139ca01d6ddSTony Luck  *
140ca01d6ddSTony Luck  * Register with kmsg_dump to save last part of console log on panic.
141ca01d6ddSTony Luck  */
142ca01d6ddSTony Luck int pstore_register(struct pstore_info *psi)
143ca01d6ddSTony Luck {
144ca01d6ddSTony Luck 	struct module *owner = psi->owner;
145ca01d6ddSTony Luck 
146ca01d6ddSTony Luck 	spin_lock(&pstore_lock);
147ca01d6ddSTony Luck 	if (psinfo) {
148ca01d6ddSTony Luck 		spin_unlock(&pstore_lock);
149ca01d6ddSTony Luck 		return -EBUSY;
150ca01d6ddSTony Luck 	}
151dee28e72SMatthew Garrett 
152dee28e72SMatthew Garrett 	if (backend && strcmp(backend, psi->name)) {
153dee28e72SMatthew Garrett 		spin_unlock(&pstore_lock);
154dee28e72SMatthew Garrett 		return -EINVAL;
155dee28e72SMatthew Garrett 	}
156dee28e72SMatthew Garrett 
157ca01d6ddSTony Luck 	psinfo = psi;
158ca01d6ddSTony Luck 	spin_unlock(&pstore_lock);
159ca01d6ddSTony Luck 
160ca01d6ddSTony Luck 	if (owner && !try_module_get(owner)) {
161ca01d6ddSTony Luck 		psinfo = NULL;
162ca01d6ddSTony Luck 		return -EINVAL;
163ca01d6ddSTony Luck 	}
164ca01d6ddSTony Luck 
165ca01d6ddSTony Luck 	if (pstore_is_mounted())
166*6dda9266SLuck, Tony 		pstore_get_records(0);
167ca01d6ddSTony Luck 
168ca01d6ddSTony Luck 	kmsg_dump_register(&pstore_dumper);
169ca01d6ddSTony Luck 
170*6dda9266SLuck, Tony 	pstore_timer.expires = jiffies + PSTORE_INTERVAL;
171*6dda9266SLuck, Tony 	add_timer(&pstore_timer);
172*6dda9266SLuck, Tony 
173ca01d6ddSTony Luck 	return 0;
174ca01d6ddSTony Luck }
175ca01d6ddSTony Luck EXPORT_SYMBOL_GPL(pstore_register);
176ca01d6ddSTony Luck 
177ca01d6ddSTony Luck /*
178*6dda9266SLuck, Tony  * Read all the records from the persistent store. Create
179*6dda9266SLuck, Tony  * files in our filesystem.  Don't warn about -EEXIST errors
180*6dda9266SLuck, Tony  * when we are re-scanning the backing store looking to add new
181*6dda9266SLuck, Tony  * error records.
182ca01d6ddSTony Luck  */
183*6dda9266SLuck, Tony void pstore_get_records(int quiet)
184ca01d6ddSTony Luck {
185ca01d6ddSTony Luck 	struct pstore_info *psi = psinfo;
1868d38d74bSChen Gong 	ssize_t			size;
187ca01d6ddSTony Luck 	u64			id;
188ca01d6ddSTony Luck 	enum pstore_type_id	type;
189ca01d6ddSTony Luck 	struct timespec		time;
19006cf91b4SChen Gong 	int			failed = 0, rc;
191ca01d6ddSTony Luck 
192ca01d6ddSTony Luck 	if (!psi)
193ca01d6ddSTony Luck 		return;
194ca01d6ddSTony Luck 
195ca01d6ddSTony Luck 	mutex_lock(&psinfo->buf_mutex);
19606cf91b4SChen Gong 	rc = psi->open(psi);
19706cf91b4SChen Gong 	if (rc)
19806cf91b4SChen Gong 		goto out;
19906cf91b4SChen Gong 
200638c1fd3SMatthew Garrett 	while ((size = psi->read(&id, &type, &time, psi)) > 0) {
201*6dda9266SLuck, Tony 		rc = pstore_mkfile(type, psi->name, id, psi->buf, (size_t)size,
202*6dda9266SLuck, Tony 				  time, psi);
203*6dda9266SLuck, Tony 		if (rc && (rc != -EEXIST || !quiet))
204ca01d6ddSTony Luck 			failed++;
205ca01d6ddSTony Luck 	}
20606cf91b4SChen Gong 	psi->close(psi);
20706cf91b4SChen Gong out:
208ca01d6ddSTony Luck 	mutex_unlock(&psinfo->buf_mutex);
209ca01d6ddSTony Luck 
210ca01d6ddSTony Luck 	if (failed)
211ca01d6ddSTony Luck 		printk(KERN_WARNING "pstore: failed to load %d record(s) from '%s'\n",
212ca01d6ddSTony Luck 		       failed, psi->name);
213ca01d6ddSTony Luck }
214ca01d6ddSTony Luck 
215*6dda9266SLuck, Tony static void pstore_dowork(struct work_struct *work)
216*6dda9266SLuck, Tony {
217*6dda9266SLuck, Tony 	pstore_get_records(1);
218*6dda9266SLuck, Tony }
219*6dda9266SLuck, Tony 
220*6dda9266SLuck, Tony static void pstore_timefunc(unsigned long dummy)
221*6dda9266SLuck, Tony {
222*6dda9266SLuck, Tony 	if (pstore_new_entry) {
223*6dda9266SLuck, Tony 		pstore_new_entry = 0;
224*6dda9266SLuck, Tony 		schedule_work(&pstore_work);
225*6dda9266SLuck, Tony 	}
226*6dda9266SLuck, Tony 
227*6dda9266SLuck, Tony 	mod_timer(&pstore_timer, jiffies + PSTORE_INTERVAL);
228*6dda9266SLuck, Tony }
229*6dda9266SLuck, Tony 
230ca01d6ddSTony Luck /*
231ca01d6ddSTony Luck  * Call platform driver to write a record to the
232ca01d6ddSTony Luck  * persistent store.
233ca01d6ddSTony Luck  */
234ca01d6ddSTony Luck int pstore_write(enum pstore_type_id type, char *buf, size_t size)
235ca01d6ddSTony Luck {
236ca01d6ddSTony Luck 	u64	id;
237ca01d6ddSTony Luck 
238ca01d6ddSTony Luck 	if (!psinfo)
239ca01d6ddSTony Luck 		return -ENODEV;
240ca01d6ddSTony Luck 
241ca01d6ddSTony Luck 	if (size > psinfo->bufsize)
242ca01d6ddSTony Luck 		return -EFBIG;
243ca01d6ddSTony Luck 
244ca01d6ddSTony Luck 	mutex_lock(&psinfo->buf_mutex);
245ca01d6ddSTony Luck 	memcpy(psinfo->buf, buf, size);
24656280682SMatthew Garrett 	id = psinfo->write(type, 0, size, psinfo);
247ca01d6ddSTony Luck 	if (pstore_is_mounted())
248ca01d6ddSTony Luck 		pstore_mkfile(PSTORE_TYPE_DMESG, psinfo->name, id, psinfo->buf,
249638c1fd3SMatthew Garrett 			      size, CURRENT_TIME, psinfo);
250ca01d6ddSTony Luck 	mutex_unlock(&psinfo->buf_mutex);
251ca01d6ddSTony Luck 
252ca01d6ddSTony Luck 	return 0;
253ca01d6ddSTony Luck }
254ca01d6ddSTony Luck EXPORT_SYMBOL_GPL(pstore_write);
255dee28e72SMatthew Garrett 
256dee28e72SMatthew Garrett module_param(backend, charp, 0444);
257dee28e72SMatthew Garrett MODULE_PARM_DESC(backend, "Pstore backend to use");
258