platform.c 18.6 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 */
D
David Howells 已提交
104
unsigned long kmsg_bytes = 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));
}

K
Kees Cook 已提交
146 147 148 149 150 151
/*
 * Should pstore_dump() wait for a concurrent pstore_dump()? If
 * not, the current pstore_dump() will report a failure to dump
 * and return.
 */
static bool pstore_cannot_wait(enum kmsg_dump_reason reason)
152
{
K
Kees Cook 已提交
153
	/* In NMI path, pstore shouldn't block regardless of reason. */
154 155 156 157 158 159
	if (in_nmi())
		return true;

	switch (reason) {
	/* In panic case, other cpus are stopped by smp_send_stop(). */
	case KMSG_DUMP_PANIC:
K
Kees Cook 已提交
160
	/* Emergency restart shouldn't be blocked. */
161 162 163 164 165 166 167
	case KMSG_DUMP_EMERG:
		return true;
	default:
		return false;
	}
}

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

G
Geliang Tang 已提交
173
	switch (size) {
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
	/* 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;
	}
192

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

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

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

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

218 219 220 221 222 223 224
#if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
static int zbufsize_zstd(size_t size)
{
	return ZSTD_compressBound(size);
}
#endif

225 226 227
static const struct pstore_zbackend *zbackend __ro_after_init;

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

static int pstore_compress(const void *in, void *out,
G
Geliang Tang 已提交
268
			   unsigned int inlen, unsigned int outlen)
269
{
G
Geliang Tang 已提交
270 271 272 273 274 275 276 277 278
	int ret;

	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;
279 280 281 282
}

static void allocate_buf_for_compression(void)
{
283 284 285 286 287
	struct crypto_comp *ctx;
	int size;
	char *buf;

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

291 292 293 294
	/* Skip if no pstore backend yet or compression init already done. */
	if (!psinfo || tfm)
		return;

G
Geliang Tang 已提交
295
	if (!crypto_has_comp(zbackend->name, 0, 0)) {
296
		pr_err("Unknown compression: %s\n", zbackend->name);
G
Geliang Tang 已提交
297 298 299
		return;
	}

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

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

314 315 316 317 318
	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 已提交
319
		return;
320
	}
321 322 323 324 325 326

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

327
	pr_info("Using crash dump compression: %s\n", zbackend->name);
328 329 330 331
}

static void free_buf_for_compression(void)
{
332
	if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && tfm) {
G
Geliang Tang 已提交
333
		crypto_free_comp(tfm);
334 335
		tfm = NULL;
	}
G
Geliang Tang 已提交
336 337 338
	kfree(big_oops_buf);
	big_oops_buf = NULL;
	big_oops_buf_sz = 0;
G
Geliang Tang 已提交
339 340
}

341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
/*
 * 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;
}

367 368 369 370 371 372
void pstore_record_init(struct pstore_record *record,
			struct pstore_info *psinfo)
{
	memset(record, 0, sizeof(*record));

	record->psi = psinfo;
373 374

	/* Report zeroed timestamp if called before timekeeping has resumed. */
375
	record->time = ns_to_timespec64(ktime_get_real_fast_ns());
376 377
}

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

390
	why = kmsg_dump_reason_str(reason);
T
Tony Luck 已提交
391

K
Kees Cook 已提交
392 393 394 395 396 397 398 399 400
	if (down_trylock(&psinfo->buf_lock)) {
		/* Failed to acquire lock: give up if we cannot wait. */
		if (pstore_cannot_wait(reason)) {
			pr_err("dump skipped in %s path: may corrupt error record\n",
				in_nmi() ? "NMI" : why);
			return;
		}
		if (down_interruptible(&psinfo->buf_lock)) {
			pr_err("could not grab semaphore?!\n");
401
			return;
402
		}
403
	}
K
Kees Cook 已提交
404

405 406
	oopscount++;
	while (total < kmsg_bytes) {
407
		char *dst;
408 409
		size_t dst_size;
		int header_size;
410
		int zipped_len = -1;
411
		size_t dump_size;
412 413 414 415 416 417 418 419
		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;
420

K
Kees Cook 已提交
421
		if (big_oops_buf) {
422
			dst = big_oops_buf;
423
			dst_size = big_oops_buf_sz;
N
Namhyung Kim 已提交
424 425
		} else {
			dst = psinfo->buf;
426
			dst_size = psinfo->bufsize;
N
Namhyung Kim 已提交
427 428
		}

429 430 431 432
		/* Write dump header. */
		header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why,
				 oopscount, part);
		dst_size -= header_size;
