platform.c 18.7 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4
/*
 * Persistent Storage - platform driver interface parts.
 *
5
 * Copyright (C) 2007-2008 Google, Inc.
6 7 8
 * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com>
 */

F
Fabian Frederick 已提交
9 10
#define pr_fmt(fmt) "pstore: " fmt

11 12 13 14 15
#include <linux/atomic.h>
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/kmsg_dump.h>
16
#include <linux/console.h>
17 18
#include <linux/module.h>
#include <linux/pstore.h>
A
Arnd Bergmann 已提交
19
#if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
20 21
#include <linux/lzo.h>
#endif
A
Arnd Bergmann 已提交
22
#if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
23 24
#include <linux/lz4.h>
#endif
25 26 27
#if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
#include <linux/zstd.h>
#endif
G
Geliang Tang 已提交
28
#include <linux/crypto.h>
29
#include <linux/string.h>
30
#include <linux/timer.h>
31 32
#include <linux/slab.h>
#include <linux/uaccess.h>
33
#include <linux/jiffies.h>
34
#include <linux/workqueue.h>
35 36 37

#include "internal.h"

38 39 40 41 42
/*
 * We defer making "oops" entries appear in pstore - see
 * whether the system is actually still running well enough
 * to let someone see the entry
 */
43
static int pstore_update_ms = -1;
44 45
module_param_named(update_ms, pstore_update_ms, int, 0600);
MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content "
46
		 "(default is -1, which means runtime updates are disabled; "
47
		 "enabling this option may not be safe; it may lead to further "
48
		 "corruption on Oopses)");
49

50 51 52 53 54 55 56 57 58 59 60 61 62
/* Names should be in the same order as the enum pstore_type_id */
static const char * const pstore_type_names[] = {
	"dmesg",
	"mce",
	"console",
	"ftrace",
	"rtas",
	"powerpc-ofw",
	"powerpc-common",
	"pmsg",
	"powerpc-opal",
};

63 64
static int pstore_new_entry;

65
static void pstore_timefunc(struct timer_list *);
66
static DEFINE_TIMER(pstore_timer, pstore_timefunc);
67 68 69 70

static void pstore_dowork(struct work_struct *);
static DECLARE_WORK(pstore_work, pstore_dowork);

71
/*
72 73 74
 * psinfo_lock protects "psinfo" during calls to
 * pstore_register(), pstore_unregister(), and
 * the filesystem mount/unmount routines.
75
 */
76
static DEFINE_MUTEX(psinfo_lock);
77
struct pstore_info *psinfo;
78

79
static char *backend;
80 81 82
module_param(backend, charp, 0444);
MODULE_PARM_DESC(backend, "specific backend to use");

83 84 85 86 87 88
static char *compress =
#ifdef CONFIG_PSTORE_COMPRESS_DEFAULT
		CONFIG_PSTORE_COMPRESS_DEFAULT;
#else
		NULL;
#endif
89 90
module_param(compress, charp, 0444);
MODULE_PARM_DESC(compress, "compression to use");
91

92
/* Compression parameters */
G
Geliang Tang 已提交
93
static struct crypto_comp *tfm;
94 95

struct pstore_zbackend {
G
Geliang Tang 已提交
96
	int (*zbufsize)(size_t size);
97 98
	const char *name;
};
99 100 101 102

static char *big_oops_buf;
static size_t big_oops_buf_sz;

103
/* How much of the console log to snapshot */
104
unsigned long kmsg_bytes = CONFIG_PSTORE_DEFAULT_KMSG_BYTES;
105

106
void pstore_set_kmsg_bytes(int bytes)
107
{
108
	kmsg_bytes = bytes;
109 110 111 112 113
}

/* Tag each group of saved records with a sequence number */
static int	oopscount;

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
const char *pstore_type_to_name(enum pstore_type_id type)
{
	BUILD_BUG_ON(ARRAY_SIZE(pstore_type_names) != PSTORE_TYPE_MAX);

	if (WARN_ON_ONCE(type >= PSTORE_TYPE_MAX))
		return "unknown";

	return pstore_type_names[type];
}
EXPORT_SYMBOL_GPL(pstore_type_to_name);

