]>
Commit | Line | Data |
---|---|---|
ca01d6dd TL |
1 | /* |
2 | * Persistent Storage - platform driver interface parts. | |
3 | * | |
f29e5956 | 4 | * Copyright (C) 2007-2008 Google, Inc. |
ca01d6dd TL |
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> | |
f29e5956 | 26 | #include <linux/console.h> |
ca01d6dd TL |
27 | #include <linux/module.h> |
28 | #include <linux/pstore.h> | |
29 | #include <linux/string.h> | |
6dda9266 | 30 | #include <linux/timer.h> |
ca01d6dd TL |
31 | #include <linux/slab.h> |
32 | #include <linux/uaccess.h> | |
abd4d558 | 33 | #include <linux/hardirq.h> |
a3f5f075 | 34 | #include <linux/jiffies.h> |
6dda9266 | 35 | #include <linux/workqueue.h> |
ca01d6dd TL |
36 | |
37 | #include "internal.h" | |
38 | ||
6dda9266 TL |
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 | */ | |
a3f5f075 AV |
44 | static int pstore_update_ms = 60000; |
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 60000; -1 means runtime updates are disabled)"); | |
6dda9266 TL |
48 | |
49 | static int pstore_new_entry; | |
50 | ||
51 | static void pstore_timefunc(unsigned long); | |
52 | static DEFINE_TIMER(pstore_timer, pstore_timefunc, 0, 0); | |
53 | ||
54 | static void pstore_dowork(struct work_struct *); | |
55 | static DECLARE_WORK(pstore_work, pstore_dowork); | |
56 | ||
ca01d6dd TL |
57 | /* |
58 | * pstore_lock just protects "psinfo" during | |
59 | * calls to pstore_register() | |
60 | */ | |
61 | static DEFINE_SPINLOCK(pstore_lock); | |
62 | static struct pstore_info *psinfo; | |
63 | ||
dee28e72 MG |
64 | static char *backend; |
65 | ||
366f7e7a | 66 | /* How much of the console log to snapshot */ |
ca01d6dd TL |
67 | static unsigned long kmsg_bytes = 10240; |
68 | ||
366f7e7a | 69 | void pstore_set_kmsg_bytes(int bytes) |
ca01d6dd | 70 | { |
366f7e7a | 71 | kmsg_bytes = bytes; |
ca01d6dd TL |
72 | } |
73 | ||
ca01d6dd TL |
74 | /* Tag each group of saved records with a sequence number */ |
75 | static int oopscount; | |
76 | ||
381b872c SA |
77 | static 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 | } | |
9f6af27f | 96 | |
ca01d6dd TL |
97 | /* |
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 | */ | |
102 | static 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; | |
381b872c SA |
110 | char *dst; |
111 | const char *why; | |
ca01d6dd | 112 | u64 id; |
b238b8fa | 113 | int hsize, ret; |
b94fdd07 | 114 | unsigned int part = 1; |
abd4d558 DZ |
115 | unsigned long flags = 0; |
116 | int is_locked = 0; | |
ca01d6dd | 117 | |
381b872c | 118 | why = get_reason_str(reason); |
9f6af27f | 119 | |
abd4d558 DZ |
120 | 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); | |
ca01d6dd TL |
126 | oopscount++; |
127 | while (total < kmsg_bytes) { | |
128 | dst = psinfo->buf; | |
56280682 | 129 | hsize = sprintf(dst, "%s#%d Part%d\n", why, oopscount, part); |
ca01d6dd TL |
130 | 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 | ||
3d6d8d20 | 145 | ret = psinfo->write(PSTORE_TYPE_DMESG, reason, &id, part, |
56280682 | 146 | hsize + l1_cpy + l2_cpy, psinfo); |
b238b8fa | 147 | if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted()) |
6dda9266 | 148 | pstore_new_entry = 1; |
ca01d6dd TL |
149 | l1 -= l1_cpy; |
150 | l2 -= l2_cpy; | |
151 | total += l1_cpy + l2_cpy; | |
56280682 | 152 | part++; |
ca01d6dd | 153 | } |
abd4d558 DZ |
154 | if (in_nmi()) { |
155 | if (is_locked) | |
156 | spin_unlock(&psinfo->buf_lock); | |
157 | } else | |
158 | spin_unlock_irqrestore(&psinfo->buf_lock, flags); | |
ca01d6dd TL |
159 | } |
160 | ||
161 | static struct kmsg_dumper pstore_dumper = { | |
162 | .dump = pstore_dump, | |
163 | }; | |
164 | ||
f29e5956 AV |
165 | #ifdef CONFIG_PSTORE_CONSOLE |
166 | static 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 | ||
184 | static 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 | ||
191 | static void pstore_register_console(void) | |
192 | { | |
193 | register_console(&pstore_console); | |
194 | } | |
195 | #else | |
196 | static void pstore_register_console(void) {} | |
197 | #endif | |
198 | ||
ca01d6dd TL |
199 | /* |
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 | */ | |
208 | int 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 | } | |
dee28e72 MG |
217 | |
218 | if (backend && strcmp(backend, psi->name)) { | |
219 | spin_unlock(&pstore_lock); | |
220 | return -EINVAL; | |
221 | } | |
222 | ||
ca01d6dd | 223 | psinfo = psi; |
f6f82851 | 224 | mutex_init(&psinfo->read_mutex); |
ca01d6dd TL |
225 | 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()) | |
6dda9266 | 233 | pstore_get_records(0); |
ca01d6dd TL |
234 | |
235 | kmsg_dump_register(&pstore_dumper); | |
f29e5956 | 236 | pstore_register_console(); |
ca01d6dd | 237 | |
a3f5f075 AV |
238 | if (pstore_update_ms >= 0) { |
239 | pstore_timer.expires = jiffies + | |
240 | msecs_to_jiffies(pstore_update_ms); | |
241 | add_timer(&pstore_timer); | |
242 | } | |
6dda9266 | 243 | |
ca01d6dd TL |
244 | return 0; |
245 | } | |
246 | EXPORT_SYMBOL_GPL(pstore_register); | |
247 | ||
248 | /* | |
6dda9266 TL |
249 | * 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. | |
ca01d6dd | 253 | */ |
6dda9266 | 254 | void pstore_get_records(int quiet) |
ca01d6dd TL |
255 | { |
256 | struct pstore_info *psi = psinfo; | |
f6f82851 | 257 | char *buf = NULL; |
8d38d74b | 258 | ssize_t size; |
ca01d6dd TL |
259 | u64 id; |
260 | enum pstore_type_id type; | |
261 | struct timespec time; | |
06cf91b4 | 262 | int failed = 0, rc; |
ca01d6dd TL |
263 | |
264 | if (!psi) | |
265 | return; | |
266 | ||
f6f82851 | 267 | mutex_lock(&psi->read_mutex); |
2174f6df | 268 | if (psi->open && psi->open(psi)) |
06cf91b4 CG |
269 | goto out; |
270 | ||
f6f82851 KC |
271 | while ((size = psi->read(&id, &type, &time, &buf, psi)) > 0) { |
272 | rc = pstore_mkfile(type, psi->name, id, buf, (size_t)size, | |
6dda9266 | 273 | time, psi); |
f6f82851 KC |
274 | kfree(buf); |
275 | buf = NULL; | |
6dda9266 | 276 | if (rc && (rc != -EEXIST || !quiet)) |
ca01d6dd TL |
277 | failed++; |
278 | } | |
2174f6df KC |
279 | if (psi->close) |
280 | psi->close(psi); | |
06cf91b4 | 281 | out: |
f6f82851 | 282 | mutex_unlock(&psi->read_mutex); |
ca01d6dd TL |
283 | |
284 | if (failed) | |
285 | printk(KERN_WARNING "pstore: failed to load %d record(s) from '%s'\n", | |
286 | failed, psi->name); | |
287 | } | |
288 | ||
6dda9266 TL |
289 | static void pstore_dowork(struct work_struct *work) |
290 | { | |
291 | pstore_get_records(1); | |
292 | } | |
293 | ||
294 | static void pstore_timefunc(unsigned long dummy) | |
295 | { | |
296 | if (pstore_new_entry) { | |
297 | pstore_new_entry = 0; | |
298 | schedule_work(&pstore_work); | |
299 | } | |
300 | ||
a3f5f075 | 301 | mod_timer(&pstore_timer, jiffies + msecs_to_jiffies(pstore_update_ms)); |
6dda9266 TL |
302 | } |
303 | ||
dee28e72 MG |
304 | module_param(backend, charp, 0444); |
305 | MODULE_PARM_DESC(backend, "Pstore backend to use"); |