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