enum pstore_type_id pstore_name_to_type(const char *name)
{
	int i;

	for (i = 0; i < PSTORE_TYPE_MAX; i++) {
		if (!strcmp(pstore_type_names[i], name))
			return i;
	}

	return PSTORE_TYPE_MAX;
}
EXPORT_SYMBOL_GPL(pstore_name_to_type);

138 139 140 141 142 143 144 145
static void pstore_timer_kick(void)
{
	if (pstore_update_ms < 0)
		return;

	mod_timer(&pstore_timer, jiffies + msecs_to_jiffies(pstore_update_ms));
}

146
static bool pstore_cannot_block_path(enum kmsg_dump_reason reason)
147
{
148 149 150 151
	/*
	 * In case of NMI path, pstore shouldn't be blocked
	 * regardless of reason.
	 */
152 153 154 155 156 157
	if (in_nmi())
		return true;

	switch (reason) {
	/* In panic case, other cpus are stopped by smp_send_stop(). */
	case KMSG_DUMP_PANIC:
158 159 160 161
	/*
	 * Emergency restart shouldn't be blocked by spinning on
	 * pstore_info::buf_lock.
	 */
162 163 164 165 166 167 168
	case KMSG_DUMP_EMERG:
		return true;
	default:
		return false;
	}
}

A
Arnd Bergmann 已提交
169
#if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
G
Geliang Tang 已提交
170
static int zbufsize_deflate(size_t size)
171
{
172 173
	size_t cmpr;

G
Geliang Tang 已提交
174
	switch (size) {
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
	/* buffer range for efivars */
	case 1000 ... 2000:
		cmpr = 56;
		break;
	case 2001 ... 3000:
		cmpr = 54;
		break;
	case 3001 ... 3999:
		cmpr = 52;
		break;
	/* buffer range for nvram, erst */
	case 4000 ... 10000:
		cmpr = 45;
		break;
	default:
		cmpr = 60;
		break;
	}
193

G
Geliang Tang 已提交
194
	return (size * 100) / cmpr;
195 196 197
}
#endif

A
Arnd Bergmann 已提交
198
#if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
G
Geliang Tang 已提交
199
static int zbufsize_lzo(size_t size)
200
{
G
Geliang Tang 已提交
201
	return lzo1x_worst_compress(size);
202 203 204
}
#endif

A
Arnd Bergmann 已提交
205
#if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
G
Geliang Tang 已提交
206
static int zbufsize_lz4(size_t size)
207
{
G
Geliang Tang 已提交
208
	return LZ4_compressBound(size);
209
}
210 211
#endif

A
Arnd Bergmann 已提交
212
#if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
G
Geliang Tang 已提交
213
static int zbufsize_842(size_t size)
214
{
215
	return size;
216
}
217 218
#endif

219 220 221
#if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
static int zbufsize_zstd(size_t size)
{
N
Nick Terrell 已提交
222
	return zstd_compress_bound(size);
223 224 225
}
#endif

226 227 228
static const struct pstore_zbackend *zbackend __ro_after_init;

static const struct pstore_zbackend zbackends[] = {
A
Arnd Bergmann 已提交
229
#if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
230
	{
G
Geliang Tang 已提交
231 232
		.zbufsize	= zbufsize_deflate,
		.name		= "deflate",
233 234
	},
#endif
A
Arnd Bergmann 已提交
235
#if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
236
	{
G
Geliang Tang 已提交
237
		.zbufsize	= zbufsize_lzo,
238 239 240
		.name		= "lzo",
	},
#endif
A
Arnd Bergmann 已提交
241
#if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS)
242
	{
G
Geliang Tang 已提交
243
		.zbufsize	= zbufsize_lz4,
244 245 246
		.name		= "lz4",
	},
#endif
A
Arnd Bergmann 已提交
247
#if IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
248
	{
G
Geliang Tang 已提交
249
		.zbufsize	= zbufsize_lz4,
250 251
		.name		= "lz4hc",
	},
252
#endif
A
Arnd Bergmann 已提交
253
#if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
254
	{
G
Geliang Tang 已提交
255
		.zbufsize	= zbufsize_842,
256 257
		.name		= "842",
	},
258 259 260 261 262 263
#endif
#if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
	{
		.zbufsize	= zbufsize_zstd,
		.name		= "zstd",
	},
264 265 266
#endif
	{ }
};
267 268

