]> git.ipfire.org Git - people/ms/linux.git/blame - fs/pstore/platform.c
pstore/platform: Switch pstore_info::name to const
[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,
f29e5956
AV
519 .index = -1,
520};
521
522static void pstore_register_console(void)
523{
b7753fc7
KC
524 /*
525 * Always initialize flags here since prior unregister_console()
526 * calls may have changed settings (specifically CON_ENABLED).
527 */
528 pstore_console.flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME;
f29e5956
AV
529 register_console(&pstore_console);
530}
ee1d2674
GT
531
532static void pstore_unregister_console(void)
533{
534 unregister_console(&pstore_console);
535}
f29e5956
AV
536#else
537static void pstore_register_console(void) {}
ee1d2674 538static void pstore_unregister_console(void) {}
f29e5956
AV
539#endif
540
4c9ec219
KC
541static int pstore_write_user_compat(struct pstore_record *record,
542 const char __user *buf)
5bf6d1b9 543{
30800d99
KC
544 int ret = 0;
545
546 if (record->buf)
547 return -EINVAL;
548
077090af 549 record->buf = memdup_user(buf, record->size);
dfd6fa39 550 if (IS_ERR(record->buf)) {
077090af 551 ret = PTR_ERR(record->buf);
30800d99 552 goto out;
5bf6d1b9 553 }
30800d99
KC
554
555 ret = record->psi->write(record);
556
30800d99 557 kfree(record->buf);
077090af 558out:
30800d99
KC
559 record->buf = NULL;
560
561 return unlikely(ret < 0) ? ret : record->size;
5bf6d1b9
MS
562}
563
ca01d6dd
TL
564/*
565 * platform specific persistent storage driver registers with
566 * us here. If pstore is already mounted, call the platform
567 * read function right away to populate the file system. If not
568 * then the pstore mount code will call us later to fill out
569 * the file system.
ca01d6dd
TL
570 */
571int pstore_register(struct pstore_info *psi)
572{
0d7cd09a
KC
573 if (backend && strcmp(backend, psi->name)) {
574 pr_warn("ignoring unexpected backend '%s'\n", psi->name);
8e48b1a8 575 return -EPERM;
0d7cd09a 576 }
8e48b1a8 577
4c9ec219
KC
578 /* Sanity check flags. */
579 if (!psi->flags) {
580 pr_warn("backend '%s' must support at least one frontend\n",
581 psi->name);
582 return -EINVAL;
583 }
584
585 /* Check for required functions. */
586 if (!psi->read || !psi->write) {
587 pr_warn("backend '%s' must implement read() and write()\n",
588 psi->name);
589 return -EINVAL;
590 }
591
cab12fd0 592 mutex_lock(&psinfo_lock);
ca01d6dd 593 if (psinfo) {
0d7cd09a
KC
594 pr_warn("backend '%s' already loaded: ignoring '%s'\n",
595 psinfo->name, psi->name);
cab12fd0 596 mutex_unlock(&psinfo_lock);
ca01d6dd
TL
597 return -EBUSY;
598 }
dee28e72 599
4c9ec219
KC
600 if (!psi->write_user)
601 psi->write_user = pstore_write_user_compat;
ca01d6dd 602 psinfo = psi;
f6f82851 603 mutex_init(&psinfo->read_mutex);
ea84b580 604 sema_init(&psinfo->buf_lock, 1);
ca01d6dd 605
8880fa32
KC
606 if (psi->flags & PSTORE_FLAGS_DMESG)
607 allocate_buf_for_compression();
b0aad7a9 608
27e5041a 609 pstore_get_records(0);
ca01d6dd 610
c950fd6f
NK
611 if (psi->flags & PSTORE_FLAGS_DMESG)
612 pstore_register_kmsg();
613 if (psi->flags & PSTORE_FLAGS_CONSOLE)
df36ac1b 614 pstore_register_console();
c950fd6f 615 if (psi->flags & PSTORE_FLAGS_FTRACE)
df36ac1b 616 pstore_register_ftrace();
c950fd6f 617 if (psi->flags & PSTORE_FLAGS_PMSG)
9d5438f4 618 pstore_register_pmsg();
ca01d6dd 619
6330d553 620 /* Start watching for new records, if desired. */
78c83c82 621 pstore_timer_kick();
6dda9266 622
42222c2a
WL
623 /*
624 * Update the module parameter backend, so it is visible
625 * through /sys/module/pstore/parameters/backend
626 */
563ca40d 627 backend = kstrdup(psi->name, GFP_KERNEL);
42222c2a 628
ef748853 629 pr_info("Registered %s as persistent store backend\n", psi->name);
8e48b1a8 630
6248a066 631 mutex_unlock(&psinfo_lock);
ca01d6dd
TL
632 return 0;
633}
634EXPORT_SYMBOL_GPL(pstore_register);
635
ee1d2674
GT
636void pstore_unregister(struct pstore_info *psi)
637{
6248a066
KC
638 /* It's okay to unregister nothing. */
639 if (!psi)
640 return;
641
642 mutex_lock(&psinfo_lock);
643
644 /* Only one backend can be registered at a time. */
645 if (WARN_ON(psi != psinfo)) {
646 mutex_unlock(&psinfo_lock);
647 return;
648 }
649
78c83c82 650 /* Unregister all callbacks. */
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 659
78c83c82
KC
660 /* Stop timer and make sure all work has finished. */
661 del_timer_sync(&pstore_timer);
662 flush_work(&pstore_work);
663
609e28bb
KC
664 /* Remove all backend records from filesystem tree. */
665 pstore_put_backend_records(psi);
666
ee1d2674
GT
667 free_buf_for_compression();
668
669 psinfo = NULL;
563ca40d 670 kfree(backend);
ee1d2674 671 backend = NULL;
6248a066 672 mutex_unlock(&psinfo_lock);
ee1d2674
GT
673}
674EXPORT_SYMBOL_GPL(pstore_unregister);
675
634f8f51
KC
676static void decompress_record(struct pstore_record *record)
677{
bdabc8e7 678 int ret;
634f8f51 679 int unzipped_len;
bdabc8e7 680 char *unzipped, *workspace;
634f8f51 681
4a16d1cb
AK
682 if (!record->compressed)
683 return;
684
634f8f51 685 /* Only PSTORE_TYPE_DMESG support compression. */
4a16d1cb 686 if (record->type != PSTORE_TYPE_DMESG) {
634f8f51
KC
687 pr_warn("ignored compressed record type %d\n", record->type);
688 return;
689 }
690
bdabc8e7 691 /* Missing compression buffer means compression was not initialized. */
634f8f51 692 if (!big_oops_buf) {
bdabc8e7 693 pr_warn("no decompression method initialized!\n");
634f8f51
KC
694 return;
695 }
696
bdabc8e7
KC
697 /* Allocate enough space to hold max decompression and ECC. */
698 unzipped_len = big_oops_buf_sz;
699 workspace = kmalloc(unzipped_len + record->ecc_notice_size,
700 GFP_KERNEL);
701 if (!workspace)
7e8cc8dc 702 return;
7e8cc8dc 703
bdabc8e7
KC
704 /* After decompression "unzipped_len" is almost certainly smaller. */
705 ret = crypto_comp_decompress(tfm, record->buf, record->size,
706 workspace, &unzipped_len);
707 if (ret) {
708 pr_err("crypto_comp_decompress failed, ret = %d!\n", ret);
709 kfree(workspace);
7e8cc8dc
KC
710 return;
711 }
7e8cc8dc
KC
712
713 /* Append ECC notice to decompressed buffer. */
bdabc8e7 714 memcpy(workspace + unzipped_len, record->buf + record->size,
7e8cc8dc
KC
715 record->ecc_notice_size);
716
bdabc8e7
KC
717 /* Copy decompressed contents into an minimum-sized allocation. */
718 unzipped = kmemdup(workspace, unzipped_len + record->ecc_notice_size,
719 GFP_KERNEL);
720 kfree(workspace);
721 if (!unzipped)
722 return;
723
724 /* Swap out compressed contents with decompressed contents. */
7e8cc8dc 725 kfree(record->buf);
bdabc8e7 726 record->buf = unzipped;
7e8cc8dc
KC
727 record->size = unzipped_len;
728 record->compressed = false;
634f8f51
KC
729}
730
ca01d6dd 731/*
3a7d2fd1 732 * Read all the records from one persistent store backend. Create
6dda9266
TL
733 * files in our filesystem. Don't warn about -EEXIST errors
734 * when we are re-scanning the backing store looking to add new
735 * error records.
ca01d6dd 736 */
3a7d2fd1
KC
737void pstore_get_backend_records(struct pstore_info *psi,
738 struct dentry *root, int quiet)
ca01d6dd 739{
2a2b0acf 740 int failed = 0;
656de42e 741 unsigned int stop_loop = 65536;
ca01d6dd 742
3a7d2fd1 743 if (!psi || !root)
ca01d6dd
TL
744 return;
745
f6f82851 746 mutex_lock(&psi->read_mutex);
2174f6df 747 if (psi->open && psi->open(psi))
06cf91b4
CG
748 goto out;
749
1dfff7dd
KC
750 /*
751 * Backend callback read() allocates record.buf. decompress_record()
752 * may reallocate record.buf. On success, pstore_mkfile() will keep
753 * the record.buf, so free it only on failure.
754 */
656de42e 755 for (; stop_loop; stop_loop--) {
2a2b0acf
KC
756 struct pstore_record *record;
757 int rc;
758
759 record = kzalloc(sizeof(*record), GFP_KERNEL);
760 if (!record) {
761 pr_err("out of memory creating record\n");
762 break;
763 }
e581ca81 764 pstore_record_init(record, psi);
2a2b0acf
KC
765
766 record->size = psi->read(record);
767
768 /* No more records left in backend? */
f6525b96
DA
769 if (record->size <= 0) {
770 kfree(record);
2a2b0acf 771 break;
f6525b96 772 }
2a2b0acf
KC
773
774 decompress_record(record);
3a7d2fd1 775 rc = pstore_mkfile(root, record);
1dfff7dd 776 if (rc) {
83f70f07 777 /* pstore_mkfile() did not take record, so free it. */
2a2b0acf 778 kfree(record->buf);
83f70f07 779 kfree(record);
1dfff7dd
KC
780 if (rc != -EEXIST || !quiet)
781 failed++;
782 }
ca01d6dd 783 }
2174f6df
KC
784 if (psi->close)
785 psi->close(psi);
06cf91b4 786out:
f6f82851 787 mutex_unlock(&psi->read_mutex);
ca01d6dd
TL
788
789 if (failed)
656de42e 790 pr_warn("failed to create %d record(s) from '%s'\n",
ef748853 791 failed, psi->name);
656de42e
KC
792 if (!stop_loop)
793 pr_err("looping? Too many records seen from '%s'\n",
794 psi->name);
ca01d6dd
TL
795}
796
6dda9266
TL
797static void pstore_dowork(struct work_struct *work)
798{
799 pstore_get_records(1);
800}
801
24ed960a 802static void pstore_timefunc(struct timer_list *unused)
6dda9266
TL
803{
804 if (pstore_new_entry) {
805 pstore_new_entry = 0;
806 schedule_work(&pstore_work);
807 }
808
78c83c82 809 pstore_timer_kick();
6dda9266
TL
810}
811
8d82cee2 812static void __init pstore_choose_compression(void)
fe1d4758
KC
813{
814 const struct pstore_zbackend *step;
815
816 if (!compress)
817 return;
818
819 for (step = zbackends; step->name; step++) {
820 if (!strcmp(compress, step->name)) {
821 zbackend = step;
fe1d4758
KC
822 return;
823 }
824 }
825}
826
cb095afd
KC
827static int __init pstore_init(void)
828{
829 int ret;
830
831 pstore_choose_compression();
832
41603165
JFG
833 /*
834 * Check if any pstore backends registered earlier but did not
835 * initialize compression because crypto was not ready. If so,
836 * initialize compression now.
837 */
95047b05 838 allocate_buf_for_compression();
41603165 839
cb095afd
KC
840 ret = pstore_init_fs();
841 if (ret)
8a57d6d4 842 free_buf_for_compression();
cb095afd 843
8a57d6d4 844 return ret;
cb095afd 845}
41603165 846late_initcall(pstore_init);
cb095afd
KC
847
848static void __exit pstore_exit(void)
849{
850 pstore_exit_fs();
851}
852module_exit(pstore_exit)
853
fe1d4758
KC
854module_param(compress, charp, 0444);
855MODULE_PARM_DESC(compress, "Pstore compression to use");
856
dee28e72
MG
857module_param(backend, charp, 0444);
858MODULE_PARM_DESC(backend, "Pstore backend to use");
cb095afd
KC
859
860MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
861MODULE_LICENSE("GPL");