433

434 435 436
		/* Write dump contents. */
		if (!kmsg_dump_get_buffer(dumper, true, dst + header_size,
					  dst_size, &dump_size))
N
Namhyung Kim 已提交
437
			break;
438

K
Kees Cook 已提交
439
		if (big_oops_buf) {
440
			zipped_len = pstore_compress(dst, psinfo->buf,
441 442
						header_size + dump_size,
						psinfo->bufsize);
443 444

			if (zipped_len > 0) {
445 446
				record.compressed = true;
				record.size = zipped_len;
447
			} else {
448 449
				record.size = copy_kmsg_to_buffer(header_size,
								  dump_size);
450 451
			}
		} else {
452
			record.size = header_size + dump_size;
453
		}
454

455
		ret = psinfo->write(&record);
456
		if (ret == 0 && reason == KMSG_DUMP_OOPS) {
457
			pstore_new_entry = 1;
458 459
			pstore_timer_kick();
		}
460

461
		total += record.size;
462
		part++;
463
	}
K
Kees Cook 已提交
464 465

	up(&psinfo->buf_lock);
466 467 468 469 470 471
}

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

472 473 474
/*
 * Register with kmsg_dump to save last part of console log on panic.
 */
475 476 477 478 479
static void pstore_register_kmsg(void)
{
	kmsg_dump_register(&pstore_dumper);
}

G
Geliang Tang 已提交
480 481 482 483 484
static void pstore_unregister_kmsg(void)
{
	kmsg_dump_unregister(&pstore_dumper);
}

485 486 487
#ifdef CONFIG_PSTORE_CONSOLE
static void pstore_console_write(struct console *con, const char *s, unsigned c)
{
488
	struct pstore_record record;
489

490 491 492
	if (!c)
		return;

493 494
	pstore_record_init(&record, psinfo);
	record.type = PSTORE_TYPE_CONSOLE;
495

496 497 498
	record.buf = (char *)s;
	record.size = c;
	psinfo->write(&record);
499 500 501 502 503 504 505 506 507
}

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

static void pstore_register_console(void)
{
508 509 510
	/* Show which backend is going to get console writes. */
	strscpy(pstore_console.name, psinfo->name,
		sizeof(pstore_console.name));
511 512 513 514 515
	/*
	 * 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;
516 517
	register_console(&pstore_console);
}
G
Geliang Tang 已提交
518 519 520 521 522

static void pstore_unregister_console(void)
{
	unregister_console(&pstore_console);
}
523 524
#else
static void pstore_register_console(void) {}
G
Geliang Tang 已提交
525
static void pstore_unregister_console(void) {}
526 527
#endif

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

	if (record->buf)
		return -EINVAL;

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

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

	kfree(record->buf);
G
Geliang Tang 已提交
545
out:
K
Kees Cook 已提交
546 547 548
	record->buf = NULL;

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

551 552 553 554 555 556 557 558 559
/*
 * 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)
{
560 561
	if (backend && strcmp(backend, psi->name)) {
		pr_warn("ignoring unexpected backend '%s'\n", psi->name);
562
		return -EPERM;
563
	}
564

K
Kees Cook 已提交
565 566 567 568 569 570 571 572 573 574 575 576 577 578
	/* 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;
	}

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

K
Kees Cook 已提交
587 588
	if (!psi->write_user)
		psi->write_user = pstore_write_user_compat;
589
	psinfo = psi;
590
	mutex_init(&psinfo->read_mutex);
K
Kees Cook 已提交
591
	sema_init(&psinfo->buf_lock, 1);
592

593 594
	if (psi->flags & PSTORE_FLAGS_DMESG)
		allocate_buf_for_compression();
595

596
	pstore_get_records(0);
597

598 599
	if (psi->flags & PSTORE_FLAGS_DMESG) {
		pstore_dumper.max_reason = psinfo->max_reason;
600
		pstore_register_kmsg();
601
	}
602
	if (psi->flags & PSTORE_FLAGS_CONSOLE)
603
		pstore_register_console();
604
	if (psi->flags & PSTORE_FLAGS_FTRACE)
605
		pstore_register_ftrace();
606
	if (psi->flags & PSTORE_FLAGS_PMSG)
607
		pstore_register_pmsg();
608

609
	/* Start watching for new records, if desired. */
610
	pstore_timer_kick();
611

612 613 614 615
	/*
	 * Update the module parameter backend, so it is visible
	 * through /sys/module/pstore/parameters/backend
	 */
616
	backend = kstrdup(psi->name, GFP_KERNEL);
617

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

620
	mutex_unlock(&psinfo_lock);
621 622 623 624
	return 0;
}
EXPORT_SYMBOL_GPL(pstore_register);