static int pstore_compress(const void *in, void *out,
G
Geliang Tang 已提交
269
			   unsigned int inlen, unsigned int outlen)
270
{
G
Geliang Tang 已提交
271 272
	int ret;

273
	if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS))
274 275
		return -EINVAL;

G
Geliang Tang 已提交
276 277 278 279 280 281 282
	ret = crypto_comp_compress(tfm, in, inlen, out, &outlen);
	if (ret) {
		pr_err("crypto_comp_compress failed, ret = %d!\n", ret);
		return ret;
	}

	return outlen;
283 284 285 286
}

static void allocate_buf_for_compression(void)
{
287 288 289 290 291
	struct crypto_comp *ctx;
	int size;
	char *buf;

	/* Skip if not built-in or compression backend not selected yet. */
292
	if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !zbackend)
G
Geliang Tang 已提交
293 294
		return;

295 296 297 298
	/* Skip if no pstore backend yet or compression init already done. */
	if (!psinfo || tfm)
		return;

G
Geliang Tang 已提交
299
	if (!crypto_has_comp(zbackend->name, 0, 0)) {
300
		pr_err("Unknown compression: %s\n", zbackend->name);
G
Geliang Tang 已提交
301 302 303
		return;
	}

304 305 306 307
	size = zbackend->zbufsize(psinfo->bufsize);
	if (size <= 0) {
		pr_err("Invalid compression size for %s: %d\n",
		       zbackend->name, size);
G
Geliang Tang 已提交
308
		return;
309
	}
G
Geliang Tang 已提交
310

311 312 313 314
	buf = kmalloc(size, GFP_KERNEL);
	if (!buf) {
		pr_err("Failed %d byte compression buffer allocation for: %s\n",
		       size, zbackend->name);
G
Geliang Tang 已提交
315 316 317
		return;
	}

318 319 320 321 322
	ctx = crypto_alloc_comp(zbackend->name, 0, 0);
	if (IS_ERR_OR_NULL(ctx)) {
		kfree(buf);
		pr_err("crypto_alloc_comp('%s') failed: %ld\n", zbackend->name,
		       PTR_ERR(ctx));
G
Geliang Tang 已提交
323
		return;
324
	}
325 326 327 328 329 330

	/* A non-NULL big_oops_buf indicates compression is available. */
	tfm = ctx;
	big_oops_buf_sz = size;
	big_oops_buf = buf;

331
	pr_info("Using crash dump compression: %s\n", zbackend->name);
332 333 334 335
}

static void free_buf_for_compression(void)
{
336
	if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && tfm) {
G
Geliang Tang 已提交
337
		crypto_free_comp(tfm);
338 339
		tfm = NULL;
	}
G
Geliang Tang 已提交
340 341 342
	kfree(big_oops_buf);
	big_oops_buf = NULL;
	big_oops_buf_sz = 0;
G
Geliang Tang 已提交
343 344
}

345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
/*
 * Called when compression fails, since the printk buffer
 * would be fetched for compression calling it again when
 * compression fails would have moved the iterator of
 * printk buffer which results in fetching old contents.
 * Copy the recent messages from big_oops_buf to psinfo->buf
 */
static size_t copy_kmsg_to_buffer(int hsize, size_t len)
{
	size_t total_len;
	size_t diff;

	total_len = hsize + len;

	if (total_len > psinfo->bufsize) {
		diff = total_len - psinfo->bufsize + hsize;
		memcpy(psinfo->buf, big_oops_buf, hsize);
		memcpy(psinfo->buf + hsize, big_oops_buf + diff,
					psinfo->bufsize - hsize);
		total_len = psinfo->bufsize;
	} else
		memcpy(psinfo->buf, big_oops_buf, total_len);

	return total_len;
}

