]>
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 | ||
ef748853 FF |
21 | #define pr_fmt(fmt) "pstore: " fmt |
22 | ||
ca01d6dd TL |
23 | #include <linux/atomic.h> |
24 | #include <linux/types.h> | |
25 | #include <linux/errno.h> | |
26 | #include <linux/init.h> | |
27 | #include <linux/kmsg_dump.h> | |
f29e5956 | 28 | #include <linux/console.h> |
ca01d6dd TL |
29 | #include <linux/module.h> |
30 | #include <linux/pstore.h> | |
8cfc8ddc | 31 | #ifdef CONFIG_PSTORE_ZLIB_COMPRESS |
b0aad7a9 | 32 | #include <linux/zlib.h> |
8cfc8ddc GT |
33 | #endif |
34 | #ifdef CONFIG_PSTORE_LZO_COMPRESS | |
35 | #include <linux/lzo.h> | |
36 | #endif | |
37 | #ifdef CONFIG_PSTORE_LZ4_COMPRESS | |
38 | #include <linux/lz4.h> | |
39 | #endif | |
ca01d6dd | 40 | #include <linux/string.h> |
6dda9266 | 41 | #include <linux/timer.h> |
ca01d6dd TL |
42 | #include <linux/slab.h> |
43 | #include <linux/uaccess.h> | |
abd4d558 | 44 | #include <linux/hardirq.h> |
a3f5f075 | 45 | #include <linux/jiffies.h> |
6dda9266 | 46 | #include <linux/workqueue.h> |
ca01d6dd TL |
47 | |
48 | #include "internal.h" | |
49 | ||
6dda9266 TL |
50 | /* |
51 | * We defer making "oops" entries appear in pstore - see | |
52 | * whether the system is actually still running well enough | |
53 | * to let someone see the entry | |
54 | */ | |
521f7288 | 55 | static int pstore_update_ms = -1; |
a3f5f075 AV |
56 | module_param_named(update_ms, pstore_update_ms, int, 0600); |
57 | MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content " | |
521f7288 AV |
58 | "(default is -1, which means runtime updates are disabled; " |
59 | "enabling this option is not safe, it may lead to further " | |
60 | "corruption on Oopses)"); | |
6dda9266 TL |
61 | |
62 | static int pstore_new_entry; | |
63 | ||
24ed960a | 64 | static void pstore_timefunc(struct timer_list *); |
1d27e3e2 | 65 | static DEFINE_TIMER(pstore_timer, pstore_timefunc); |
6dda9266 TL |
66 | |
67 | static void pstore_dowork(struct work_struct *); | |
68 | static DECLARE_WORK(pstore_work, pstore_dowork); | |
69 | ||
ca01d6dd TL |
70 | /* |
71 | * pstore_lock just protects "psinfo" during | |
72 | * calls to pstore_register() | |
73 | */ | |
74 | static DEFINE_SPINLOCK(pstore_lock); | |
060287b8 | 75 | struct pstore_info *psinfo; |
ca01d6dd | 76 | |
dee28e72 MG |
77 | static char *backend; |
78 | ||
b0aad7a9 | 79 | /* Compression parameters */ |
8cfc8ddc | 80 | #ifdef CONFIG_PSTORE_ZLIB_COMPRESS |
b0aad7a9 AB |
81 | #define COMPR_LEVEL 6 |
82 | #define WINDOW_BITS 12 | |
83 | #define MEM_LEVEL 4 | |
84 | static struct z_stream_s stream; | |
8cfc8ddc GT |
85 | #else |
86 | static unsigned char *workspace; | |
87 | #endif | |
88 | ||
89 | struct pstore_zbackend { | |
90 | int (*compress)(const void *in, void *out, size_t inlen, size_t outlen); | |
91 | int (*decompress)(void *in, void *out, size_t inlen, size_t outlen); | |
92 | void (*allocate)(void); | |
93 | void (*free)(void); | |
94 | ||
95 | const char *name; | |
96 | }; | |
b0aad7a9 AB |
97 | |
98 | static char *big_oops_buf; | |
99 | static size_t big_oops_buf_sz; | |
100 | ||
366f7e7a | 101 | /* How much of the console log to snapshot */ |
349d7438 | 102 | unsigned long kmsg_bytes = PSTORE_DEFAULT_KMSG_BYTES; |
ca01d6dd | 103 | |
366f7e7a | 104 | void pstore_set_kmsg_bytes(int bytes) |
ca01d6dd | 105 | { |
366f7e7a | 106 | kmsg_bytes = bytes; |
ca01d6dd TL |
107 | } |
108 | ||
ca01d6dd TL |
109 | /* Tag each group of saved records with a sequence number */ |
110 | static int oopscount; | |
111 | ||
381b872c SA |
112 | static const char *get_reason_str(enum kmsg_dump_reason reason) |
113 | { | |
114 | switch (reason) { | |
115 | case KMSG_DUMP_PANIC: | |
116 | return "Panic"; | |
117 | case KMSG_DUMP_OOPS: | |
118 | return "Oops"; | |
119 | case KMSG_DUMP_EMERG: | |
120 | return "Emergency"; | |
121 | case KMSG_DUMP_RESTART: | |
122 | return "Restart"; | |
123 | case KMSG_DUMP_HALT: | |
124 | return "Halt"; | |
125 | case KMSG_DUMP_POWEROFF: | |
126 | return "Poweroff"; | |
127 | default: | |
128 | return "Unknown"; | |
129 | } | |
130 | } | |
9f6af27f | 131 | |
dca6d5e2 KC |
132 | /* |
133 | * Should pstore_dump() wait for a concurrent pstore_dump()? If | |
134 | * not, the current pstore_dump() will report a failure to dump | |
135 | * and return. | |
136 | */ | |
137 | static bool pstore_cannot_wait(enum kmsg_dump_reason reason) | |
9f244e9c | 138 | { |
dca6d5e2 | 139 | /* In NMI path, pstore shouldn't block regardless of reason. */ |
9f244e9c SA |
140 | if (in_nmi()) |
141 | return true; | |
142 | ||
143 | switch (reason) { | |
144 | /* In panic case, other cpus are stopped by smp_send_stop(). */ | |
145 | case KMSG_DUMP_PANIC: | |
dca6d5e2 | 146 | /* Emergency restart shouldn't be blocked. */ |
9f244e9c SA |
147 | case KMSG_DUMP_EMERG: |
148 | return true; | |
149 | default: | |
150 | return false; | |
151 | } | |
152 | } | |
9f244e9c | 153 | |
8cfc8ddc | 154 | #ifdef CONFIG_PSTORE_ZLIB_COMPRESS |
b0aad7a9 | 155 | /* Derived from logfs_compress() */ |
8cfc8ddc | 156 | static int compress_zlib(const void *in, void *out, size_t inlen, size_t outlen) |
b0aad7a9 AB |
157 | { |
158 | int err, ret; | |
159 | ||
160 | ret = -EIO; | |
161 | err = zlib_deflateInit2(&stream, COMPR_LEVEL, Z_DEFLATED, WINDOW_BITS, | |
162 | MEM_LEVEL, Z_DEFAULT_STRATEGY); | |
163 | if (err != Z_OK) | |
164 | goto error; | |
165 | ||
166 | stream.next_in = in; | |
167 | stream.avail_in = inlen; | |
168 | stream.total_in = 0; | |
169 | stream.next_out = out; | |
170 | stream.avail_out = outlen; | |
171 | stream.total_out = 0; | |
172 | ||
173 | err = zlib_deflate(&stream, Z_FINISH); | |
174 | if (err != Z_STREAM_END) | |
175 | goto error; | |
176 | ||
177 | err = zlib_deflateEnd(&stream); | |
178 | if (err != Z_OK) | |
179 | goto error; | |
180 | ||
181 | if (stream.total_out >= stream.total_in) | |
182 | goto error; | |
183 | ||
184 | ret = stream.total_out; | |
185 | error: | |
186 | return ret; | |
187 | } | |
188 | ||
adb42f5e | 189 | /* Derived from logfs_uncompress */ |
8cfc8ddc | 190 | static int decompress_zlib(void *in, void *out, size_t inlen, size_t outlen) |
adb42f5e AB |
191 | { |
192 | int err, ret; | |
193 | ||
194 | ret = -EIO; | |
b61edf8e | 195 | err = zlib_inflateInit2(&stream, WINDOW_BITS); |
adb42f5e AB |
196 | if (err != Z_OK) |
197 | goto error; | |
198 | ||
199 | stream.next_in = in; | |
200 | stream.avail_in = inlen; | |
201 | stream.total_in = 0; | |
202 | stream.next_out = out; | |
203 | stream.avail_out = outlen; | |
204 | stream.total_out = 0; | |
205 | ||
206 | err = zlib_inflate(&stream, Z_FINISH); | |
207 | if (err != Z_STREAM_END) | |
208 | goto error; | |
209 | ||
210 | err = zlib_inflateEnd(&stream); | |
211 | if (err != Z_OK) | |
212 | goto error; | |
213 | ||
214 | ret = stream.total_out; | |
215 | error: | |
216 | return ret; | |
217 | } | |
218 | ||
8cfc8ddc | 219 | static void allocate_zlib(void) |
b0aad7a9 AB |
220 | { |
221 | size_t size; | |
7de8fe2f AB |
222 | size_t cmpr; |
223 | ||
224 | switch (psinfo->bufsize) { | |
225 | /* buffer range for efivars */ | |
226 | case 1000 ... 2000: | |
227 | cmpr = 56; | |
228 | break; | |
229 | case 2001 ... 3000: | |
230 | cmpr = 54; | |
231 | break; | |
232 | case 3001 ... 3999: | |
233 | cmpr = 52; | |
234 | break; | |
235 | /* buffer range for nvram, erst */ | |
236 | case 4000 ... 10000: | |
237 | cmpr = 45; | |
238 | break; | |
239 | default: | |
240 | cmpr = 60; | |
241 | break; | |
242 | } | |
b0aad7a9 | 243 | |
7de8fe2f | 244 | big_oops_buf_sz = (psinfo->bufsize * 100) / cmpr; |
b0aad7a9 AB |
245 | big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL); |
246 | if (big_oops_buf) { | |
247 | size = max(zlib_deflate_workspacesize(WINDOW_BITS, MEM_LEVEL), | |
248 | zlib_inflate_workspacesize()); | |
249 | stream.workspace = kmalloc(size, GFP_KERNEL); | |
250 | if (!stream.workspace) { | |
ef748853 | 251 | pr_err("No memory for compression workspace; skipping compression\n"); |
b0aad7a9 AB |
252 | kfree(big_oops_buf); |
253 | big_oops_buf = NULL; | |
254 | } | |
255 | } else { | |
ef748853 | 256 | pr_err("No memory for uncompressed data; skipping compression\n"); |
b0aad7a9 AB |
257 | stream.workspace = NULL; |
258 | } | |
259 | ||
260 | } | |
261 | ||
8cfc8ddc | 262 | static void free_zlib(void) |
ee1d2674 GT |
263 | { |
264 | kfree(stream.workspace); | |
265 | stream.workspace = NULL; | |
266 | kfree(big_oops_buf); | |
267 | big_oops_buf = NULL; | |
8cfc8ddc GT |
268 | big_oops_buf_sz = 0; |
269 | } | |
270 | ||
3faf9354 | 271 | static const struct pstore_zbackend backend_zlib = { |
8cfc8ddc GT |
272 | .compress = compress_zlib, |
273 | .decompress = decompress_zlib, | |
274 | .allocate = allocate_zlib, | |
275 | .free = free_zlib, | |
276 | .name = "zlib", | |
277 | }; | |
278 | #endif | |
279 | ||
280 | #ifdef CONFIG_PSTORE_LZO_COMPRESS | |
281 | static int compress_lzo(const void *in, void *out, size_t inlen, size_t outlen) | |
282 | { | |
283 | int ret; | |
284 | ||
285 | ret = lzo1x_1_compress(in, inlen, out, &outlen, workspace); | |
286 | if (ret != LZO_E_OK) { | |
287 | pr_err("lzo_compress error, ret = %d!\n", ret); | |
288 | return -EIO; | |
289 | } | |
290 | ||
291 | return outlen; | |
292 | } | |
293 | ||
294 | static int decompress_lzo(void *in, void *out, size_t inlen, size_t outlen) | |
295 | { | |
296 | int ret; | |
297 | ||
298 | ret = lzo1x_decompress_safe(in, inlen, out, &outlen); | |
299 | if (ret != LZO_E_OK) { | |
300 | pr_err("lzo_decompress error, ret = %d!\n", ret); | |
301 | return -EIO; | |
302 | } | |
303 | ||
304 | return outlen; | |
305 | } | |
306 | ||
307 | static void allocate_lzo(void) | |
308 | { | |
309 | big_oops_buf_sz = lzo1x_worst_compress(psinfo->bufsize); | |
310 | big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL); | |
311 | if (big_oops_buf) { | |
312 | workspace = kmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL); | |
313 | if (!workspace) { | |
314 | pr_err("No memory for compression workspace; skipping compression\n"); | |
315 | kfree(big_oops_buf); | |
316 | big_oops_buf = NULL; | |
317 | } | |
318 | } else { | |
319 | pr_err("No memory for uncompressed data; skipping compression\n"); | |
320 | workspace = NULL; | |
321 | } | |
322 | } | |
323 | ||
324 | static void free_lzo(void) | |
325 | { | |
326 | kfree(workspace); | |
327 | kfree(big_oops_buf); | |
328 | big_oops_buf = NULL; | |
329 | big_oops_buf_sz = 0; | |
330 | } | |
331 | ||
3faf9354 | 332 | static const struct pstore_zbackend backend_lzo = { |
8cfc8ddc GT |
333 | .compress = compress_lzo, |
334 | .decompress = decompress_lzo, | |
335 | .allocate = allocate_lzo, | |
336 | .free = free_lzo, | |
337 | .name = "lzo", | |
338 | }; | |
339 | #endif | |
340 | ||
341 | #ifdef CONFIG_PSTORE_LZ4_COMPRESS | |
342 | static int compress_lz4(const void *in, void *out, size_t inlen, size_t outlen) | |
343 | { | |
344 | int ret; | |
345 | ||
d21b5ff1 SS |
346 | ret = LZ4_compress_default(in, out, inlen, outlen, workspace); |
347 | if (!ret) { | |
348 | pr_err("LZ4_compress_default error; compression failed!\n"); | |
8cfc8ddc GT |
349 | return -EIO; |
350 | } | |
351 | ||
d21b5ff1 | 352 | return ret; |
8cfc8ddc GT |
353 | } |
354 | ||
355 | static int decompress_lz4(void *in, void *out, size_t inlen, size_t outlen) | |
356 | { | |
357 | int ret; | |
358 | ||
d21b5ff1 SS |
359 | ret = LZ4_decompress_safe(in, out, inlen, outlen); |
360 | if (ret < 0) { | |
361 | /* | |
362 | * LZ4_decompress_safe will return an error code | |
363 | * (< 0) if decompression failed | |
364 | */ | |
365 | pr_err("LZ4_decompress_safe error, ret = %d!\n", ret); | |
8cfc8ddc GT |
366 | return -EIO; |
367 | } | |
368 | ||
d21b5ff1 | 369 | return ret; |
8cfc8ddc GT |
370 | } |
371 | ||
372 | static void allocate_lz4(void) | |
373 | { | |
d21b5ff1 | 374 | big_oops_buf_sz = LZ4_compressBound(psinfo->bufsize); |
8cfc8ddc GT |
375 | big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL); |
376 | if (big_oops_buf) { | |
377 | workspace = kmalloc(LZ4_MEM_COMPRESS, GFP_KERNEL); | |
378 | if (!workspace) { | |
379 | pr_err("No memory for compression workspace; skipping compression\n"); | |
380 | kfree(big_oops_buf); | |
381 | big_oops_buf = NULL; | |
382 | } | |
383 | } else { | |
384 | pr_err("No memory for uncompressed data; skipping compression\n"); | |
385 | workspace = NULL; | |
386 | } | |
387 | } | |
388 | ||
389 | static void free_lz4(void) | |
390 | { | |
391 | kfree(workspace); | |
392 | kfree(big_oops_buf); | |
393 | big_oops_buf = NULL; | |
394 | big_oops_buf_sz = 0; | |
395 | } | |
396 | ||
3faf9354 | 397 | static const struct pstore_zbackend backend_lz4 = { |
8cfc8ddc GT |
398 | .compress = compress_lz4, |
399 | .decompress = decompress_lz4, | |
400 | .allocate = allocate_lz4, | |
401 | .free = free_lz4, | |
402 | .name = "lz4", | |
403 | }; | |
404 | #endif | |
405 | ||
3faf9354 | 406 | static const struct pstore_zbackend *zbackend = |
8cfc8ddc GT |
407 | #if defined(CONFIG_PSTORE_ZLIB_COMPRESS) |
408 | &backend_zlib; | |
409 | #elif defined(CONFIG_PSTORE_LZO_COMPRESS) | |
410 | &backend_lzo; | |
411 | #elif defined(CONFIG_PSTORE_LZ4_COMPRESS) | |
412 | &backend_lz4; | |
413 | #else | |
414 | NULL; | |
415 | #endif | |
416 | ||
417 | static int pstore_compress(const void *in, void *out, | |
418 | size_t inlen, size_t outlen) | |
419 | { | |
420 | if (zbackend) | |
421 | return zbackend->compress(in, out, inlen, outlen); | |
422 | else | |
423 | return -EIO; | |
424 | } | |
425 | ||
426 | static int pstore_decompress(void *in, void *out, size_t inlen, size_t outlen) | |
427 | { | |
428 | if (zbackend) | |
429 | return zbackend->decompress(in, out, inlen, outlen); | |
430 | else | |
431 | return -EIO; | |
432 | } | |
433 | ||
434 | static void allocate_buf_for_compression(void) | |
435 | { | |
436 | if (zbackend) { | |
437 | pr_info("using %s compression\n", zbackend->name); | |
438 | zbackend->allocate(); | |
439 | } else { | |
440 | pr_err("allocate compression buffer error!\n"); | |
441 | } | |
442 | } | |
443 | ||
444 | static void free_buf_for_compression(void) | |
445 | { | |
446 | if (zbackend) | |
447 | zbackend->free(); | |
448 | else | |
449 | pr_err("free compression buffer error!\n"); | |
ee1d2674 GT |
450 | } |
451 | ||
b0aad7a9 AB |
452 | /* |
453 | * Called when compression fails, since the printk buffer | |
454 | * would be fetched for compression calling it again when | |
455 | * compression fails would have moved the iterator of | |
456 | * printk buffer which results in fetching old contents. | |
457 | * Copy the recent messages from big_oops_buf to psinfo->buf | |
458 | */ | |
459 | static size_t copy_kmsg_to_buffer(int hsize, size_t len) | |
460 | { | |
461 | size_t total_len; | |
462 | size_t diff; | |
463 | ||
464 | total_len = hsize + len; | |
465 | ||
466 | if (total_len > psinfo->bufsize) { | |
467 | diff = total_len - psinfo->bufsize + hsize; | |
468 | memcpy(psinfo->buf, big_oops_buf, hsize); | |
469 | memcpy(psinfo->buf + hsize, big_oops_buf + diff, | |
470 | psinfo->bufsize - hsize); | |
471 | total_len = psinfo->bufsize; | |
472 | } else | |
473 | memcpy(psinfo->buf, big_oops_buf, total_len); | |
474 | ||
475 | return total_len; | |
476 | } | |
477 | ||
e581ca81 KC |
478 | void pstore_record_init(struct pstore_record *record, |
479 | struct pstore_info *psinfo) | |
480 | { | |
481 | memset(record, 0, sizeof(*record)); | |
482 | ||
483 | record->psi = psinfo; | |
c7f3c595 KC |
484 | |
485 | /* Report zeroed timestamp if called before timekeeping has resumed. */ | |
df27067e | 486 | record->time = ns_to_timespec(ktime_get_real_fast_ns()); |
e581ca81 KC |
487 | } |
488 | ||
ca01d6dd TL |
489 | /* |
490 | * callback from kmsg_dump. (s2,l2) has the most recently | |
491 | * written bytes, older bytes are in (s1,l1). Save as much | |
492 | * as we can from the end of the buffer. | |
493 | */ | |
494 | static void pstore_dump(struct kmsg_dumper *dumper, | |
e2ae715d | 495 | enum kmsg_dump_reason reason) |
ca01d6dd | 496 | { |
e2ae715d | 497 | unsigned long total = 0; |
381b872c | 498 | const char *why; |
b94fdd07 | 499 | unsigned int part = 1; |
e2ae715d | 500 | int ret; |
ca01d6dd | 501 | |
381b872c | 502 | why = get_reason_str(reason); |
9f6af27f | 503 | |
dca6d5e2 KC |
504 | if (down_trylock(&psinfo->buf_lock)) { |
505 | /* Failed to acquire lock: give up if we cannot wait. */ | |
506 | if (pstore_cannot_wait(reason)) { | |
507 | pr_err("dump skipped in %s path: may corrupt error record\n", | |
508 | in_nmi() ? "NMI" : why); | |
509 | return; | |
510 | } | |
511 | if (down_interruptible(&psinfo->buf_lock)) { | |
512 | pr_err("could not grab semaphore?!\n"); | |
959217c8 | 513 | return; |
9f244e9c | 514 | } |
98e44fda | 515 | } |
dca6d5e2 | 516 | |
ca01d6dd TL |
517 | oopscount++; |
518 | while (total < kmsg_bytes) { | |
e2ae715d | 519 | char *dst; |
76cc9580 KC |
520 | size_t dst_size; |
521 | int header_size; | |
b0aad7a9 | 522 | int zipped_len = -1; |
76cc9580 | 523 | size_t dump_size; |
e581ca81 KC |
524 | struct pstore_record record; |
525 | ||
526 | pstore_record_init(&record, psinfo); | |
527 | record.type = PSTORE_TYPE_DMESG; | |
528 | record.count = oopscount; | |
529 | record.reason = reason; | |
530 | record.part = part; | |
531 | record.buf = psinfo->buf; | |
e2ae715d | 532 | |
dca6d5e2 | 533 | if (big_oops_buf) { |
b0aad7a9 | 534 | dst = big_oops_buf; |
76cc9580 | 535 | dst_size = big_oops_buf_sz; |
235f6d15 NK |
536 | } else { |
537 | dst = psinfo->buf; | |
76cc9580 | 538 | dst_size = psinfo->bufsize; |
235f6d15 NK |
539 | } |
540 | ||
76cc9580 KC |
541 | /* Write dump header. */ |
542 | header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why, | |
543 | oopscount, part); | |
544 | dst_size -= header_size; | |
ca01d6dd | 545 | |
76cc9580 KC |
546 | /* Write dump contents. */ |
547 | if (!kmsg_dump_get_buffer(dumper, true, dst + header_size, | |
548 | dst_size, &dump_size)) | |
235f6d15 | 549 | break; |
b0aad7a9 | 550 | |
dca6d5e2 | 551 | if (big_oops_buf) { |
b0aad7a9 | 552 | zipped_len = pstore_compress(dst, psinfo->buf, |
76cc9580 KC |
553 | header_size + dump_size, |
554 | psinfo->bufsize); | |
b0aad7a9 AB |
555 | |
556 | if (zipped_len > 0) { | |
76cc9580 KC |
557 | record.compressed = true; |
558 | record.size = zipped_len; | |
b0aad7a9 | 559 | } else { |
76cc9580 KC |
560 | record.size = copy_kmsg_to_buffer(header_size, |
561 | dump_size); | |
b0aad7a9 AB |
562 | } |
563 | } else { | |
76cc9580 | 564 | record.size = header_size + dump_size; |
b0aad7a9 | 565 | } |
ca01d6dd | 566 | |
76cc9580 | 567 | ret = psinfo->write(&record); |
b238b8fa | 568 | if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted()) |
6dda9266 | 569 | pstore_new_entry = 1; |
e2ae715d | 570 | |
76cc9580 | 571 | total += record.size; |
56280682 | 572 | part++; |
ca01d6dd | 573 | } |
dca6d5e2 KC |
574 | |
575 | up(&psinfo->buf_lock); | |
ca01d6dd TL |
576 | } |
577 | ||
578 | static struct kmsg_dumper pstore_dumper = { | |
579 | .dump = pstore_dump, | |
580 | }; | |
581 | ||
306e5c2a GT |
582 | /* |
583 | * Register with kmsg_dump to save last part of console log on panic. | |
584 | */ | |
18730411 GT |
585 | static void pstore_register_kmsg(void) |
586 | { | |
587 | kmsg_dump_register(&pstore_dumper); | |
588 | } | |
589 | ||
ee1d2674 GT |
590 | static void pstore_unregister_kmsg(void) |
591 | { | |
592 | kmsg_dump_unregister(&pstore_dumper); | |
593 | } | |
594 | ||
f29e5956 AV |
595 | #ifdef CONFIG_PSTORE_CONSOLE |
596 | static void pstore_console_write(struct console *con, const char *s, unsigned c) | |
597 | { | |
3a3985d4 | 598 | struct pstore_record record; |
f29e5956 | 599 | |
3a3985d4 KC |
600 | pstore_record_init(&record, psinfo); |
601 | record.type = PSTORE_TYPE_CONSOLE; | |
80c9d03c | 602 | |
3a3985d4 KC |
603 | record.buf = (char *)s; |
604 | record.size = c; | |
605 | psinfo->write(&record); | |
f29e5956 AV |
606 | } |
607 | ||
608 | static struct console pstore_console = { | |
609 | .name = "pstore", | |
610 | .write = pstore_console_write, | |
611 | .flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME, | |
612 | .index = -1, | |
613 | }; | |
614 | ||
615 | static void pstore_register_console(void) | |
616 | { | |
617 | register_console(&pstore_console); | |
618 | } | |
ee1d2674 GT |
619 | |
620 | static void pstore_unregister_console(void) | |
621 | { | |
622 | unregister_console(&pstore_console); | |
623 | } | |
f29e5956 AV |
624 | #else |
625 | static void pstore_register_console(void) {} | |
ee1d2674 | 626 | static void pstore_unregister_console(void) {} |
f29e5956 AV |
627 | #endif |
628 | ||
4c9ec219 KC |
629 | static int pstore_write_user_compat(struct pstore_record *record, |
630 | const char __user *buf) | |
5bf6d1b9 | 631 | { |
30800d99 KC |
632 | int ret = 0; |
633 | ||
634 | if (record->buf) | |
635 | return -EINVAL; | |
636 | ||
077090af | 637 | record->buf = memdup_user(buf, record->size); |
dfd6fa39 | 638 | if (IS_ERR(record->buf)) { |
077090af | 639 | ret = PTR_ERR(record->buf); |
30800d99 | 640 | goto out; |
5bf6d1b9 | 641 | } |
30800d99 KC |
642 | |
643 | ret = record->psi->write(record); | |
644 | ||
30800d99 | 645 | kfree(record->buf); |
077090af | 646 | out: |
30800d99 KC |
647 | record->buf = NULL; |
648 | ||
649 | return unlikely(ret < 0) ? ret : record->size; | |
5bf6d1b9 MS |
650 | } |
651 | ||
ca01d6dd TL |
652 | /* |
653 | * platform specific persistent storage driver registers with | |
654 | * us here. If pstore is already mounted, call the platform | |
655 | * read function right away to populate the file system. If not | |
656 | * then the pstore mount code will call us later to fill out | |
657 | * the file system. | |
ca01d6dd TL |
658 | */ |
659 | int pstore_register(struct pstore_info *psi) | |
660 | { | |
661 | struct module *owner = psi->owner; | |
662 | ||
0d7cd09a KC |
663 | if (backend && strcmp(backend, psi->name)) { |
664 | pr_warn("ignoring unexpected backend '%s'\n", psi->name); | |
8e48b1a8 | 665 | return -EPERM; |
0d7cd09a | 666 | } |
8e48b1a8 | 667 | |
4c9ec219 KC |
668 | /* Sanity check flags. */ |
669 | if (!psi->flags) { | |
670 | pr_warn("backend '%s' must support at least one frontend\n", | |
671 | psi->name); | |
672 | return -EINVAL; | |
673 | } | |
674 | ||
675 | /* Check for required functions. */ | |
676 | if (!psi->read || !psi->write) { | |
677 | pr_warn("backend '%s' must implement read() and write()\n", | |
678 | psi->name); | |
679 | return -EINVAL; | |
680 | } | |
681 | ||
ca01d6dd TL |
682 | spin_lock(&pstore_lock); |
683 | if (psinfo) { | |
0d7cd09a KC |
684 | pr_warn("backend '%s' already loaded: ignoring '%s'\n", |
685 | psinfo->name, psi->name); | |
ca01d6dd TL |
686 | spin_unlock(&pstore_lock); |
687 | return -EBUSY; | |
688 | } | |
dee28e72 | 689 | |
4c9ec219 KC |
690 | if (!psi->write_user) |
691 | psi->write_user = pstore_write_user_compat; | |
ca01d6dd | 692 | psinfo = psi; |
f6f82851 | 693 | mutex_init(&psinfo->read_mutex); |
dca6d5e2 | 694 | sema_init(&psinfo->buf_lock, 1); |
ca01d6dd TL |
695 | spin_unlock(&pstore_lock); |
696 | ||
697 | if (owner && !try_module_get(owner)) { | |
698 | psinfo = NULL; | |
699 | return -EINVAL; | |
700 | } | |
701 | ||
b0aad7a9 AB |
702 | allocate_buf_for_compression(); |
703 | ||
ca01d6dd | 704 | if (pstore_is_mounted()) |
6dda9266 | 705 | pstore_get_records(0); |
ca01d6dd | 706 | |
c950fd6f NK |
707 | if (psi->flags & PSTORE_FLAGS_DMESG) |
708 | pstore_register_kmsg(); | |
709 | if (psi->flags & PSTORE_FLAGS_CONSOLE) | |
df36ac1b | 710 | pstore_register_console(); |
c950fd6f | 711 | if (psi->flags & PSTORE_FLAGS_FTRACE) |
df36ac1b | 712 | pstore_register_ftrace(); |
c950fd6f | 713 | if (psi->flags & PSTORE_FLAGS_PMSG) |
9d5438f4 | 714 | pstore_register_pmsg(); |
ca01d6dd | 715 | |
6330d553 | 716 | /* Start watching for new records, if desired. */ |
a3f5f075 AV |
717 | if (pstore_update_ms >= 0) { |
718 | pstore_timer.expires = jiffies + | |
719 | msecs_to_jiffies(pstore_update_ms); | |
720 | add_timer(&pstore_timer); | |
721 | } | |
6dda9266 | 722 | |
42222c2a WL |
723 | /* |
724 | * Update the module parameter backend, so it is visible | |
725 | * through /sys/module/pstore/parameters/backend | |
726 | */ | |
727 | backend = psi->name; | |
728 | ||
ef748853 | 729 | pr_info("Registered %s as persistent store backend\n", psi->name); |
8e48b1a8 | 730 | |
1344dd86 KC |
731 | module_put(owner); |
732 | ||
ca01d6dd TL |
733 | return 0; |
734 | } | |
735 | EXPORT_SYMBOL_GPL(pstore_register); | |
736 | ||
ee1d2674 GT |
737 | void pstore_unregister(struct pstore_info *psi) |
738 | { | |
6330d553 KC |
739 | /* Stop timer and make sure all work has finished. */ |
740 | pstore_update_ms = -1; | |
741 | del_timer_sync(&pstore_timer); | |
742 | flush_work(&pstore_work); | |
743 | ||
c950fd6f | 744 | if (psi->flags & PSTORE_FLAGS_PMSG) |
a1db8060 | 745 | pstore_unregister_pmsg(); |
c950fd6f | 746 | if (psi->flags & PSTORE_FLAGS_FTRACE) |
a1db8060 | 747 | pstore_unregister_ftrace(); |
c950fd6f | 748 | if (psi->flags & PSTORE_FLAGS_CONSOLE) |
a1db8060 | 749 | pstore_unregister_console(); |
c950fd6f NK |
750 | if (psi->flags & PSTORE_FLAGS_DMESG) |
751 | pstore_unregister_kmsg(); | |
ee1d2674 GT |
752 | |
753 | free_buf_for_compression(); | |
754 | ||
755 | psinfo = NULL; | |
756 | backend = NULL; | |
757 | } | |
758 | EXPORT_SYMBOL_GPL(pstore_unregister); | |
759 | ||
634f8f51 KC |
760 | static void decompress_record(struct pstore_record *record) |
761 | { | |
762 | int unzipped_len; | |
7e8cc8dc | 763 | char *decompressed; |
634f8f51 | 764 | |
4a16d1cb AK |
765 | if (!record->compressed) |
766 | return; | |
767 | ||
634f8f51 | 768 | /* Only PSTORE_TYPE_DMESG support compression. */ |
4a16d1cb | 769 | if (record->type != PSTORE_TYPE_DMESG) { |
634f8f51 KC |
770 | pr_warn("ignored compressed record type %d\n", record->type); |
771 | return; | |
772 | } | |
773 | ||
774 | /* No compression method has created the common buffer. */ | |
775 | if (!big_oops_buf) { | |
776 | pr_warn("no decompression buffer allocated\n"); | |
777 | return; | |
778 | } | |
779 | ||
780 | unzipped_len = pstore_decompress(record->buf, big_oops_buf, | |
781 | record->size, big_oops_buf_sz); | |
7e8cc8dc | 782 | if (unzipped_len <= 0) { |
634f8f51 | 783 | pr_err("decompression failed: %d\n", unzipped_len); |
7e8cc8dc KC |
784 | return; |
785 | } | |
786 | ||
787 | /* Build new buffer for decompressed contents. */ | |
788 | decompressed = kmalloc(unzipped_len + record->ecc_notice_size, | |
789 | GFP_KERNEL); | |
790 | if (!decompressed) { | |
791 | pr_err("decompression ran out of memory\n"); | |
792 | return; | |
793 | } | |
794 | memcpy(decompressed, big_oops_buf, unzipped_len); | |
795 | ||
796 | /* Append ECC notice to decompressed buffer. */ | |
797 | memcpy(decompressed + unzipped_len, record->buf + record->size, | |
798 | record->ecc_notice_size); | |
799 | ||
800 | /* Swap out compresed contents with decompressed contents. */ | |
801 | kfree(record->buf); | |
802 | record->buf = decompressed; | |
803 | record->size = unzipped_len; | |
804 | record->compressed = false; | |
634f8f51 KC |
805 | } |
806 | ||
ca01d6dd | 807 | /* |
3a7d2fd1 | 808 | * Read all the records from one persistent store backend. Create |
6dda9266 TL |
809 | * files in our filesystem. Don't warn about -EEXIST errors |
810 | * when we are re-scanning the backing store looking to add new | |
811 | * error records. | |
ca01d6dd | 812 | */ |
3a7d2fd1 KC |
813 | void pstore_get_backend_records(struct pstore_info *psi, |
814 | struct dentry *root, int quiet) | |
ca01d6dd | 815 | { |
2a2b0acf | 816 | int failed = 0; |
656de42e | 817 | unsigned int stop_loop = 65536; |
ca01d6dd | 818 | |
3a7d2fd1 | 819 | if (!psi || !root) |
ca01d6dd TL |
820 | return; |
821 | ||
f6f82851 | 822 | mutex_lock(&psi->read_mutex); |
2174f6df | 823 | if (psi->open && psi->open(psi)) |
06cf91b4 CG |
824 | goto out; |
825 | ||
1dfff7dd KC |
826 | /* |
827 | * Backend callback read() allocates record.buf. decompress_record() | |
828 | * may reallocate record.buf. On success, pstore_mkfile() will keep | |
829 | * the record.buf, so free it only on failure. | |
830 | */ | |
656de42e | 831 | for (; stop_loop; stop_loop--) { |
2a2b0acf KC |
832 | struct pstore_record *record; |
833 | int rc; | |
834 | ||
835 | record = kzalloc(sizeof(*record), GFP_KERNEL); | |
836 | if (!record) { | |
837 | pr_err("out of memory creating record\n"); | |
838 | break; | |
839 | } | |
e581ca81 | 840 | pstore_record_init(record, psi); |
2a2b0acf KC |
841 | |
842 | record->size = psi->read(record); | |
843 | ||
844 | /* No more records left in backend? */ | |
f6525b96 DA |
845 | if (record->size <= 0) { |
846 | kfree(record); | |
2a2b0acf | 847 | break; |
f6525b96 | 848 | } |
2a2b0acf KC |
849 | |
850 | decompress_record(record); | |
3a7d2fd1 | 851 | rc = pstore_mkfile(root, record); |
1dfff7dd | 852 | if (rc) { |
83f70f07 | 853 | /* pstore_mkfile() did not take record, so free it. */ |
2a2b0acf | 854 | kfree(record->buf); |
83f70f07 | 855 | kfree(record); |
1dfff7dd KC |
856 | if (rc != -EEXIST || !quiet) |
857 | failed++; | |
858 | } | |
ca01d6dd | 859 | } |
2174f6df KC |
860 | if (psi->close) |
861 | psi->close(psi); | |
06cf91b4 | 862 | out: |
f6f82851 | 863 | mutex_unlock(&psi->read_mutex); |
ca01d6dd TL |
864 | |
865 | if (failed) | |
656de42e | 866 | pr_warn("failed to create %d record(s) from '%s'\n", |
ef748853 | 867 | failed, psi->name); |
656de42e KC |
868 | if (!stop_loop) |
869 | pr_err("looping? Too many records seen from '%s'\n", | |
870 | psi->name); | |
ca01d6dd TL |
871 | } |
872 | ||
6dda9266 TL |
873 | static void pstore_dowork(struct work_struct *work) |
874 | { | |
875 | pstore_get_records(1); | |
876 | } | |
877 | ||
24ed960a | 878 | static void pstore_timefunc(struct timer_list *unused) |
6dda9266 TL |
879 | { |
880 | if (pstore_new_entry) { | |
881 | pstore_new_entry = 0; | |
882 | schedule_work(&pstore_work); | |
883 | } | |
884 | ||
6330d553 KC |
885 | if (pstore_update_ms >= 0) |
886 | mod_timer(&pstore_timer, | |
887 | jiffies + msecs_to_jiffies(pstore_update_ms)); | |
6dda9266 TL |
888 | } |
889 | ||
dee28e72 MG |
890 | module_param(backend, charp, 0444); |
891 | MODULE_PARM_DESC(backend, "Pstore backend to use"); |