xref: /linux/fs/pstore/platform.c (revision d195c39052d1da278a00a6744ce59c383b67b191)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Persistent Storage - platform driver interface parts.
4  *
5  * Copyright (C) 2007-2008 Google, Inc.
6  * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com>
7  */
8 
9 #define pr_fmt(fmt) "pstore: " fmt
10 
11 #include <linux/atomic.h>
12 #include <linux/types.h>
13 #include <linux/errno.h>
14 #include <linux/init.h>
15 #include <linux/kmsg_dump.h>
16 #include <linux/console.h>
17 #include <linux/module.h>
18 #include <linux/pstore.h>
19 #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
20 #include <linux/lzo.h>
21 #endif
22 #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
23 #include <linux/lz4.h>
24 #endif
25 #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
26 #include <linux/zstd.h>
27 #endif
28 #include <linux/crypto.h>
29 #include <linux/string.h>
30 #include <linux/timer.h>
31 #include <linux/slab.h>
32 #include <linux/uaccess.h>
33 #include <linux/jiffies.h>
34 #include <linux/workqueue.h>
35 
36 #include "internal.h"
37 
38 /*
39  * We defer making "oops" entries appear in pstore - see
40  * whether the system is actually still running well enough
41  * to let someone see the entry
42  */
43 static int pstore_update_ms = -1;
44 module_param_named(update_ms, pstore_update_ms, int, 0600);
45 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 /* Names should be in the same order as the enum pstore_type_id */
51 static const char * const pstore_type_names[] = {
52 	"dmesg",
53 	"mce",
54 	"console",
55 	"ftrace",
56 	"rtas",
57 	"powerpc-ofw",
58 	"powerpc-common",
59 	"pmsg",
60 	"powerpc-opal",
61 };
62 
63 static int pstore_new_entry;
64 
65 static void pstore_timefunc(struct timer_list *);
66 static DEFINE_TIMER(pstore_timer, pstore_timefunc);
67 
68 static void pstore_dowork(struct work_struct *);
69 static DECLARE_WORK(pstore_work, pstore_dowork);
70 
71 /*
72  * psinfo_lock protects "psinfo" during calls to
73  * pstore_register(), pstore_unregister(), and
74  * the filesystem mount/unmount routines.
75  */
76 static DEFINE_MUTEX(psinfo_lock);
77 struct pstore_info *psinfo;
78 
79 static char *backend;
80 static char *compress =
81 #ifdef CONFIG_PSTORE_COMPRESS_DEFAULT
82 		CONFIG_PSTORE_COMPRESS_DEFAULT;
83 #else
84 		NULL;
85 #endif
86 
87 /* Compression parameters */
88 static struct crypto_comp *tfm;
89 
90 struct pstore_zbackend {
91 	int (*zbufsize)(size_t size);
92 	const char *name;
93 };
94 
95 static char *big_oops_buf;
96 static size_t big_oops_buf_sz;
97 
98 /* How much of the console log to snapshot */
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 /* Tag each group of saved records with a sequence number */
107 static int	oopscount;
108 
109 const char *pstore_type_to_name(enum pstore_type_id type)
110 {
111 	BUILD_BUG_ON(ARRAY_SIZE(pstore_type_names) != PSTORE_TYPE_MAX);
112 
113 	if (WARN_ON_ONCE(type >= PSTORE_TYPE_MAX))
114 		return "unknown";
115 
116 	return pstore_type_names[type];
117 }
118 EXPORT_SYMBOL_GPL(pstore_type_to_name);
119 
120 enum pstore_type_id pstore_name_to_type(const char *name)
121 {
122 	int i;
123 
124 	for (i = 0; i < PSTORE_TYPE_MAX; i++) {
125 		if (!strcmp(pstore_type_names[i], name))
126 			return i;
127 	}
128 
129 	return PSTORE_TYPE_MAX;
130 }
131 EXPORT_SYMBOL_GPL(pstore_name_to_type);
132 
133 static const char *get_reason_str(enum kmsg_dump_reason reason)
134 {
135 	switch (reason) {
136 	case KMSG_DUMP_PANIC:
137 		return "Panic";
138 	case KMSG_DUMP_OOPS:
139 		return "Oops";
140 	case KMSG_DUMP_EMERG:
141 		return "Emergency";
142 	case KMSG_DUMP_RESTART:
143 		return "Restart";
144 	case KMSG_DUMP_HALT:
145 		return "Halt";
146 	case KMSG_DUMP_POWEROFF:
147 		return "Poweroff";
148 	default:
149 		return "Unknown";
150 	}
151 }
152 
153 static void pstore_timer_kick(void)
154 {
155 	if (pstore_update_ms < 0)
156 		return;
157 
158 	mod_timer(&pstore_timer, jiffies + msecs_to_jiffies(pstore_update_ms));
159 }
160 
161 /*
162  * Should pstore_dump() wait for a concurrent pstore_dump()? If
163  * not, the current pstore_dump() will report a failure to dump
164  * and return.
165  */
166 static bool pstore_cannot_wait(enum kmsg_dump_reason reason)
167 {
168 	/* In NMI path, pstore shouldn't block regardless of reason. */
169 	if (in_nmi())
170 		return true;
171 
172 	switch (reason) {
173 	/* In panic case, other cpus are stopped by smp_send_stop(). */
174 	case KMSG_DUMP_PANIC:
175 	/* Emergency restart shouldn't be blocked. */
176 	case KMSG_DUMP_EMERG:
177 		return true;
178 	default:
179 		return false;
180 	}
181 }
182 
183 #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
184 static int zbufsize_deflate(size_t size)
185 {
186 	size_t cmpr;
187 
188 	switch (size) {
189 	/* buffer range for efivars */
190 	case 1000 ... 2000:
191 		cmpr = 56;
192 		break;
193 	case 2001 ... 3000:
194 		cmpr = 54;
195 		break;
196 	case 3001 ... 3999:
197 		cmpr = 52;
198 		break;
199 	/* buffer range for nvram, erst */
200 	case 4000 ... 10000:
201 		cmpr = 45;
202 		break;
203 	default:
204 		cmpr = 60;
205 		break;
206 	}
207 
208 	return (size * 100) / cmpr;
209 }
210 #endif
211 
212 #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
213 static int zbufsize_lzo(size_t size)
214 {
215 	return lzo1x_worst_compress(size);
216 }
217 #endif
218 
219 #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
220 static int zbufsize_lz4(size_t size)
221 {
222 	return LZ4_compressBound(size);
223 }
224 #endif
225 
226 #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
227 static int zbufsize_842(size_t size)
228 {
229 	return size;
230 }
231 #endif
232 
233 #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
234 static int zbufsize_zstd(size_t size)
235 {
236 	return ZSTD_compressBound(size);
237 }
238 #endif
239 
240 static const struct pstore_zbackend *zbackend __ro_after_init;
241 
242 static const struct pstore_zbackend zbackends[] = {
243 #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
244 	{
245 		.zbufsize	= zbufsize_deflate,
246 		.name		= "deflate",
247 	},
248 #endif
249 #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
250 	{
251 		.zbufsize	= zbufsize_lzo,
252 		.name		= "lzo",
253 	},
254 #endif
255 #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS)
256 	{
257 		.zbufsize	= zbufsize_lz4,
258 		.name		= "lz4",
259 	},
260 #endif
261 #if IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
262 	{
263 		.zbufsize	= zbufsize_lz4,
264 		.name		= "lz4hc",
265 	},
266 #endif
267 #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
268 	{
269 		.zbufsize	= zbufsize_842,
270 		.name		= "842",
271 	},
272 #endif
273 #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
274 	{
275 		.zbufsize	= zbufsize_zstd,
276 		.name		= "zstd",
277 	},
278 #endif
279 	{ }
280 };
281 
282 static int pstore_compress(const void *in, void *out,
283 			   unsigned int inlen, unsigned int outlen)
284 {
285 	int ret;
286 
287 	ret = crypto_comp_compress(tfm, in, inlen, out, &outlen);
288 	if (ret) {
289 		pr_err("crypto_comp_compress failed, ret = %d!\n", ret);
290 		return ret;
291 	}
292 
293 	return outlen;
294 }
295 
296 static void allocate_buf_for_compression(void)
297 {
298 	struct crypto_comp *ctx;
299 	int size;
300 	char *buf;
301 
302 	/* Skip if not built-in or compression backend not selected yet. */
303 	if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !zbackend)
304 		return;
305 
306 	/* Skip if no pstore backend yet or compression init already done. */
307 	if (!psinfo || tfm)
308 		return;
309 
310 	if (!crypto_has_comp(zbackend->name, 0, 0)) {
311 		pr_err("Unknown compression: %s\n", zbackend->name);
312 		return;
313 	}
314 
315 	size = zbackend->zbufsize(psinfo->bufsize);
316 	if (size <= 0) {
317 		pr_err("Invalid compression size for %s: %d\n",
318 		       zbackend->name, size);
319 		return;
320 	}
321 
322 	buf = kmalloc(size, GFP_KERNEL);
323 	if (!buf) {
324 		pr_err("Failed %d byte compression buffer allocation for: %s\n",
325 		       size, zbackend->name);
326 		return;
327 	}
328 
329 	ctx = crypto_alloc_comp(zbackend->name, 0, 0);
330 	if (IS_ERR_OR_NULL(ctx)) {
331 		kfree(buf);
332 		pr_err("crypto_alloc_comp('%s') failed: %ld\n", zbackend->name,
333 		       PTR_ERR(ctx));
334 		return;
335 	}
336 
337 	/* A non-NULL big_oops_buf indicates compression is available. */
338 	tfm = ctx;
339 	big_oops_buf_sz = size;
340 	big_oops_buf = buf;
341 
342 	pr_info("Using crash dump compression: %s\n", zbackend->name);
343 }
344 
345 static void free_buf_for_compression(void)
346 {
347 	if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && tfm) {
348 		crypto_free_comp(tfm);
349 		tfm = NULL;
350 	}
351 	kfree(big_oops_buf);
352 	big_oops_buf = NULL;
353 	big_oops_buf_sz = 0;
354 }
355 
356 /*
357  * Called when compression fails, since the printk buffer
358  * would be fetched for compression calling it again when
359  * compression fails would have moved the iterator of
360  * printk buffer which results in fetching old contents.
361  * Copy the recent messages from big_oops_buf to psinfo->buf
362  */
363 static size_t copy_kmsg_to_buffer(int hsize, size_t len)
364 {
365 	size_t total_len;
366 	size_t diff;
367 
368 	total_len = hsize + len;
369 
370 	if (total_len > psinfo->bufsize) {
371 		diff = total_len - psinfo->bufsize + hsize;
372 		memcpy(psinfo->buf, big_oops_buf, hsize);
373 		memcpy(psinfo->buf + hsize, big_oops_buf + diff,
374 					psinfo->bufsize - hsize);
375 		total_len = psinfo->bufsize;
376 	} else
377 		memcpy(psinfo->buf, big_oops_buf, total_len);
378 
379 	return total_len;
380 }
381 
382 void pstore_record_init(struct pstore_record *record,
383 			struct pstore_info *psinfo)
384 {
385 	memset(record, 0, sizeof(*record));
386 
387 	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  * callback from kmsg_dump. Save as much as we can (up to kmsg_bytes) from the
395  * 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;
402 	unsigned int	part = 1;
403 	int		ret;
404 
405 	why = get_reason_str(reason);
406 
407 	if (down_trylock(&psinfo->buf_lock)) {
408 		/* Failed to acquire lock: give up if we cannot wait. */
409 		if (pstore_cannot_wait(reason)) {
410 			pr_err("dump skipped in %s path: may corrupt error record\n",
411 				in_nmi() ? "NMI" : why);
412 			return;
413 		}
414 		if (down_interruptible(&psinfo->buf_lock)) {
415 			pr_err("could not grab semaphore?!\n");
416 			return;
417 		}
418 	}
419 
420 	oopscount++;
421 	while (total < kmsg_bytes) {
422 		char *dst;
423 		size_t dst_size;
424 		int header_size;
425 		int zipped_len = -1;
426 		size_t dump_size;
427 		struct pstore_record record;
428 
429 		pstore_record_init(&record, psinfo);
430 		record.type = PSTORE_TYPE_DMESG;
431 		record.count = oopscount;
432 		record.reason = reason;
433 		record.part = part;
434 		record.buf = psinfo->buf;
435 
436 		if (big_oops_buf) {
437 			dst = big_oops_buf;
438 			dst_size = big_oops_buf_sz;
439 		} else {
440 			dst = psinfo->buf;
441 			dst_size = psinfo->bufsize;
442 		}
443 
444 		/* Write dump header. */
445 		header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why,
446 				 oopscount, part);
447 		dst_size -= header_size;
448 
449 		/* Write dump contents. */
450 		if (!kmsg_dump_get_buffer(dumper, true, dst + header_size,
451 					  dst_size, &dump_size))
452 			break;
453 
454 		if (big_oops_buf) {
455 			zipped_len = pstore_compress(dst, psinfo->buf,
456 						header_size + dump_size,
457 						psinfo->bufsize);
458 
459 			if (zipped_len > 0) {
460 				record.compressed = true;
461 				record.size = zipped_len;
462 			} else {
463 				record.size = copy_kmsg_to_buffer(header_size,
464 								  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 			pstore_timer_kick();
474 		}
475 
476 		total += record.size;
477 		part++;
478 	}
479 
480 	up(&psinfo->buf_lock);
481 }
482 
483 static struct kmsg_dumper pstore_dumper = {
484 	.dump = pstore_dump,
485 };
486 
487 /*
488  * Register with kmsg_dump to save last part of console log on panic.
489  */
490 static void pstore_register_kmsg(void)
491 {
492 	kmsg_dump_register(&pstore_dumper);
493 }
494 
495 static void pstore_unregister_kmsg(void)
496 {
497 	kmsg_dump_unregister(&pstore_dumper);
498 }
499 
500 #ifdef CONFIG_PSTORE_CONSOLE
501 static void pstore_console_write(struct console *con, const char *s, unsigned c)
502 {
503 	struct pstore_record record;
504 
505 	if (!c)
506 		return;
507 
508 	pstore_record_init(&record, psinfo);
509 	record.type = PSTORE_TYPE_CONSOLE;
510 
511 	record.buf = (char *)s;
512 	record.size = c;
513 	psinfo->write(&record);
514 }
515 
516 static struct console pstore_console = {
517 	.write	= pstore_console_write,
518 	.index	= -1,
519 };
520 
521 static void pstore_register_console(void)
522 {
523 	/* Show which backend is going to get console writes. */
524 	strscpy(pstore_console.name, psinfo->name,
525 		sizeof(pstore_console.name));
526 	/*
527 	 * Always initialize flags here since prior unregister_console()
528 	 * calls may have changed settings (specifically CON_ENABLED).
529 	 */
530 	pstore_console.flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME;
531 	register_console(&pstore_console);
532 }
533 
534 static void pstore_unregister_console(void)
535 {
536 	unregister_console(&pstore_console);
537 }
538 #else
539 static void pstore_register_console(void) {}
540 static void pstore_unregister_console(void) {}
541 #endif
542 
543 static int pstore_write_user_compat(struct pstore_record *record,
544 				    const char __user *buf)
545 {
546 	int ret = 0;
547 
548 	if (record->buf)
549 		return -EINVAL;
550 
551 	record->buf = memdup_user(buf, record->size);
552 	if (IS_ERR(record->buf)) {
553 		ret = PTR_ERR(record->buf);
554 		goto out;
555 	}
556 
557 	ret = record->psi->write(record);
558 
559 	kfree(record->buf);
560 out:
561 	record->buf = NULL;
562 
563 	return unlikely(ret < 0) ? ret : record->size;
564 }
565 
566 /*
567  * platform specific persistent storage driver registers with
568  * us here. If pstore is already mounted, call the platform
569  * read function right away to populate the file system. If not
570  * then the pstore mount code will call us later to fill out
571  * the file system.
572  */
573 int pstore_register(struct pstore_info *psi)
574 {
575 	if (backend && strcmp(backend, psi->name)) {
576 		pr_warn("ignoring unexpected backend '%s'\n", psi->name);
577 		return -EPERM;
578 	}
579 
580 	/* Sanity check flags. */
581 	if (!psi->flags) {
582 		pr_warn("backend '%s' must support at least one frontend\n",
583 			psi->name);
584 		return -EINVAL;
585 	}
586 
587 	/* Check for required functions. */
588 	if (!psi->read || !psi->write) {
589 		pr_warn("backend '%s' must implement read() and write()\n",
590 			psi->name);
591 		return -EINVAL;
592 	}
593 
594 	mutex_lock(&psinfo_lock);
595 	if (psinfo) {
596 		pr_warn("backend '%s' already loaded: ignoring '%s'\n",
597 			psinfo->name, psi->name);
598 		mutex_unlock(&psinfo_lock);
599 		return -EBUSY;
600 	}
601 
602 	if (!psi->write_user)
603 		psi->write_user = pstore_write_user_compat;
604 	psinfo = psi;
605 	mutex_init(&psinfo->read_mutex);
606 	sema_init(&psinfo->buf_lock, 1);
607 
608 	if (psi->flags & PSTORE_FLAGS_DMESG)
609 		allocate_buf_for_compression();
610 
611 	pstore_get_records(0);
612 
613 	if (psi->flags & PSTORE_FLAGS_DMESG)
614 		pstore_register_kmsg();
615 	if (psi->flags & PSTORE_FLAGS_CONSOLE)
616 		pstore_register_console();
617 	if (psi->flags & PSTORE_FLAGS_FTRACE)
618 		pstore_register_ftrace();
619 	if (psi->flags & PSTORE_FLAGS_PMSG)
620 		pstore_register_pmsg();
621 
622 	/* Start watching for new records, if desired. */
623 	pstore_timer_kick();
624 
625 	/*
626 	 * Update the module parameter backend, so it is visible
627 	 * through /sys/module/pstore/parameters/backend
628 	 */
629 	backend = kstrdup(psi->name, GFP_KERNEL);
630 
631 	pr_info("Registered %s as persistent store backend\n", psi->name);
632 
633 	mutex_unlock(&psinfo_lock);
634 	return 0;
635 }
636 EXPORT_SYMBOL_GPL(pstore_register);
637 
638 void pstore_unregister(struct pstore_info *psi)
639 {
640 	/* It's okay to unregister nothing. */
641 	if (!psi)
642 		return;
643 
644 	mutex_lock(&psinfo_lock);
645 
646 	/* Only one backend can be registered at a time. */
647 	if (WARN_ON(psi != psinfo)) {
648 		mutex_unlock(&psinfo_lock);
649 		return;
650 	}
651 
652 	/* Unregister all callbacks. */
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 	if (psi->flags & PSTORE_FLAGS_DMESG)
660 		pstore_unregister_kmsg();
661 
662 	/* Stop timer and make sure all work has finished. */
663 	del_timer_sync(&pstore_timer);
664 	flush_work(&pstore_work);
665 
666 	/* Remove all backend records from filesystem tree. */
667 	pstore_put_backend_records(psi);
668 
669 	free_buf_for_compression();
670 
671 	psinfo = NULL;
672 	kfree(backend);
673 	backend = NULL;
674 	mutex_unlock(&psinfo_lock);
675 }
676 EXPORT_SYMBOL_GPL(pstore_unregister);
677 
678 static void decompress_record(struct pstore_record *record)
679 {
680 	int ret;
681 	int unzipped_len;
682 	char *unzipped, *workspace;
683 
684 	if (!record->compressed)
685 		return;
686 
687 	/* Only PSTORE_TYPE_DMESG support compression. */
688 	if (record->type != PSTORE_TYPE_DMESG) {
689 		pr_warn("ignored compressed record type %d\n", record->type);
690 		return;
691 	}
692 
693 	/* Missing compression buffer means compression was not initialized. */
694 	if (!big_oops_buf) {
695 		pr_warn("no decompression method initialized!\n");
696 		return;
697 	}
698 
699 	/* Allocate enough space to hold max decompression and ECC. */
700 	unzipped_len = big_oops_buf_sz;
701 	workspace = kmalloc(unzipped_len + record->ecc_notice_size,
702 			    GFP_KERNEL);
703 	if (!workspace)
704 		return;
705 
706 	/* After decompression "unzipped_len" is almost certainly smaller. */
707 	ret = crypto_comp_decompress(tfm, record->buf, record->size,
708 					  workspace, &unzipped_len);
709 	if (ret) {
710 		pr_err("crypto_comp_decompress failed, ret = %d!\n", ret);
711 		kfree(workspace);
712 		return;
713 	}
714 
715 	/* Append ECC notice to decompressed buffer. */
716 	memcpy(workspace + unzipped_len, record->buf + record->size,
717 	       record->ecc_notice_size);
718 
719 	/* Copy decompressed contents into an minimum-sized allocation. */
720 	unzipped = kmemdup(workspace, unzipped_len + record->ecc_notice_size,
721 			   GFP_KERNEL);
722 	kfree(workspace);
723 	if (!unzipped)
724 		return;
725 
726 	/* Swap out compressed contents with decompressed contents. */
727 	kfree(record->buf);
728 	record->buf = unzipped;
729 	record->size = unzipped_len;
730 	record->compressed = false;
731 }
732 
733 /*
734  * Read all the records from one persistent store backend. Create
735  * files in our filesystem.  Don't warn about -EEXIST errors
736  * when we are re-scanning the backing store looking to add new
737  * error records.
738  */
739 void pstore_get_backend_records(struct pstore_info *psi,
740 				struct dentry *root, int quiet)
741 {
742 	int failed = 0;
743 	unsigned int stop_loop = 65536;
744 
745 	if (!psi || !root)
746 		return;
747 
748 	mutex_lock(&psi->read_mutex);
749 	if (psi->open && psi->open(psi))
750 		goto out;
751 
752 	/*
753 	 * Backend callback read() allocates record.buf. decompress_record()
754 	 * may reallocate record.buf. On success, pstore_mkfile() will keep
755 	 * the record.buf, so free it only on failure.
756 	 */
757 	for (; stop_loop; stop_loop--) {
758 		struct pstore_record *record;
759 		int rc;
760 
761 		record = kzalloc(sizeof(*record), GFP_KERNEL);
762 		if (!record) {
763 			pr_err("out of memory creating record\n");
764 			break;
765 		}
766 		pstore_record_init(record, psi);
767 
768 		record->size = psi->read(record);
769 
770 		/* No more records left in backend? */
771 		if (record->size <= 0) {
772 			kfree(record);
773 			break;
774 		}
775 
776 		decompress_record(record);
777 		rc = pstore_mkfile(root, record);
778 		if (rc) {
779 			/* pstore_mkfile() did not take record, so free it. */
780 			kfree(record->buf);
781 			kfree(record);
782 			if (rc != -EEXIST || !quiet)
783 				failed++;
784 		}
785 	}
786 	if (psi->close)
787 		psi->close(psi);
788 out:
789 	mutex_unlock(&psi->read_mutex);
790 
791 	if (failed)
792 		pr_warn("failed to create %d record(s) from '%s'\n",
793 			failed, psi->name);
794 	if (!stop_loop)
795 		pr_err("looping? Too many records seen from '%s'\n",
796 			psi->name);
797 }
798 
799 static void pstore_dowork(struct work_struct *work)
800 {
801 	pstore_get_records(1);
802 }
803 
804 static void pstore_timefunc(struct timer_list *unused)
805 {
806 	if (pstore_new_entry) {
807 		pstore_new_entry = 0;
808 		schedule_work(&pstore_work);
809 	}
810 
811 	pstore_timer_kick();
812 }
813 
814 static void __init pstore_choose_compression(void)
815 {
816 	const struct pstore_zbackend *step;
817 
818 	if (!compress)
819 		return;
820 
821 	for (step = zbackends; step->name; step++) {
822 		if (!strcmp(compress, step->name)) {
823 			zbackend = step;
824 			return;
825 		}
826 	}
827 }
828 
829 static int __init pstore_init(void)
830 {
831 	int ret;
832 
833 	pstore_choose_compression();
834 
835 	/*
836 	 * Check if any pstore backends registered earlier but did not
837 	 * initialize compression because crypto was not ready. If so,
838 	 * initialize compression now.
839 	 */
840 	allocate_buf_for_compression();
841 
842 	ret = pstore_init_fs();
843 	if (ret)
844 		free_buf_for_compression();
845 
846 	return ret;
847 }
848 late_initcall(pstore_init);
849 
850 static void __exit pstore_exit(void)
851 {
852 	pstore_exit_fs();
853 }
854 module_exit(pstore_exit)
855 
856 module_param(compress, charp, 0444);
857 MODULE_PARM_DESC(compress, "Pstore compression to use");
858 
859 module_param(backend, charp, 0444);
860 MODULE_PARM_DESC(backend, "Pstore backend to use");
861 
862 MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
863 MODULE_LICENSE("GPL");
864