371 372 373 374 375 376
void pstore_record_init(struct pstore_record *record,
			struct pstore_info *psinfo)
{
	memset(record, 0, sizeof(*record));

	record->psi = psinfo;
377 378

	/* Report zeroed timestamp if called before timekeeping has resumed. */
379
	record->time = ns_to_timespec64(ktime_get_real_fast_ns());
380 381
}

382
/*
383 384
 * callback from kmsg_dump. Save as much as we can (up to kmsg_bytes) from the
 * end of the buffer.
385 386
 */
static void pstore_dump(struct kmsg_dumper *dumper,
387
			enum kmsg_dump_reason reason)
388
{
389
	struct kmsg_dump_iter iter;
390
	unsigned long	total = 0;
391
	const char	*why;
M
Matthew Garrett 已提交
392
	unsigned int	part = 1;
393
	unsigned long	flags = 0;
394
	int		ret;
395

396
	why = kmsg_dump_reason_str(reason);
T
Tony Luck 已提交
397

398 399 400 401
	if (pstore_cannot_block_path(reason)) {
		if (!spin_trylock_irqsave(&psinfo->buf_lock, flags)) {
			pr_err("dump skipped in %s path because of concurrent dump\n",
					in_nmi() ? "NMI" : why);
402
			return;
403
		}
404 405
	} else {
		spin_lock_irqsave(&psinfo->buf_lock, flags);
406
	}
K
Kees Cook 已提交
407

408 409
	kmsg_dump_rewind(&iter);

410 411
	oopscount++;
	while (total < kmsg_bytes) {
412
		char *dst;
413 414
		size_t dst_size;
		int header_size;
415
		int zipped_len = -1;
416
		size_t dump_size;
417 418 419 420 421 422 423 424
		struct pstore_record record;

		pstore_record_init(&record, psinfo);
		record.type = PSTORE_TYPE_DMESG;
		record.count = oopscount;
		record.reason = reason;
		record.part = part;
		record.buf = psinfo->buf;
425

K
Kees Cook 已提交
426
		if (big_oops_buf) {
427
			dst = big_oops_buf;
428
			dst_size = big_oops_buf_sz;
N
Namhyung Kim 已提交
429 430
		} else {
			dst = psinfo->buf;
431
			dst_size = psinfo->bufsize;
N
Namhyung Kim 已提交
432 433
		}

434 435 436 437
		/* Write dump header. */
		header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why,
				 oopscount, part);
		dst_size -= header_size;
438

439
		/* Write dump contents. */
440
		if (!kmsg_dump_get_buffer(&iter, true, dst + header_size,
441
					  dst_size, &dump_size))
N
Namhyung Kim 已提交
442
			break;
443

K
Kees Cook 已提交
444
		if (big_oops_buf) {
445
			zipped_len = pstore_compress(dst, psinfo->buf,
446 447
						header_size + dump_size,
						psinfo->bufsize);
448 449

			if (zipped_len > 0) {
450 451
				record.compressed = true;
				record.size = zipped_len;
452
			} else {
453 454
				record.size = copy_kmsg_to_buffer(header_size,
								  dump_size);
455 456
			}
		} else {
457
			record.size = header_size + dump_size;
458
		}
459

460
		ret = psinfo->write(&record);
461
		if (ret == 0 && reason == KMSG_DUMP_OOPS) {
462
			pstore_new_entry = 1;
463 464
			pstore_timer_kick();
		}
465

466
		total += record.size;
467
		part++;
468
	}
469
	spin_unlock_irqrestore(&psinfo->buf_lock, flags);
470 471 472 473 474 475
}

static struct kmsg_dumper pstore_dumper = {
	.dump = pstore_dump,
};

476 477 478
/*
 * Register with kmsg_dump to save last part of console log on panic.
 */
479 480 481 482 483
static void pstore_register_kmsg(void)
{
	kmsg_dump_register(&pstore_dumper);
}

G
Geliang Tang 已提交
484 485 486 487 488
static void pstore_unregister_kmsg(void)
{
	kmsg_dump_unregister(&pstore_dumper);
}

