platform.c 18.4 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 47 48
		 "(default is -1, which means runtime updates are disabled; "
		 "enabling this option is not safe, it may lead to further "
		 "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 75
/*
 * pstore_lock just protects "psinfo" during
 * calls to pstore_register()
 */
static DEFINE_SPINLOCK(pstore_lock);
76
struct pstore_info *psinfo;
77

78
static char *backend;
79 80 81 82 83 84
static char *compress =
#ifdef CONFIG_PSTORE_COMPRESS_DEFAULT
		CONFIG_PSTORE_COMPRESS_DEFAULT;
#else
		NULL;
#endif
85

86
/* Compression parameters */
G
Geliang Tang 已提交
87
static struct crypto_comp *tfm;
88 89

struct pstore_zbackend {
G
Geliang Tang 已提交
90
	int (*zbufsize)(size_t size);
91 92
	const char *name;
};
93 94 95 96

static char *big_oops_buf;
static size_t big_oops_buf_sz;

97
/* How much of the console log to snapshot */
D
David Howells 已提交
98
unsigned long kmsg_bytes = PSTORE_DEFAULT_KMSG_BYTES;
99

100
void pstore_set_kmsg_bytes(int bytes)
101
{
102
	kmsg_bytes = bytes;
103 104 105 106 107
}

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

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
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);

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
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 已提交
151

K
Kees Cook 已提交
152 153 154 155 156 157
/*
 * 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)
158
{
K
Kees Cook 已提交
159
	/* In NMI path, pstore shouldn't block regardless of reason. */
160 161 162 163 164 165
	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 已提交
166
	/* Emergency restart shouldn't be blocked. */
167 168 169 170 171 172 173
	case KMSG_DUMP_EMERG:
		return true;
	default:
		return false;
	}
}

