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