489 490 491
#ifdef CONFIG_PSTORE_CONSOLE
static void pstore_console_write(struct console *con, const char *s, unsigned c)
{
492
	struct pstore_record record;
493

494 495 496
	if (!c)
		return;

497 498
	pstore_record_init(&record, psinfo);
	record.type = PSTORE_TYPE_CONSOLE;
499

500 501 502
	record.buf = (char *)s;
	record.size = c;
	psinfo->write(&record);
503 504 505 506 507 508 509 510 511
}

static struct console pstore_console = {
	.write	= pstore_console_write,
	.index	= -1,
};

static void pstore_register_console(void)
{
512 513 514
	/* Show which backend is going to get console writes. */
	strscpy(pstore_console.name, psinfo->name,
		sizeof(pstore_console.name));
515 516 517 518 519
	/*
	 * Always initialize flags here since prior unregister_console()
	 * calls may have changed settings (specifically CON_ENABLED).
	 */
	pstore_console.flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME;
520 521
	register_console(&pstore_console);
}
G
Geliang Tang 已提交
522 523 524 525 526

static void pstore_unregister_console(void)
{
	unregister_console(&pstore_console);
}
527 528
#else
static void pstore_register_console(void) {}
G
Geliang Tang 已提交
529
static void pstore_unregister_console(void) {}
530 531
#endif

K
Kees Cook 已提交
532 533
static int pstore_write_user_compat(struct pstore_record *record,
				    const char __user *buf)
M
Mark Salyzyn 已提交
534
{
K
Kees Cook 已提交
535 536 537 538 539
	int ret = 0;

	if (record->buf)
		return -EINVAL;

G
Geliang Tang 已提交
540
	record->buf = memdup_user(buf, record->size);
541
	if (IS_ERR(record->buf)) {
G
Geliang Tang 已提交
542
		ret = PTR_ERR(record->buf);
K
Kees Cook 已提交
543
		goto out;
M
Mark Salyzyn 已提交
544
	}
K
Kees Cook 已提交
545 546 547 548

	ret = record->psi->write(record);

	kfree(record->buf);
G
Geliang Tang 已提交
549
out:
K
Kees Cook 已提交
550 551 552
	record->buf = NULL;

	return unlikely(ret < 0) ? ret : record->size;
M
Mark Salyzyn 已提交
553 554
}

555 556 557 558 559 560 561 562 563
/*
 * platform specific persistent storage driver registers with
 * us here. If pstore is already mounted, call the platform
 * read function right away to populate the file system. If not
 * then the pstore mount code will call us later to fill out
 * the file system.
 */
int pstore_register(struct pstore_info *psi)
{
564 565
	if (backend && strcmp(backend, psi->name)) {
		pr_warn("ignoring unexpected backend '%s'\n", psi->name);
566
		return -EPERM;
567
	}
568

K
Kees Cook 已提交
569 570 571 572 573 574 575 576 577 578 579 580 581 582
	/* Sanity check flags. */
	if (!psi->flags) {
		pr_warn("backend '%s' must support at least one frontend\n",
			psi->name);
		return -EINVAL;
	}

	/* Check for required functions. */
	if (!psi->read || !psi->write) {
		pr_warn("backend '%s' must implement read() and write()\n",
			psi->name);
		return -EINVAL;
	}

583
	mutex_lock(&psinfo_lock);
584
	if (psinfo) {
585 586
		pr_warn("backend '%s' already loaded: ignoring '%s'\n",
			psinfo->name, psi->name);
587
		mutex_unlock(&psinfo_lock);
588 589
		return -EBUSY;
	}
590

K
Kees Cook 已提交
591 592
	if (!psi->write_user)
		psi->write_user = pstore_write_user_compat;
593
	psinfo = psi;
594
	mutex_init(&psinfo->read_mutex);
595
	spin_lock_init(&psinfo->buf_lock);
596

597 598
	if (psi->flags & PSTORE_FLAGS_DMESG)
		allocate_buf_for_compression();
599

600
	pstore_get_records(0);
601

602 603
	if (psi->flags & PSTORE_FLAGS_DMESG) {
		pstore_dumper.max_reason = psinfo->max_reason;
604
		pstore_register_kmsg();
605
	}
606
	if (psi->flags & PSTORE_FLAGS_CONSOLE)
607
		pstore_register_console();
608
	if (psi->flags & PSTORE_FLAGS_FTRACE)
609
		pstore_register_ftrace();
610
	if (psi->flags & PSTORE_FLAGS_PMSG)
611
		pstore_register_pmsg();
612

613
	/* Start watching for new records, if desired. */
614
	pstore_timer_kick();
615

616 617 618 619
	/*
	 * Update the module parameter backend, so it is visible
	 * through /sys/module/pstore/parameters/backend
	 */
620
	backend = kstrdup(psi->name, GFP_KERNEL);
621

F
Fabian Frederick 已提交
622
	pr_info("Registered %s as persistent store backend\n", psi->name);
623

624
	mutex_unlock(&psinfo_lock);
625 626 627 628
	return 0;
}
EXPORT_SYMBOL_GPL(pstore_register);

