platform.c 19.0 KB
Newer Older
1 2 3
/*
 * Persistent Storage - platform driver interface parts.
 *
4
 * Copyright (C) 2007-2008 Google, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

F
Fabian Frederick 已提交
21 22
#define pr_fmt(fmt) "pstore: " fmt

23 24 25 26 27
#include <linux/atomic.h>
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/kmsg_dump.h>
28
#include <linux/console.h>
29 30
#include <linux/module.h>
#include <linux/pstore.h>
A
Arnd Bergmann 已提交
31
#if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
32 33
#include <linux/lzo.h>
#endif
A
Arnd Bergmann 已提交
34
#if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
35 36
#include <linux/lz4.h>
#endif
37 38 39
#if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
#include <linux/zstd.h>
#endif
G
Geliang Tang 已提交
40
#include <linux/crypto.h>
41
#include <linux/string.h>
42
#include <linux/timer.h>
43 44
#include <linux/slab.h>
#include <linux/uaccess.h>
45
#include <linux/jiffies.h>
46
#include <linux/workqueue.h>
47 48 49

#include "internal.h"

50 51 52 53 54
/*
 * We defer making "oops" entries appear in pstore - see
 * whether the system is actually still running well enough
 * to let someone see the entry
 */
55
static int pstore_update_ms = -1;
56 57
module_param_named(update_ms, pstore_update_ms, int, 0600);
MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content "
58 59 60
		 "(default is -1, which means runtime updates are disabled; "
		 "enabling this option is not safe, it may lead to further "
		 "corruption on Oopses)");
61

62 63 64 65 66 67 68 69 70 71 72 73 74
/* 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",
};

75 76
static int pstore_new_entry;

77
static void pstore_timefunc(struct timer_list *);
78
static DEFINE_TIMER(pstore_timer, pstore_timefunc);
79 80 81 82

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

83 84 85 86 87
/*
 * pstore_lock just protects "psinfo" during
 * calls to pstore_register()
 */
static DEFINE_SPINLOCK(pstore_lock);
88
struct pstore_info *psinfo;
89

90
static char *backend;
91 92 93 94 95 96
static char *compress =
#ifdef CONFIG_PSTORE_COMPRESS_DEFAULT
		CONFIG_PSTORE_COMPRESS_DEFAULT;
#else
		NULL;
#endif
97

98
/* Compression parameters */
G
Geliang Tang 已提交
99
static struct crypto_comp *tfm;
100 101

struct pstore_zbackend {
G
Geliang Tang 已提交
102
	int (*zbufsize)(size_t size);
103 104
	const char *name;
};
105 106 107 108

static char *big_oops_buf;
static size_t big_oops_buf_sz;

109
/* How much of the console log to snapshot */
D
David Howells 已提交
110
unsigned long kmsg_bytes = PSTORE_DEFAULT_KMSG_BYTES;
111

112
void pstore_set_kmsg_bytes(int bytes)
113
{
114
	kmsg_bytes = bytes;
115 116 117 118 119
}

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

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
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);

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
static const char *get_reason_str(enum kmsg_dump_reason reason)
{
	switch (reason) {
	case KMSG_DUMP_PANIC:
		return "Panic";
	case KMSG_DUMP_OOPS:
		return "Oops";
	case KMSG_DUMP_EMERG:
		return "Emergency";
	case KMSG_DUMP_RESTART:
		return "Restart";
	case KMSG_DUMP_HALT:
		return "Halt";
	case KMSG_DUMP_POWEROFF:
		return "Poweroff";
	default:
		return "Unknown";
	}
}
T
Tony Luck 已提交
163

K
Kees Cook 已提交
164 165 166 167 168 169
/*
 * 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)
170
{
K
Kees Cook 已提交
171
	/* In NMI path, pstore shouldn't block regardless of reason. */
172 173 174 175 176 177
	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 已提交
178
	/* Emergency restart shouldn't be blocked. */
179 180 181 182 183 184 185
	case KMSG_DUMP_EMERG:
		return true;
	default:
		return false;
	}
}

