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