G
Geliang Tang 已提交
629 630
void pstore_unregister(struct pstore_info *psi)
{
631 632 633 634 635 636 637 638 639 640 641 642
	/* It's okay to unregister nothing. */
	if (!psi)
		return;

	mutex_lock(&psinfo_lock);

	/* Only one backend can be registered at a time. */
	if (WARN_ON(psi != psinfo)) {
		mutex_unlock(&psinfo_lock);
		return;
	}

643
	/* Unregister all callbacks. */
644
	if (psi->flags & PSTORE_FLAGS_PMSG)
645
		pstore_unregister_pmsg();
646
	if (psi->flags & PSTORE_FLAGS_FTRACE)
647
		pstore_unregister_ftrace();
648
	if (psi->flags & PSTORE_FLAGS_CONSOLE)
649
		pstore_unregister_console();
650 651
	if (psi->flags & PSTORE_FLAGS_DMESG)
		pstore_unregister_kmsg();
G
Geliang Tang 已提交
652

653 654 655 656
	/* Stop timer and make sure all work has finished. */
	del_timer_sync(&pstore_timer);
	flush_work(&pstore_work);

657 658 659
	/* Remove all backend records from filesystem tree. */
	pstore_put_backend_records(psi);

G
Geliang Tang 已提交
660 661 662
	free_buf_for_compression();

	psinfo = NULL;
663
	kfree(backend);
G
Geliang Tang 已提交
664
	backend = NULL;
665
	mutex_unlock(&psinfo_lock);
G
Geliang Tang 已提交
666 667 668
}
EXPORT_SYMBOL_GPL(pstore_unregister);

669 670
static void decompress_record(struct pstore_record *record)
{
671
	int ret;
672
	int unzipped_len;
673
	char *unzipped, *workspace;
674

675
	if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !record->compressed)
676 677
		return;

678
	/* Only PSTORE_TYPE_DMESG support compression. */
679
	if (record->type != PSTORE_TYPE_DMESG) {
680 681 682 683
		pr_warn("ignored compressed record type %d\n", record->type);
		return;
	}

684
	/* Missing compression buffer means compression was not initialized. */
685
	if (!big_oops_buf) {
686
		pr_warn("no decompression method initialized!\n");
687 688 689
		return;
	}

690 691 692 693 694
	/* Allocate enough space to hold max decompression and ECC. */
	unzipped_len = big_oops_buf_sz;
	workspace = kmalloc(unzipped_len + record->ecc_notice_size,
			    GFP_KERNEL);
	if (!workspace)
695 696
		return;

697 698 699 700 701 702
	/* After decompression "unzipped_len" is almost certainly smaller. */
	ret = crypto_comp_decompress(tfm, record->buf, record->size,
					  workspace, &unzipped_len);
	if (ret) {
		pr_err("crypto_comp_decompress failed, ret = %d!\n", ret);
		kfree(workspace);
703 704 705 706
		return;
	}

	/* Append ECC notice to decompressed buffer. */
707
	memcpy(workspace + unzipped_len, record->buf + record->size,
708 709
	       record->ecc_notice_size);