A
Arnd Bergmann 已提交
186
#if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
G
Geliang Tang 已提交
187
static int zbufsize_deflate(size_t size)
188
{
189 190
	size_t cmpr;

G
Geliang Tang 已提交
191
	switch (size) {
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
	/* 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;
	}
210

G
Geliang Tang 已提交
211
	return (size * 100) / cmpr;
212 213 214
}
#endif

A
Arnd Bergmann 已提交
215
#if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
G
Geliang Tang 已提交
216
static int zbufsize_lzo(size_t size)
217
{
G
Geliang Tang 已提交
218
	return lzo1x_worst_compress(size);
219 220 221
}
#endif

A
Arnd Bergmann 已提交
222
#if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
G
Geliang Tang 已提交
223
static int zbufsize_lz4(size_t size)
224
{
G
Geliang Tang 已提交
225
	return LZ4_compressBound(size);
226
}
227 228
#endif

A
Arnd Bergmann 已提交
229
#if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
G
Geliang Tang 已提交
230
static int zbufsize_842(size_t size)
231
{
232
	return size;
233
}
234 235
#endif

236 237 238 239 240 241 242
#if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
static int zbufsize_zstd(size_t size)
{
	return ZSTD_compressBound(size);
}
#endif

243 244 245
static const struct pstore_zbackend *zbackend __ro_after_init;

static const struct pstore_zbackend zbackends[] = {
A
Arnd Bergmann 已提交
246
#if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
247
	{
G
Geliang Tang 已提交
248 249
		.zbufsize	= zbufsize_deflate,
		.name		= "deflate",
250 251
	},
#endif
A
Arnd Bergmann 已提交
252
#if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
253
	{
G
Geliang Tang 已提交
254
		.zbufsize	= zbufsize_lzo,
255 256 257
		.name		= "lzo",
	},
#endif
A
Arnd Bergmann 已提交
258
#if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS)
259
	{
G
Geliang Tang 已提交
260
		.zbufsize	= zbufsize_lz4,
261 262 263
		.name		= "lz4",
	},
#endif
A
Arnd Bergmann 已提交
264
#if IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
265
	{
G
Geliang Tang 已提交
266
		.zbufsize	= zbufsize_lz4,
267 268
		.name		= "lz4hc",
	},
269
#endif
A
Arnd Bergmann 已提交
270
#if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
271
	{
G
Geliang Tang 已提交
272
		.zbufsize	= zbufsize_842,
273 274
		.name		= "842",
	},
275 276 277 278 279 280
#endif
#if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
	{
		.zbufsize	= zbufsize_zstd,
		.name		= "zstd",
	},
281 282 283
#endif
	{ }
};
284 285

static int pstore_compress(const void *in, void *out,
G
Geliang Tang 已提交
286
			   unsigned int inlen, unsigned int outlen)
287
{
G
Geliang Tang 已提交
288 289 290 291 292 293 294 295 296
	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;
297 298 299 300
}

static void allocate_buf_for_compression(void)
{
301 302 303 304 305
	struct crypto_comp *ctx;
	int size;
	char *buf;

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

309 310 311 312
	/* Skip if no pstore backend yet or compression init already done. */
	if (!psinfo || tfm)
		return;

G
Geliang Tang 已提交
313
	if (!crypto_has_comp(zbackend->name, 0, 0)) {
314
		pr_err("Unknown compression: %s\n", zbackend->name);
G
Geliang Tang 已提交
315 316 317
		return;
	}

318 319 320 321
	size = zbackend->zbufsize(psinfo->bufsize);
	if (size <= 0) {
		pr_err("Invalid compression size for %s: %d\n",
		       zbackend->name, size);
G
Geliang Tang 已提交
322
		return;
323
	}
G
Geliang Tang 已提交
324

325 326 327 328
	buf = kmalloc(size, GFP_KERNEL);
	if (!buf) {
		pr_err("Failed %d byte compression buffer allocation for: %s\n",
		       size, zbackend->name);
G
Geliang Tang 已提交
329 330 331
		return;
	}

332 333 334 335 336
	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 已提交
337
		return;
338
	}
339 340 341 342 343 344

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

345
	pr_info("Using crash dump compression: %s\n", zbackend->name);
346 347 348 349
}

static void free_buf_for_compression(void)
{
350
	if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && tfm) {
G
Geliang Tang 已提交
351
		crypto_free_comp(tfm);
352 353
		tfm = NULL;
	}
G
Geliang Tang 已提交
354 355 356
	kfree(big_oops_buf);
	big_oops_buf = NULL;
	big_oops_buf_sz = 0;
G
Geliang Tang 已提交
357 358
}

359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
/*
 * 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;
}

385 386 387 388 389 390
void pstore_record_init(struct pstore_record *record,
			struct pstore_info *psinfo)
{
	memset(record, 0, sizeof(*record));

	record->psi = psinfo;
391 392

	/* Report zeroed timestamp if called before timekeeping has resumed. */
393
	record->time = ns_to_timespec64(ktime_get_real_fast_ns());
