platform.c 18.8 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 83 84 85
static char *compress =
#ifdef CONFIG_PSTORE_COMPRESS_DEFAULT
		CONFIG_PSTORE_COMPRESS_DEFAULT;
#else
		NULL;
#endif
86

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

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

static char *big_oops_buf;
static size_t big_oops_buf_sz;

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

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

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

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

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

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

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

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

G
Geliang Tang 已提交
208
	return (size * 100) / cmpr;
209 210 211
}
#endif

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

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

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

233 234 235 236 237 238 239
#if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
static int zbufsize_zstd(size_t size)
{
	return ZSTD_compressBound(size);
}
#endif

240 241 242
static const struct pstore_zbackend *zbackend __ro_after_init;

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

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

static void allocate_buf_for_compression(void)
{
298 299 300 301 302
	struct crypto_comp *ctx;
	int size;
	char *buf;

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

306 307 308 309
	/* Skip if no pstore backend yet or compression init already done. */
	if (!psinfo || tfm)
		return;

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

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

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

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

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

342
	pr_info("Using crash dump compression: %s\n", zbackend->name);
343 344 345 346
}

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

356 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
/*
 * 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;
}

382 383 384 385 386 387
void pstore_record_init(struct pstore_record *record,
			struct pstore_info *psinfo)
{
	memset(record, 0, sizeof(*record));

	record->psi = psinfo;
388 389

	/* Report zeroed timestamp if called before timekeeping has resumed. */
390
	record->time = ns_to_timespec64(ktime_get_real_fast_ns());
391 392
}

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

405
	why = get_reason_str(reason);
T
Tony Luck 已提交
406

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

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

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

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

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

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

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

470
		ret = psinfo->write(&record);
471
		if (ret == 0 && reason == KMSG_DUMP_OOPS) {
472
			pstore_new_entry = 1;
473 474
			pstore_timer_kick();
		}
475

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

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

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

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

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

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

505 506 507
	if (!c)
		return;

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

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

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

static void pstore_register_console(void)
{
524 525 526 527 528
	/*
	 * 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;
529 530
	register_console(&pstore_console);
}
G
Geliang Tang 已提交
531 532 533 534 535

static void pstore_unregister_console(void)
{
	unregister_console(&pstore_console);
}
536 537
#else
static void pstore_register_console(void) {}
G
Geliang Tang 已提交
538
static void pstore_unregister_console(void) {}
539 540
#endif

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

	if (record->buf)
		return -EINVAL;

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

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

	kfree(record->buf);
G
Geliang Tang 已提交
558
out:
K
Kees Cook 已提交
559 560 561
	record->buf = NULL;

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

564 565 566 567 568 569 570 571 572
/*
 * 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)
{
573 574
	if (backend && strcmp(backend, psi->name)) {
		pr_warn("ignoring unexpected backend '%s'\n", psi->name);
575
		return -EPERM;
576
	}
577

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

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

K
Kees Cook 已提交
600 601
	if (!psi->write_user)
		psi->write_user = pstore_write_user_compat;
602
	psinfo = psi;
603
	mutex_init(&psinfo->read_mutex);
K
Kees Cook 已提交
604
	sema_init(&psinfo->buf_lock, 1);
605

606 607
	if (psi->flags & PSTORE_FLAGS_DMESG)
		allocate_buf_for_compression();
608

609
	pstore_get_records(0);
610

611 612 613
	if (psi->flags & PSTORE_FLAGS_DMESG)
		pstore_register_kmsg();
	if (psi->flags & PSTORE_FLAGS_CONSOLE)
614
		pstore_register_console();
615
	if (psi->flags & PSTORE_FLAGS_FTRACE)
616
		pstore_register_ftrace();
617
	if (psi->flags & PSTORE_FLAGS_PMSG)
618
		pstore_register_pmsg();
619

620
	/* Start watching for new records, if desired. */
621
	pstore_timer_kick();
622

623 624 625 626
	/*
	 * Update the module parameter backend, so it is visible
	 * through /sys/module/pstore/parameters/backend
	 */
627
	backend = kstrdup(psi->name, GFP_KERNEL);
628

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

631
	mutex_unlock(&psinfo_lock);
632 633 634 635
	return 0;
}
EXPORT_SYMBOL_GPL(pstore_register);

