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