394 395
}

396
/*
397 398
 * callback from kmsg_dump. Save as much as we can (up to kmsg_bytes) from the
 * end of the buffer.
399 400
 */
static void pstore_dump(struct kmsg_dumper *dumper,
401
			enum kmsg_dump_reason reason)
402
{
403
	unsigned long	total = 0;
404
	const char	*why;
M
Matthew Garrett 已提交
405
	unsigned int	part = 1;
406
	int		ret;
407

408
	why = get_reason_str(reason);
T
Tony Luck 已提交
409

K
Kees Cook 已提交
410 411 412 413 414 415 416 417 418
	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");
419
			return;
420
		}
421
	}
K
Kees Cook 已提交
422

423 424
	oopscount++;
	while (total < kmsg_bytes) {
425
		char *dst;
426 427
		size_t dst_size;
		int header_size;
428
		int zipped_len = -1;
429
		size_t dump_size;
430 431 432 433 434 435 436 437
		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;
438

K
Kees Cook 已提交
439
		if (big_oops_buf) {
440
			dst = big_oops_buf;
441
			dst_size = big_oops_buf_sz;
N
Namhyung Kim 已提交
442 443
		} else {
			dst = psinfo->buf;
444
			dst_size = psinfo->bufsize;
N
Namhyung Kim 已提交
445 446
		}

447 448 449 450
		/* Write dump header. */
		header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why,
				 oopscount, part);
		dst_size -= header_size;
451

452 453 454
		/* Write dump contents. */
		if (!kmsg_dump_get_buffer(dumper, true, dst + header_size,
					  dst_size, &dump_size))
N
Namhyung Kim 已提交
455
			break;
456

K
Kees Cook 已提交
457
		if (big_oops_buf) {
458
			zipped_len = pstore_compress(dst, psinfo->buf,
459 460
						header_size + dump_size,
						psinfo->bufsize);
461 462

			if (zipped_len > 0) {
463 464
				record.compressed = true;
				record.size = zipped_len;
465
			} else {
466 467
				record.size = copy_kmsg_to_buffer(header_size,
								  dump_size);
468 469
			}
		} else {
470
			record.size = header_size + dump_size;
471
		}
472

473
		ret = psinfo->write(&record);
474
		if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted())
475
			pstore_new_entry = 1;
476

477
		total += record.size;
478
		part++;
479
	}
K
Kees Cook 已提交
480 481

	up(&psinfo->buf_lock);
482 483 484 485 486 487
}

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

488 489 490
/*
 * Register with kmsg_dump to save last part of console log on panic.
 */
491 492 493 494 495
static void pstore_register_kmsg(void)
{
	kmsg_dump_register(&pstore_dumper);
}

G
Geliang Tang 已提交
496 497 498 499 500
static void pstore_unregister_kmsg(void)
{
	kmsg_dump_unregister(&pstore_dumper);
}

501 502 503
#ifdef CONFIG_PSTORE_CONSOLE
static void pstore_console_write(struct console *con, const char *s, unsigned c)
{
504
	struct pstore_record record;
505

506 507 508
	if (!c)
		return;

509 510
	pstore_record_init(&record, psinfo);
	record.type = PSTORE_TYPE_CONSOLE;
511

512 513 514
	record.buf = (char *)s;
	record.size = c;
	psinfo->write(&record);
515 516 517 518 519 520 521 522 523 524 525 526 527
}

static struct console pstore_console = {
	.name	= "pstore",
	.write	= pstore_console_write,
	.flags	= CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME,
	.index	= -1,
};

static void pstore_register_console(void)
{
	register_console(&pstore_console);
}
G
Geliang Tang 已提交
528 529 530 531 532

static void pstore_unregister_console(void)
{
	unregister_console(&pstore_console);
}
533 534
#else
static void pstore_register_console(void) {}
G
Geliang Tang 已提交
535
static void pstore_unregister_console(void) {}
536 537
#endif

K
Kees Cook 已提交
538 539
static int pstore_write_user_compat(struct pstore_record *record,
				    const char __user *buf)
