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