A
Arnd Bergmann 已提交
174
#if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
G
Geliang Tang 已提交
175
static int zbufsize_deflate(size_t size)
176
{
177 178
	size_t cmpr;

G
Geliang Tang 已提交
179
	switch (size) {
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
	/* 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;
	}
198

G
Geliang Tang 已提交
199
	return (size * 100) / cmpr;
200 201 202
}
#endif

A
Arnd Bergmann 已提交
203
#if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
G
Geliang Tang 已提交
204
static int zbufsize_lzo(size_t size)
205
{
G
Geliang Tang 已提交
206
	return lzo1x_worst_compress(size);
207 208 209
}
#endif

A
Arnd Bergmann 已提交
210
#if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
G
Geliang Tang 已提交
211
static int zbufsize_lz4(size_t size)
212
{
G
Geliang Tang 已提交
213
	return LZ4_compressBound(size);
214
}
215 216
#endif

A
Arnd Bergmann 已提交
217
#if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
G
Geliang Tang 已提交
218
static int zbufsize_842(size_t size)
219
{
220
	return size;
221
}
222 223
#endif

224 225 226 227 228 229 230
#if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
static int zbufsize_zstd(size_t size)
{
	return ZSTD_compressBound(size);
}
#endif

231 232 233
static const struct pstore_zbackend *zbackend __ro_after_init;

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

static int pstore_compress(const void *in, void *out,
G
Geliang Tang 已提交
274
			   unsigned int inlen, unsigned int outlen)
275
{
G
Geliang Tang 已提交
276 277 278 279 280 281 282 283 284
	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;
285 286 287 288
}

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

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

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

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

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

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

320 321 322 323 324
	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 已提交
325
		return;
326
	}
327 328 329 330 331 332

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

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

static void free_buf_for_compression(void)
{
338
	if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && tfm) {
G
Geliang Tang 已提交
339
		crypto_free_comp(tfm);
340 341
		tfm = NULL;
	}
G
Geliang Tang 已提交
342 343 344
	kfree(big_oops_buf);
	big_oops_buf = NULL;
	big_oops_buf_sz = 0;
G
Geliang Tang 已提交
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 371 372
/*
 * 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;
}

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

	record->psi = psinfo;
379 380

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

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

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

K
Kees Cook 已提交
398 399 400 401 402 403 404 405 406
	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");
407
			return;
408
		}
409
	}
K
Kees Cook 已提交
410

411 412
	oopscount++;
	while (total < kmsg_bytes) {
413
		char *dst;
414 415
		size_t dst_size;
		int header_size;
416
		int zipped_len = -1;
417
		size_t dump_size;
418 419 420 421 422 423 424 425
		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;
426

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

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

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

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

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

461
		ret = psinfo->write(&record);
462
		if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted())
463
			pstore_new_entry = 1;
464

465
		total += record.size;
466
		part++;
467
	}
K
Kees Cook 已提交
468 469

	up(&psinfo->buf_lock);
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 512 513 514 515
}

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 已提交
516 517 518 519 520

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

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

	if (record->buf)
		return -EINVAL;

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

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

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

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

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)
{
	struct module *owner = psi->owner;

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 580
	spin_lock(&pstore_lock);
	if (psinfo) {
581 582
		pr_warn("backend '%s' already loaded: ignoring '%s'\n",
			psinfo->name, psi->name);
583 584 585
		spin_unlock(&pstore_lock);
		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 595 596 597 598
	spin_unlock(&pstore_lock);

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

599 600
	if (psi->flags & PSTORE_FLAGS_DMESG)
		allocate_buf_for_compression();
601

602
	if (pstore_is_mounted())
603
		pstore_get_records(0);
604

605 606 607
	if (psi->flags & PSTORE_FLAGS_DMESG)
		pstore_register_kmsg();
	if (psi->flags & PSTORE_FLAGS_CONSOLE)
608
		pstore_register_console();
609
	if (psi->flags & PSTORE_FLAGS_FTRACE)
610
		pstore_register_ftrace();
611
	if (psi->flags & PSTORE_FLAGS_PMSG)
612
		pstore_register_pmsg();
613

614
	/* Start watching for new records, if desired. */
615 616 617 618 619
	if (pstore_update_ms >= 0) {
		pstore_timer.expires = jiffies +
			msecs_to_jiffies(pstore_update_ms);
		add_timer(&pstore_timer);
	}
620

621 622 623 624 625 626
	/*
	 * Update the module parameter backend, so it is visible
	 * through /sys/module/pstore/parameters/backend
	 */
	backend = psi->name;

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

629 630
	module_put(owner);

631 632 633 634
	return 0;
}
EXPORT_SYMBOL_GPL(pstore_register);

G
Geliang Tang 已提交
635 636
void pstore_unregister(struct pstore_info *psi)
{
637 638 639 640 641
	/* Stop timer and make sure all work has finished. */
	pstore_update_ms = -1;
	del_timer_sync(&pstore_timer);
	flush_work(&pstore_work);

642
	if (psi->flags & PSTORE_FLAGS_PMSG)
643
		pstore_unregister_pmsg();
644
	if (psi->flags & PSTORE_FLAGS_FTRACE)
645
		pstore_unregister_ftrace();
646
	if (psi->flags & PSTORE_FLAGS_CONSOLE)
647
		pstore_unregister_console();
648 649
	if (psi->flags & PSTORE_FLAGS_DMESG)
		pstore_unregister_kmsg();
G
Geliang Tang 已提交
650 651 652 653 654 655 656 657

	free_buf_for_compression();

	psinfo = NULL;
	backend = NULL;
}
EXPORT_SYMBOL_GPL(pstore_unregister);

658 659
static void decompress_record(struct pstore_record *record)
{
660
	int ret;
661
	int unzipped_len;
662
	char *unzipped, *workspace;
663

664 665 666
	if (!record->compressed)
		return;

667
	/* Only PSTORE_TYPE_DMESG support compression. */
668
	if (record->type != PSTORE_TYPE_DMESG) {
669 670 671 672
		pr_warn("ignored compressed record type %d\n", record->type);
		return;
	}

673
	/* Missing compression buffer means compression was not initialized. */
674
	if (!big_oops_buf) {
675
		pr_warn("no decompression method initialized!\n");
676 677 678
		return;
	}

679 680 681 682 683
	/* 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)
684 685
		return;

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

	/* Append ECC notice to decompressed buffer. */
696
	memcpy(workspace + unzipped_len, record->buf + record->size,
697 698
	       record->ecc_notice_size);

699 700 701 702 703 704 705 706
	/* 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. */
707
	kfree(record->buf);
708
	record->buf = unzipped;
709 710
	record->size = unzipped_len;
	record->compressed = false;
711 712
}

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

725
	if (!psi || !root)
726 727
		return;

728
	mutex_lock(&psi->read_mutex);
729
	if (psi->open && psi->open(psi))
730 731
		goto out;

732 733 734 735 736
	/*
	 * 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.
	 */
737
	for (; stop_loop; stop_loop--) {
738 739 740 741 742 743 744 745
		struct pstore_record *record;
		int rc;

		record = kzalloc(sizeof(*record), GFP_KERNEL);
		if (!record) {
			pr_err("out of memory creating record\n");
			break;
		}
746
		pstore_record_init(record, psi);
747 748 749 750

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

		/* No more records left in backend? */
751 752
		if (record->size <= 0) {
			kfree(record);
753
			break;
754
		}
755 756

		decompress_record(record);
757
		rc = pstore_mkfile(root, record);
758
		if (rc) {
759
			/* pstore_mkfile() did not take record, so free it. */
760
			kfree(record->buf);
761
			kfree(record);
762 763 764
			if (rc != -EEXIST || !quiet)
				failed++;
		}
765
	}
766 767
	if (psi->close)
		psi->close(psi);
768
out:
769
	mutex_unlock(&psi->read_mutex);
770 771

	if (failed)
772
		pr_warn("failed to create %d record(s) from '%s'\n",
F
Fabian Frederick 已提交
773
			failed, psi->name);
774 775 776
	if (!stop_loop)
		pr_err("looping? Too many records seen from '%s'\n",
			psi->name);
777 778
}

779 780 781 782 783
static void pstore_dowork(struct work_struct *work)
{
	pstore_get_records(1);
}

784
static void pstore_timefunc(struct timer_list *unused)
785 786 787 788 789 790
{
	if (pstore_new_entry) {
		pstore_new_entry = 0;
		schedule_work(&pstore_work);
	}

791 792 793
	if (pstore_update_ms >= 0)
		mod_timer(&pstore_timer,
			  jiffies + msecs_to_jiffies(pstore_update_ms));
794 795
}

796
static void __init pstore_choose_compression(void)
797 798 799 800 801 802 803 804 805 806 807 808 809 810
{
	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 已提交
811 812 813 814 815 816
static int __init pstore_init(void)
{
	int ret;

	pstore_choose_compression();

817 818 819 820 821
	/*
	 * Check if any pstore backends registered earlier but did not
	 * initialize compression because crypto was not ready. If so,
	 * initialize compression now.
	 */
822
	allocate_buf_for_compression();
823

K
Kees Cook 已提交
824 825 826 827 828 829
	ret = pstore_init_fs();
	if (ret)
		return ret;

	return 0;
}
830
late_initcall(pstore_init);
K
Kees Cook 已提交
831 832 833 834 835 836 837

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

838 839 840
module_param(compress, charp, 0444);
MODULE_PARM_DESC(compress, "Pstore compression to use");

841 842
module_param(backend, charp, 0444);
MODULE_PARM_DESC(backend, "Pstore backend to use");
K
Kees Cook 已提交
843 844 845

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