710 711 712 713 714 715 716 717
	/* Copy decompressed contents into an minimum-sized allocation. */
	unzipped = kmemdup(workspace, unzipped_len + record->ecc_notice_size,
			   GFP_KERNEL);
	kfree(workspace);
	if (!unzipped)
		return;

	/* Swap out compressed contents with decompressed contents. */
718
	kfree(record->buf);
719
	record->buf = unzipped;
720 721
	record->size = unzipped_len;
	record->compressed = false;
722 723
}

724
/*
725
 * Read all the records from one persistent store backend. Create
726 727 728
 * files in our filesystem.  Don't warn about -EEXIST errors
 * when we are re-scanning the backing store looking to add new
 * error records.
729
 */
730 731
void pstore_get_backend_records(struct pstore_info *psi,
				struct dentry *root, int quiet)
732
{
733
	int failed = 0;
734
	unsigned int stop_loop = 65536;
735

736
	if (!psi || !root)
737 738
		return;

739
	mutex_lock(&psi->read_mutex);
740
	if (psi->open && psi->open(psi))
741 742
		goto out;

743 744 745 746 747
	/*
	 * Backend callback read() allocates record.buf. decompress_record()
	 * may reallocate record.buf. On success, pstore_mkfile() will keep
	 * the record.buf, so free it only on failure.
	 */
748
	for (; stop_loop; stop_loop--) {
749 750 751 752 753 754 755 756
		struct pstore_record *record;
		int rc;

		record = kzalloc(sizeof(*record), GFP_KERNEL);
		if (!record) {
			pr_err("out of memory creating record\n");
			break;
		}
757
		pstore_record_init(record, psi);
758 759 760 761

		record->size = psi->read(record);

		/* No more records left in backend? */
762 763
		if (record->size <= 0) {
			kfree(record);
764
			break;
765
		}
766 767

		decompress_record(record);
768
		rc = pstore_mkfile(root, record);
769
		if (rc) {
770
			/* pstore_mkfile() did not take record, so free it. */
771
			kfree(record->buf);
772
			kfree(record);
773 774 775
			if (rc != -EEXIST || !quiet)
				failed++;
		}
776
	}
777 778
	if (psi->close)
		psi->close(psi);
779
out:
780
	mutex_unlock(&psi->read_mutex);
781 782

	if (failed)
783
		pr_warn("failed to create %d record(s) from '%s'\n",
F
Fabian Frederick 已提交
784
			failed, psi->name);
785 786 787
	if (!stop_loop)
		pr_err("looping? Too many records seen from '%s'\n",
			psi->name);
788 789
}

790 791 792 793 794
static void pstore_dowork(struct work_struct *work)
{
	pstore_get_records(1);
}

795
static void pstore_timefunc(struct timer_list *unused)
796 797 798 799 800 801
{
	if (pstore_new_entry) {
		pstore_new_entry = 0;
		schedule_work(&pstore_work);
	}

802
	pstore_timer_kick();
803 804
}

805
static void __init pstore_choose_compression(void)
806 807 808 809 810 811 812 813 814 815 816 817 818 819
{
	const struct pstore_zbackend *step;

	if (!compress)
		return;

	for (step = zbackends; step->name; step++) {
		if (!strcmp(compress, step->name)) {
			zbackend = step;
			return;
		}
	}
}

K
Kees Cook 已提交
820 821 822 823 824 825
static int __init pstore_init(void)
{
	int ret;

	pstore_choose_compression();

826 827 828 829 830
	/*
	 * Check if any pstore backends registered earlier but did not
	 * initialize compression because crypto was not ready. If so,
	 * initialize compression now.
	 */
831
	allocate_buf_for_compression();
832

K
Kees Cook 已提交
833 834
	ret = pstore_init_fs();
	if (ret)
835
		free_buf_for_compression();
K
Kees Cook 已提交
836

837
	return ret;
K
Kees Cook 已提交
838
}
839
late_initcall(pstore_init);
K
Kees Cook 已提交
840 841 842 843 844 845 846 847 848

static void __exit pstore_exit(void)
{
	pstore_exit_fs();
}
module_exit(pstore_exit)

MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
MODULE_LICENSE("GPL");