G
Geliang Tang 已提交
625 626
void pstore_unregister(struct pstore_info *psi)
{
627 628 629 630 631 632 633 634 635 636 637 638
	/* 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;
	}

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

649 650 651 652
	/* Stop timer and make sure all work has finished. */
	del_timer_sync(&pstore_timer);
	flush_work(&pstore_work);

653 654 655
	/* Remove all backend records from filesystem tree. */
	pstore_put_backend_records(psi);

G
Geliang Tang 已提交
656 657 658
	free_buf_for_compression();

	psinfo = NULL;
659
	kfree(backend);
G
Geliang Tang 已提交
660
	backend = NULL;
661
	mutex_unlock(&psinfo_lock);
G
Geliang Tang 已提交
662 663 664
}
EXPORT_SYMBOL_GPL(pstore_unregister);

665 666
static void decompress_record(struct pstore_record *record)
{
667
	int ret;
668
	int unzipped_len;
669
	char *unzipped, *workspace;
670

671 672 673
	if (!record->compressed)
		return;

674
	/* Only PSTORE_TYPE_DMESG support compression. */
675
	if (record->type != PSTORE_TYPE_DMESG) {
676 677 678 679
		pr_warn("ignored compressed record type %d\n", record->type);
		return;
	}

680
	/* Missing compression buffer means compression was not initialized. */
681
	if (!big_oops_buf) {
682
		pr_warn("no decompression method initialized!\n");
683 684 685
		return;
	}

686 687 688 689 690
	/* 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)
691 692
		return;

693 694 695 696 697 698
	/* 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);
699 700 701 702
		return;
	}

	/* Append ECC notice to decompressed buffer. */
703
	memcpy(workspace + unzipped_len, record->buf + record->size,
704 705
	       record->ecc_notice_size);

706 707 708 709 710 711 712 713
	/* 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. */
714
	kfree(record->buf);
715
	record->buf = unzipped;
716 717
	record->size = unzipped_len;
	record->compressed = false;
718 719
}

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

732
	if (!psi || !root)
733 734
		return;

735
	mutex_lock(&psi->read_mutex);
736
	if (psi->open && psi->open(psi))
737 738
		goto out;

739 740 741 742 743
	/*
	 * 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.
	 */
744
	for (; stop_loop; stop_loop--) {
745 746 747 748 749 750 751 752
		struct pstore_record *record;
		int rc;

		record = kzalloc(sizeof(*record), GFP_KERNEL);
		if (!record) {
			pr_err("out of memory creating record\n");
			break;
		}
753
		pstore_record_init(record, psi);
754 755 756 757

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

		/* No more records left in backend? */
758 759
		if (record->size <= 0) {
			kfree(record);
760
			break;
761
		}
762 763

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

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

786 787 788 789 790
static void pstore_dowork(struct work_struct *work)
{
	pstore_get_records(1);
}

791
static void pstore_timefunc(struct timer_list *unused)
792 793 794 795 796 797
{
	if (pstore_new_entry) {
		pstore_new_entry = 0;
		schedule_work(&pstore_work);
	}

798
	pstore_timer_kick();
799 800
}

801
static void __init pstore_choose_compression(void)
802 803 804 805 806 807 808 809 810 811 812 813 814 815
{
	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 已提交
816 817 818 819 820 821
static int __init pstore_init(void)
{
	int ret;

	pstore_choose_compression();

822 823 824 825 826
	/*
	 * Check if any pstore backends registered earlier but did not
	 * initialize compression because crypto was not ready. If so,
	 * initialize compression now.
	 */
827
	allocate_buf_for_compression();
828

K
Kees Cook 已提交
829 830
	ret = pstore_init_fs();
	if (ret)
831
		free_buf_for_compression();
K
Kees Cook 已提交
832

833
	return ret;
K
Kees Cook 已提交
834
}
835
late_initcall(pstore_init);
K
Kees Cook 已提交
836 837 838 839 840 841 842 843 844

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

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