xref: /linux/fs/pstore/platform.c (revision f49f4ab95c301dbccad0efe85296d908b8ae7ad4)
1 /*
2  * Persistent Storage - platform driver interface parts.
3  *
4  * Copyright (C) 2007-2008 Google, Inc.
5  * 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>
26 #include <linux/console.h>
27 #include <linux/module.h>
28 #include <linux/pstore.h>
29 #include <linux/string.h>
30 #include <linux/timer.h>
31 #include <linux/slab.h>
32 #include <linux/uaccess.h>
33 #include <linux/hardirq.h>
34 #include <linux/jiffies.h>
35 #include <linux/workqueue.h>
36 
37 #include "internal.h"
38 
39 /*
40  * 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  */
44 static int pstore_update_ms = -1;
45 module_param_named(update_ms, pstore_update_ms, int, 0600);
46 MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content "
47 		 "(default is -1, which means runtime updates are disabled; "
48 		 "enabling this option is not safe, it may lead to further "
49 		 "corruption on Oopses)");
50 
51 static int pstore_new_entry;
52 
53 static void pstore_timefunc(unsigned long);
54 static DEFINE_TIMER(pstore_timer, pstore_timefunc, 0, 0);
55 
56 static void pstore_dowork(struct work_struct *);
57 static DECLARE_WORK(pstore_work, pstore_dowork);
58 
59 /*
60  * pstore_lock just protects "psinfo" during
61  * calls to pstore_register()
62  */
63 static DEFINE_SPINLOCK(pstore_lock);
64 struct pstore_info *psinfo;
65 
66 static char *backend;
67 
68 /* How much of the console log to snapshot */
69 static unsigned long kmsg_bytes = 10240;
70 
71 void pstore_set_kmsg_bytes(int bytes)
72 {
73 	kmsg_bytes = bytes;
74 }
75 
76 /* Tag each group of saved records with a sequence number */
77 static int	oopscount;
78 
79 static const char *get_reason_str(enum kmsg_dump_reason reason)
80 {
81 	switch (reason) {
82 	case KMSG_DUMP_PANIC:
83 		return "Panic";
84 	case KMSG_DUMP_OOPS:
85 		return "Oops";
86 	case KMSG_DUMP_EMERG:
87 		return "Emergency";
88 	case KMSG_DUMP_RESTART:
89 		return "Restart";
90 	case KMSG_DUMP_HALT:
91 		return "Halt";
92 	case KMSG_DUMP_POWEROFF:
93 		return "Poweroff";
94 	default:
95 		return "Unknown";
96 	}
97 }
98 
99 /*
100  * callback from kmsg_dump. (s2,l2) has the most recently
101  * written bytes, older bytes are in (s1,l1). Save as much
102  * as we can from the end of the buffer.
103  */
104 static void pstore_dump(struct kmsg_dumper *dumper,
105 			enum kmsg_dump_reason reason)
106 {
107 	unsigned long	total = 0;
108 	const char	*why;
109 	u64		id;
110 	unsigned int	part = 1;
111 	unsigned long	flags = 0;
112 	int		is_locked = 0;
113 	int		ret;
114 
115 	why = get_reason_str(reason);
116 
117 	if (in_nmi()) {
118 		is_locked = spin_trylock(&psinfo->buf_lock);
119 		if (!is_locked)
120 			pr_err("pstore dump routine blocked in NMI, may corrupt error record\n");
121 	} else
122 		spin_lock_irqsave(&psinfo->buf_lock, flags);
123 	oopscount++;
124 	while (total < kmsg_bytes) {
125 		char *dst;
126 		unsigned long size;
127 		int hsize;
128 		size_t len;
129 
130 		dst = psinfo->buf;
131 		hsize = sprintf(dst, "%s#%d Part%d\n", why, oopscount, part);
132 		size = psinfo->bufsize - hsize;
133 		dst += hsize;
134 
135 		if (!kmsg_dump_get_buffer(dumper, true, dst, size, &len))
136 			break;
137 
138 		ret = psinfo->write(PSTORE_TYPE_DMESG, reason, &id, part,
139 				    hsize + len, psinfo);
140 		if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted())
141 			pstore_new_entry = 1;
142 
143 		total += hsize + len;
144 		part++;
145 	}
146 	if (in_nmi()) {
147 		if (is_locked)
148 			spin_unlock(&psinfo->buf_lock);
149 	} else
150 		spin_unlock_irqrestore(&psinfo->buf_lock, flags);
151 }
152 
153 static struct kmsg_dumper pstore_dumper = {
154 	.dump = pstore_dump,
155 };
156 
157 #ifdef CONFIG_PSTORE_CONSOLE
158 static void pstore_console_write(struct console *con, const char *s, unsigned c)
159 {
160 	const char *e = s + c;
161 
162 	while (s < e) {
163 		unsigned long flags;
164 
165 		if (c > psinfo->bufsize)
166 			c = psinfo->bufsize;
167 
168 		if (oops_in_progress) {
169 			if (!spin_trylock_irqsave(&psinfo->buf_lock, flags))
170 				break;
171 		} else {
172 			spin_lock_irqsave(&psinfo->buf_lock, flags);
173 		}
174 		memcpy(psinfo->buf, s, c);
175 		psinfo->write(PSTORE_TYPE_CONSOLE, 0, NULL, 0, c, psinfo);
176 		spin_unlock_irqrestore(&psinfo->buf_lock, flags);
177 		s += c;
178 		c = e - s;
179 	}
180 }
181 
182 static struct console pstore_console = {
183 	.name	= "pstore",
184 	.write	= pstore_console_write,
185 	.flags	= CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME,
186 	.index	= -1,
187 };
188 
189 static void pstore_register_console(void)
190 {
191 	register_console(&pstore_console);
192 }
193 #else
194 static void pstore_register_console(void) {}
195 #endif
196 
197 static int pstore_write_compat(enum pstore_type_id type,
198 			       enum kmsg_dump_reason reason,
199 			       u64 *id, unsigned int part,
200 			       size_t size, struct pstore_info *psi)
201 {
202 	return psi->write_buf(type, reason, id, part, psinfo->buf, size, psi);
203 }
204 
205 /*
206  * platform specific persistent storage driver registers with
207  * us here. If pstore is already mounted, call the platform
208  * read function right away to populate the file system. If not
209  * then the pstore mount code will call us later to fill out
210  * the file system.
211  *
212  * Register with kmsg_dump to save last part of console log on panic.
213  */
214 int pstore_register(struct pstore_info *psi)
215 {
216 	struct module *owner = psi->owner;
217 
218 	spin_lock(&pstore_lock);
219 	if (psinfo) {
220 		spin_unlock(&pstore_lock);
221 		return -EBUSY;
222 	}
223 
224 	if (backend && strcmp(backend, psi->name)) {
225 		spin_unlock(&pstore_lock);
226 		return -EINVAL;
227 	}
228 
229 	if (!psi->write)
230 		psi->write = pstore_write_compat;
231 	psinfo = psi;
232 	mutex_init(&psinfo->read_mutex);
233 	spin_unlock(&pstore_lock);
234 
235 	if (owner && !try_module_get(owner)) {
236 		psinfo = NULL;
237 		return -EINVAL;
238 	}
239 
240 	if (pstore_is_mounted())
241 		pstore_get_records(0);
242 
243 	kmsg_dump_register(&pstore_dumper);
244 	pstore_register_console();
245 	pstore_register_ftrace();
246 
247 	if (pstore_update_ms >= 0) {
248 		pstore_timer.expires = jiffies +
249 			msecs_to_jiffies(pstore_update_ms);
250 		add_timer(&pstore_timer);
251 	}
252 
253 	return 0;
254 }
255 EXPORT_SYMBOL_GPL(pstore_register);
256 
257 /*
258  * Read all the records from the persistent store. Create
259  * files in our filesystem.  Don't warn about -EEXIST errors
260  * when we are re-scanning the backing store looking to add new
261  * error records.
262  */
263 void pstore_get_records(int quiet)
264 {
265 	struct pstore_info *psi = psinfo;
266 	char			*buf = NULL;
267 	ssize_t			size;
268 	u64			id;
269 	enum pstore_type_id	type;
270 	struct timespec		time;
271 	int			failed = 0, rc;
272 
273 	if (!psi)
274 		return;
275 
276 	mutex_lock(&psi->read_mutex);
277 	if (psi->open && psi->open(psi))
278 		goto out;
279 
280 	while ((size = psi->read(&id, &type, &time, &buf, psi)) > 0) {
281 		rc = pstore_mkfile(type, psi->name, id, buf, (size_t)size,
282 				  time, psi);
283 		kfree(buf);
284 		buf = NULL;
285 		if (rc && (rc != -EEXIST || !quiet))
286 			failed++;
287 	}
288 	if (psi->close)
289 		psi->close(psi);
290 out:
291 	mutex_unlock(&psi->read_mutex);
292 
293 	if (failed)
294 		printk(KERN_WARNING "pstore: failed to load %d record(s) from '%s'\n",
295 		       failed, psi->name);
296 }
297 
298 static void pstore_dowork(struct work_struct *work)
299 {
300 	pstore_get_records(1);
301 }
302 
303 static void pstore_timefunc(unsigned long dummy)
304 {
305 	if (pstore_new_entry) {
306 		pstore_new_entry = 0;
307 		schedule_work(&pstore_work);
308 	}
309 
310 	mod_timer(&pstore_timer, jiffies + msecs_to_jiffies(pstore_update_ms));
311 }
312 
313 module_param(backend, charp, 0444);
314 MODULE_PARM_DESC(backend, "Pstore backend to use");
315