M
Mark Salyzyn 已提交
540
{
K
Kees Cook 已提交
541 542 543 544 545
	int ret = 0;

	if (record->buf)
		return -EINVAL;

G
Geliang Tang 已提交
546
	record->buf = memdup_user(buf, record->size);
547
	if (IS_ERR(record->buf)) {
G
Geliang Tang 已提交
548
		ret = PTR_ERR(record->buf);
K
Kees Cook 已提交
549
		goto out;
M
Mark Salyzyn 已提交
550
	}
K
Kees Cook 已提交
551 552 553 554

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

	kfree(record->buf);
G
Geliang Tang 已提交
555
out:
K
Kees Cook 已提交
556 557 558
	record->buf = NULL;

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

561 562 563 564 565 566 567 568 569 570 571
/*
 * 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)
{
	struct module *owner = psi->owner;

572 573
	if (backend && strcmp(backend, psi->name)) {
		pr_warn("ignoring unexpected backend '%s'\n", psi->name);
574
		return -EPERM;
575
	}
576

K
Kees Cook 已提交
577 578 579 580 581 582 583 584 585 586 587 588 589 590
	/* 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;
	}

591 592
	spin_lock(&pstore_lock);
	if (psinfo) {
593 594
		pr_warn("backend '%s' already loaded: ignoring '%s'\n",
			psinfo->name, psi->name);
595 596 597
		spin_unlock(&pstore_lock);
		return -EBUSY;
	}
598

K
Kees Cook 已提交
599 600
	if (!psi->write_user)
		psi->write_user = pstore_write_user_compat;
601
	psinfo = psi;
602
	mutex_init(&psinfo->read_mutex);
K
Kees Cook 已提交
603
	sema_init(&psinfo->buf_lock, 1);
604 605 606 607 608 609 610
	spin_unlock(&pstore_lock);

	if (owner && !try_module_get(owner)) {
		psinfo = NULL;
		return -EINVAL;
	}

611 612
	allocate_buf_for_compression();

613
	if (pstore_is_mounted())
614
		pstore_get_records(0);
615

616 617 618
	if (psi->flags & PSTORE_FLAGS_DMESG)
		pstore_register_kmsg();
	if (psi->flags & PSTORE_FLAGS_CONSOLE)
619
		pstore_register_console();
620
	if (psi->flags & PSTORE_FLAGS_FTRACE)
621
		pstore_register_ftrace();
622
	if (psi->flags & PSTORE_FLAGS_PMSG)
623
		pstore_register_pmsg();
624

625
	/* Start watching for new records, if desired. */
626 627 628 629 630
	if (pstore_update_ms >= 0) {
		pstore_timer.expires = jiffies +
			msecs_to_jiffies(pstore_update_ms);
		add_timer(&pstore_timer);
	}
631

632 633 634 635 636 637
	/*
	 * Update the module parameter backend, so it is visible
	 * through /sys/module/pstore/parameters/backend
	 */
	backend = psi->name;

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

640 641
	module_put(owner);

642 643 644 645
	return 0;
}
EXPORT_SYMBOL_GPL(pstore_register);

G
Geliang Tang 已提交
646 647
void pstore_unregister(struct pstore_info *psi)
{
648 649 650 651 652
	/* Stop timer and make sure all work has finished. */
	pstore_update_ms = -1;
	del_timer_sync(&pstore_timer);
	flush_work(&pstore_work);

653
	if (psi->flags & PSTORE_FLAGS_PMSG)
654
		pstore_unregister_pmsg();
655
	if (psi->flags & PSTORE_FLAGS_FTRACE)
656
		pstore_unregister_ftrace();
657
	if (psi->flags & PSTORE_FLAGS_CONSOLE)
658
		pstore_unregister_console();
659 660
	if (psi->flags & PSTORE_FLAGS_DMESG)
		pstore_unregister_kmsg();
G
Geliang Tang 已提交
661 662 663 664 665 666 667 668

	free_buf_for_compression();

	psinfo = NULL;
	backend = NULL;
}
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 676 677
	if (!record->compressed)
		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 803 804
	if (pstore_update_ms >= 0)
		mod_timer(&pstore_timer,
			  jiffies + msecs_to_jiffies(pstore_update_ms));
805 806
}

807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
void __init pstore_choose_compression(void)
{
	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 已提交
822 823 824 825 826 827
static int __init pstore_init(void)
{
	int ret;

	pstore_choose_compression();

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

K
Kees Cook 已提交
835 836 837 838 839 840
	ret = pstore_init_fs();
	if (ret)
		return ret;

	return 0;
}
841
late_initcall(pstore_init);
K
Kees Cook 已提交
842 843 844 845 846 847 848

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

849 850 851
module_param(compress, charp, 0444);
MODULE_PARM_DESC(compress, "Pstore compression to use");

852 853
module_param(backend, charp, 0444);
MODULE_PARM_DESC(backend, "Pstore backend to use");
K
Kees Cook 已提交
854 855 856

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