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