G
Geliang Tang 已提交
636 637
void pstore_unregister(struct pstore_info *psi)
{
638 639 640 641 642 643 644 645 646 647 648 649
	/* 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;
	}

650
	/* Unregister all callbacks. */
651
	if (psi->flags & PSTORE_FLAGS_PMSG)
652
		pstore_unregister_pmsg();
653
	if (psi->flags & PSTORE_FLAGS_FTRACE)
654
		pstore_unregister_ftrace();
655
	if (psi->flags & PSTORE_FLAGS_CONSOLE)
656
		pstore_unregister_console();
657 658
	if (psi->flags & PSTORE_FLAGS_DMESG)
		pstore_unregister_kmsg();
G
Geliang Tang 已提交
659

660 661 662 663
	/* Stop timer and make sure all work has finished. */
	del_timer_sync(&pstore_timer);
	flush_work(&pstore_work);

664 665 666
	/* Remove all backend records from filesystem tree. */
	pstore_put_backend_records(psi);

G
Geliang Tang 已提交
667 668 669
	free_buf_for_compression();

	psinfo = NULL;
670
	kfree(backend);
G
Geliang Tang 已提交
671
	backend = NULL;
672
	mutex_unlock(&psinfo_lock);
G
Geliang Tang 已提交
673 674 675
}
EXPORT_SYMBOL_GPL(pstore_unregister);

676 677
static void decompress_record(struct pstore_record *record)
{
678
	int ret;
679
	int unzipped_len;
680
	char *unzipped, *workspace;
681

682 683 684
	if (!record->compressed)
		return;

685
	/* Only PSTORE_TYPE_DMESG support compression. */
686
	if (record->type != PSTORE_TYPE_DMESG) {
687 688 689 690
		pr_warn("ignored compressed record type %d\n", record->type);
		return;
	}

691
	/* Missing compression buffer means compression was not initialized. */
692
	if (!big_oops_buf) {
693
		pr_warn("no decompression method initialized!\n");
694 695 696
		return;
	}

697 698 699 700 701
	/* 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)
702 703
		return;

704 705 706 707 708 709
	/* 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);
710 711 712 713
		return;
	}

	/* Append ECC notice to decompressed buffer. */
714
	memcpy(workspace + unzipped_len, record->buf + record->size,
715 716
	       record->ecc_notice_size);

717 718 719 720 721 722 723 724
	/* 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. */
725
	kfree(record->buf);
726
	record->buf = unzipped;
727 728
	record->size = unzipped_len;
	record->compressed = false;
729 730
}

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

743
	if (!psi || !root)
744 745
		return;

746
	mutex_lock(&psi->read_mutex);
747
	if (psi->open && psi->open(psi))
748 749
		goto out;

750 751 752 753 754
	/*
	 * 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.
	 */
755
	for (; stop_loop; stop_loop--) {
756 757 758 759 760 761 762 763
		struct pstore_record *record;
		int rc;

		record = kzalloc(sizeof(*record), GFP_KERNEL);
		if (!record) {
			pr_err("out of memory creating record\n");
			break;
		}
764
		pstore_record_init(record, psi);
765 766 767 768

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

		/* No more records left in backend? */
769 770
		if (record->size <= 0) {
			kfree(record);
771
			break;
772
		}
773 774

		decompress_record(record);
775
		rc = pstore_mkfile(root, record);
776
		if (rc) {
777
			/* pstore_mkfile() did not take record, so free it. */
778
			kfree(record->buf);
779
			kfree(record);
780 781 782
			if (rc != -EEXIST || !quiet)
				failed++;
		}
783
	}
784 785
	if (psi->close)
		psi->close(psi);
786
out:
787
	mutex_unlock(&psi->read_mutex);
788 789

	if (failed)
790
		pr_warn("failed to create %d record(s) from '%s'\n",
F
Fabian Frederick 已提交
791
			failed, psi->name);
792 793 794
	if (!stop_loop)
		pr_err("looping? Too many records seen from '%s'\n",
			psi->name);
795 796
}

797 798 799 800 801
static void pstore_dowork(struct work_struct *work)
{
	pstore_get_records(1);
}

802
static void pstore_timefunc(struct timer_list *unused)
803 804 805 806 807 808
{
	if (pstore_new_entry) {
		pstore_new_entry = 0;
		schedule_work(&pstore_work);
	}

809
	pstore_timer_kick();
810 811
}

812
static void __init pstore_choose_compression(void)
813 814 815 816 817 818 819 820 821 822 823 824 825 826
{
	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 已提交
827 828 829 830 831 832
static int __init pstore_init(void)
{
	int ret;

	pstore_choose_compression();

833 834 835 836 837
	/*
	 * Check if any pstore backends registered earlier but did not
	 * initialize compression because crypto was not ready. If so,
	 * initialize compression now.
	 */
838
	allocate_buf_for_compression();
839

K
Kees Cook 已提交
840 841
	ret = pstore_init_fs();
	if (ret)
842
		free_buf_for_compression();
K
Kees Cook 已提交
843

844
	return ret;
K
Kees Cook 已提交
845
}
846
late_initcall(pstore_init);
K
Kees Cook 已提交
847 848 849 850 851 852 853

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

854 855 856
module_param(compress, charp, 0444);
MODULE_PARM_DESC(compress, "Pstore compression to use");

857 858
module_param(backend, charp, 0444);
MODULE_PARM_DESC(backend, "Pstore backend to use");
K
Kees Cook 已提交
859 860 861

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