xref: /openbmc/linux/fs/pstore/platform.c (revision f0f23e5469dc80b482d985898a930be0e249a162)
1ca01d6ddSTony Luck /*
2ca01d6ddSTony Luck  * Persistent Storage - platform driver interface parts.
3ca01d6ddSTony Luck  *
4f29e5956SAnton Vorontsov  * Copyright (C) 2007-2008 Google, Inc.
5ca01d6ddSTony Luck  * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com>
6ca01d6ddSTony Luck  *
7ca01d6ddSTony Luck  *  This program is free software; you can redistribute it and/or modify
8ca01d6ddSTony Luck  *  it under the terms of the GNU General Public License version 2 as
9ca01d6ddSTony Luck  *  published by the Free Software Foundation.
10ca01d6ddSTony Luck  *
11ca01d6ddSTony Luck  *  This program is distributed in the hope that it will be useful,
12ca01d6ddSTony Luck  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13ca01d6ddSTony Luck  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14ca01d6ddSTony Luck  *  GNU General Public License for more details.
15ca01d6ddSTony Luck  *
16ca01d6ddSTony Luck  *  You should have received a copy of the GNU General Public License
17ca01d6ddSTony Luck  *  along with this program; if not, write to the Free Software
18ca01d6ddSTony Luck  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19ca01d6ddSTony Luck  */
20ca01d6ddSTony Luck 
21ef748853SFabian Frederick #define pr_fmt(fmt) "pstore: " fmt
22ef748853SFabian Frederick 
23ca01d6ddSTony Luck #include <linux/atomic.h>
24ca01d6ddSTony Luck #include <linux/types.h>
25ca01d6ddSTony Luck #include <linux/errno.h>
26ca01d6ddSTony Luck #include <linux/init.h>
27ca01d6ddSTony Luck #include <linux/kmsg_dump.h>
28f29e5956SAnton Vorontsov #include <linux/console.h>
29ca01d6ddSTony Luck #include <linux/module.h>
30ca01d6ddSTony Luck #include <linux/pstore.h>
3158eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
328cfc8ddcSGeliang Tang #include <linux/lzo.h>
338cfc8ddcSGeliang Tang #endif
3458eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
358cfc8ddcSGeliang Tang #include <linux/lz4.h>
368cfc8ddcSGeliang Tang #endif
371021bcf4SGeliang Tang #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
381021bcf4SGeliang Tang #include <linux/zstd.h>
391021bcf4SGeliang Tang #endif
40cb3bee03SGeliang Tang #include <linux/crypto.h>
41ca01d6ddSTony Luck #include <linux/string.h>
426dda9266SLuck, Tony #include <linux/timer.h>
43ca01d6ddSTony Luck #include <linux/slab.h>
44ca01d6ddSTony Luck #include <linux/uaccess.h>
45a3f5f075SAnton Vorontsov #include <linux/jiffies.h>
466dda9266SLuck, Tony #include <linux/workqueue.h>
47ca01d6ddSTony Luck 
48ca01d6ddSTony Luck #include "internal.h"
49ca01d6ddSTony Luck 
50ca01d6ddSTony Luck /*
516dda9266SLuck, Tony  * We defer making "oops" entries appear in pstore - see
526dda9266SLuck, Tony  * whether the system is actually still running well enough
536dda9266SLuck, Tony  * to let someone see the entry
546dda9266SLuck, Tony  */
55521f7288SAnton Vorontsov static int pstore_update_ms = -1;
56a3f5f075SAnton Vorontsov module_param_named(update_ms, pstore_update_ms, int, 0600);
57a3f5f075SAnton Vorontsov MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content "
58521f7288SAnton Vorontsov 		 "(default is -1, which means runtime updates are disabled; "
59521f7288SAnton Vorontsov 		 "enabling this option is not safe, it may lead to further "
60521f7288SAnton Vorontsov 		 "corruption on Oopses)");
616dda9266SLuck, Tony 
62*f0f23e54SJoel Fernandes (Google) /* Names should be in the same order as the enum pstore_type_id */
63*f0f23e54SJoel Fernandes (Google) static const char * const pstore_type_names[] = {
64*f0f23e54SJoel Fernandes (Google) 	"dmesg",
65*f0f23e54SJoel Fernandes (Google) 	"mce",
66*f0f23e54SJoel Fernandes (Google) 	"console",
67*f0f23e54SJoel Fernandes (Google) 	"ftrace",
68*f0f23e54SJoel Fernandes (Google) 	"rtas",
69*f0f23e54SJoel Fernandes (Google) 	"powerpc-ofw",
70*f0f23e54SJoel Fernandes (Google) 	"powerpc-common",
71*f0f23e54SJoel Fernandes (Google) 	"pmsg",
72*f0f23e54SJoel Fernandes (Google) 	"powerpc-opal",
73*f0f23e54SJoel Fernandes (Google) };
74*f0f23e54SJoel Fernandes (Google) 
756dda9266SLuck, Tony static int pstore_new_entry;
766dda9266SLuck, Tony 
7724ed960aSKees Cook static void pstore_timefunc(struct timer_list *);
781d27e3e2SKees Cook static DEFINE_TIMER(pstore_timer, pstore_timefunc);
796dda9266SLuck, Tony 
806dda9266SLuck, Tony static void pstore_dowork(struct work_struct *);
816dda9266SLuck, Tony static DECLARE_WORK(pstore_work, pstore_dowork);
826dda9266SLuck, Tony 
836dda9266SLuck, Tony /*
84ca01d6ddSTony Luck  * pstore_lock just protects "psinfo" during
85ca01d6ddSTony Luck  * calls to pstore_register()
86ca01d6ddSTony Luck  */
87ca01d6ddSTony Luck static DEFINE_SPINLOCK(pstore_lock);
88060287b8SAnton Vorontsov struct pstore_info *psinfo;
89ca01d6ddSTony Luck 
90dee28e72SMatthew Garrett static char *backend;
91fe1d4758SKees Cook static char *compress =
92fe1d4758SKees Cook #ifdef CONFIG_PSTORE_COMPRESS_DEFAULT
93fe1d4758SKees Cook 		CONFIG_PSTORE_COMPRESS_DEFAULT;
94fe1d4758SKees Cook #else
95fe1d4758SKees Cook 		NULL;
96fe1d4758SKees Cook #endif
97dee28e72SMatthew Garrett 
98b0aad7a9SAruna Balakrishnaiah /* Compression parameters */
99cb3bee03SGeliang Tang static struct crypto_comp *tfm;
1008cfc8ddcSGeliang Tang 
1018cfc8ddcSGeliang Tang struct pstore_zbackend {
102cb3bee03SGeliang Tang 	int (*zbufsize)(size_t size);
1038cfc8ddcSGeliang Tang 	const char *name;
1048cfc8ddcSGeliang Tang };
105b0aad7a9SAruna Balakrishnaiah 
106b0aad7a9SAruna Balakrishnaiah static char *big_oops_buf;
107b0aad7a9SAruna Balakrishnaiah static size_t big_oops_buf_sz;
108b0aad7a9SAruna Balakrishnaiah 
109366f7e7aSLuck, Tony /* How much of the console log to snapshot */
110349d7438SDavid Howells unsigned long kmsg_bytes = PSTORE_DEFAULT_KMSG_BYTES;
111ca01d6ddSTony Luck 
112366f7e7aSLuck, Tony void pstore_set_kmsg_bytes(int bytes)
113ca01d6ddSTony Luck {
114366f7e7aSLuck, Tony 	kmsg_bytes = bytes;
115ca01d6ddSTony Luck }
116ca01d6ddSTony Luck 
117ca01d6ddSTony Luck /* Tag each group of saved records with a sequence number */
118ca01d6ddSTony Luck static int	oopscount;
119ca01d6ddSTony Luck 
120*f0f23e54SJoel Fernandes (Google) const char *pstore_type_to_name(enum pstore_type_id type)
121*f0f23e54SJoel Fernandes (Google) {
122*f0f23e54SJoel Fernandes (Google) 	BUILD_BUG_ON(ARRAY_SIZE(pstore_type_names) != PSTORE_TYPE_MAX);
123*f0f23e54SJoel Fernandes (Google) 
124*f0f23e54SJoel Fernandes (Google) 	if (WARN_ON_ONCE(type >= PSTORE_TYPE_MAX))
125*f0f23e54SJoel Fernandes (Google) 		return "unknown";
126*f0f23e54SJoel Fernandes (Google) 
127*f0f23e54SJoel Fernandes (Google) 	return pstore_type_names[type];
128*f0f23e54SJoel Fernandes (Google) }
129*f0f23e54SJoel Fernandes (Google) EXPORT_SYMBOL_GPL(pstore_type_to_name);
130*f0f23e54SJoel Fernandes (Google) 
131*f0f23e54SJoel Fernandes (Google) enum pstore_type_id pstore_name_to_type(const char *name)
132*f0f23e54SJoel Fernandes (Google) {
133*f0f23e54SJoel Fernandes (Google) 	int i;
134*f0f23e54SJoel Fernandes (Google) 
135*f0f23e54SJoel Fernandes (Google) 	for (i = 0; i < PSTORE_TYPE_MAX; i++) {
136*f0f23e54SJoel Fernandes (Google) 		if (!strcmp(pstore_type_names[i], name))
137*f0f23e54SJoel Fernandes (Google) 			return i;
138*f0f23e54SJoel Fernandes (Google) 	}
139*f0f23e54SJoel Fernandes (Google) 
140*f0f23e54SJoel Fernandes (Google) 	return PSTORE_TYPE_MAX;
141*f0f23e54SJoel Fernandes (Google) }
142*f0f23e54SJoel Fernandes (Google) EXPORT_SYMBOL_GPL(pstore_name_to_type);
143*f0f23e54SJoel Fernandes (Google) 
144381b872cSSeiji Aguchi static const char *get_reason_str(enum kmsg_dump_reason reason)
145381b872cSSeiji Aguchi {
146381b872cSSeiji Aguchi 	switch (reason) {
147381b872cSSeiji Aguchi 	case KMSG_DUMP_PANIC:
148381b872cSSeiji Aguchi 		return "Panic";
149381b872cSSeiji Aguchi 	case KMSG_DUMP_OOPS:
150381b872cSSeiji Aguchi 		return "Oops";
151381b872cSSeiji Aguchi 	case KMSG_DUMP_EMERG:
152381b872cSSeiji Aguchi 		return "Emergency";
153381b872cSSeiji Aguchi 	case KMSG_DUMP_RESTART:
154381b872cSSeiji Aguchi 		return "Restart";
155381b872cSSeiji Aguchi 	case KMSG_DUMP_HALT:
156381b872cSSeiji Aguchi 		return "Halt";
157381b872cSSeiji Aguchi 	case KMSG_DUMP_POWEROFF:
158381b872cSSeiji Aguchi 		return "Poweroff";
159381b872cSSeiji Aguchi 	default:
160381b872cSSeiji Aguchi 		return "Unknown";
161381b872cSSeiji Aguchi 	}
162381b872cSSeiji Aguchi }
1639f6af27fSTony Luck 
1649f244e9cSSeiji Aguchi bool pstore_cannot_block_path(enum kmsg_dump_reason reason)
1659f244e9cSSeiji Aguchi {
1669f244e9cSSeiji Aguchi 	/*
1679f244e9cSSeiji Aguchi 	 * In case of NMI path, pstore shouldn't be blocked
1689f244e9cSSeiji Aguchi 	 * regardless of reason.
1699f244e9cSSeiji Aguchi 	 */
1709f244e9cSSeiji Aguchi 	if (in_nmi())
1719f244e9cSSeiji Aguchi 		return true;
1729f244e9cSSeiji Aguchi 
1739f244e9cSSeiji Aguchi 	switch (reason) {
1749f244e9cSSeiji Aguchi 	/* In panic case, other cpus are stopped by smp_send_stop(). */
1759f244e9cSSeiji Aguchi 	case KMSG_DUMP_PANIC:
1769f244e9cSSeiji Aguchi 	/* Emergency restart shouldn't be blocked by spin lock. */
1779f244e9cSSeiji Aguchi 	case KMSG_DUMP_EMERG:
1789f244e9cSSeiji Aguchi 		return true;
1799f244e9cSSeiji Aguchi 	default:
1809f244e9cSSeiji Aguchi 		return false;
1819f244e9cSSeiji Aguchi 	}
1829f244e9cSSeiji Aguchi }
1839f244e9cSSeiji Aguchi EXPORT_SYMBOL_GPL(pstore_cannot_block_path);
1849f244e9cSSeiji Aguchi 
18558eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
186cb3bee03SGeliang Tang static int zbufsize_deflate(size_t size)
187b0aad7a9SAruna Balakrishnaiah {
1887de8fe2fSAruna Balakrishnaiah 	size_t cmpr;
189b0aad7a9SAruna Balakrishnaiah 
190cb3bee03SGeliang Tang 	switch (size) {
1917de8fe2fSAruna Balakrishnaiah 	/* buffer range for efivars */
1927de8fe2fSAruna Balakrishnaiah 	case 1000 ... 2000:
1937de8fe2fSAruna Balakrishnaiah 		cmpr = 56;
1947de8fe2fSAruna Balakrishnaiah 		break;
1957de8fe2fSAruna Balakrishnaiah 	case 2001 ... 3000:
1967de8fe2fSAruna Balakrishnaiah 		cmpr = 54;
1977de8fe2fSAruna Balakrishnaiah 		break;
1987de8fe2fSAruna Balakrishnaiah 	case 3001 ... 3999:
1997de8fe2fSAruna Balakrishnaiah 		cmpr = 52;
2007de8fe2fSAruna Balakrishnaiah 		break;
2017de8fe2fSAruna Balakrishnaiah 	/* buffer range for nvram, erst */
2027de8fe2fSAruna Balakrishnaiah 	case 4000 ... 10000:
2037de8fe2fSAruna Balakrishnaiah 		cmpr = 45;
2047de8fe2fSAruna Balakrishnaiah 		break;
2057de8fe2fSAruna Balakrishnaiah 	default:
2067de8fe2fSAruna Balakrishnaiah 		cmpr = 60;
2077de8fe2fSAruna Balakrishnaiah 		break;
2087de8fe2fSAruna Balakrishnaiah 	}
2097de8fe2fSAruna Balakrishnaiah 
210cb3bee03SGeliang Tang 	return (size * 100) / cmpr;
2118cfc8ddcSGeliang Tang }
2128cfc8ddcSGeliang Tang #endif
2138cfc8ddcSGeliang Tang 
21458eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
215cb3bee03SGeliang Tang static int zbufsize_lzo(size_t size)
2168cfc8ddcSGeliang Tang {
217cb3bee03SGeliang Tang 	return lzo1x_worst_compress(size);
2188cfc8ddcSGeliang Tang }
2198cfc8ddcSGeliang Tang #endif
2208cfc8ddcSGeliang Tang 
22158eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
222cb3bee03SGeliang Tang static int zbufsize_lz4(size_t size)
2238cfc8ddcSGeliang Tang {
224cb3bee03SGeliang Tang 	return LZ4_compressBound(size);
225239b7161SGeliang Tang }
226239b7161SGeliang Tang #endif
227239b7161SGeliang Tang 
22858eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
229cb3bee03SGeliang Tang static int zbufsize_842(size_t size)
230239b7161SGeliang Tang {
23155597406SKees Cook 	return size;
232239b7161SGeliang Tang }
233fe1d4758SKees Cook #endif
2348cfc8ddcSGeliang Tang 
2351021bcf4SGeliang Tang #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
2361021bcf4SGeliang Tang static int zbufsize_zstd(size_t size)
2371021bcf4SGeliang Tang {
2381021bcf4SGeliang Tang 	return ZSTD_compressBound(size);
2391021bcf4SGeliang Tang }
2401021bcf4SGeliang Tang #endif
2411021bcf4SGeliang Tang 
242fe1d4758SKees Cook static const struct pstore_zbackend *zbackend __ro_after_init;
243fe1d4758SKees Cook 
244fe1d4758SKees Cook static const struct pstore_zbackend zbackends[] = {
24558eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
246fe1d4758SKees Cook 	{
247cb3bee03SGeliang Tang 		.zbufsize	= zbufsize_deflate,
248cb3bee03SGeliang Tang 		.name		= "deflate",
249fe1d4758SKees Cook 	},
250fe1d4758SKees Cook #endif
25158eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
252fe1d4758SKees Cook 	{
253cb3bee03SGeliang Tang 		.zbufsize	= zbufsize_lzo,
254fe1d4758SKees Cook 		.name		= "lzo",
255fe1d4758SKees Cook 	},
256fe1d4758SKees Cook #endif
25758eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS)
258fe1d4758SKees Cook 	{
259cb3bee03SGeliang Tang 		.zbufsize	= zbufsize_lz4,
260fe1d4758SKees Cook 		.name		= "lz4",
261fe1d4758SKees Cook 	},
262fe1d4758SKees Cook #endif
26358eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
264fe1d4758SKees Cook 	{
265cb3bee03SGeliang Tang 		.zbufsize	= zbufsize_lz4,
266fe1d4758SKees Cook 		.name		= "lz4hc",
267fe1d4758SKees Cook 	},
268fe1d4758SKees Cook #endif
26958eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
270fe1d4758SKees Cook 	{
271cb3bee03SGeliang Tang 		.zbufsize	= zbufsize_842,
272239b7161SGeliang Tang 		.name		= "842",
273fe1d4758SKees Cook 	},
274fe1d4758SKees Cook #endif
2751021bcf4SGeliang Tang #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
2761021bcf4SGeliang Tang 	{
2771021bcf4SGeliang Tang 		.zbufsize	= zbufsize_zstd,
2781021bcf4SGeliang Tang 		.name		= "zstd",
2791021bcf4SGeliang Tang 	},
2801021bcf4SGeliang Tang #endif
281fe1d4758SKees Cook 	{ }
2828cfc8ddcSGeliang Tang };
2838cfc8ddcSGeliang Tang 
2848cfc8ddcSGeliang Tang static int pstore_compress(const void *in, void *out,
285cb3bee03SGeliang Tang 			   unsigned int inlen, unsigned int outlen)
2868cfc8ddcSGeliang Tang {
287cb3bee03SGeliang Tang 	int ret;
288cb3bee03SGeliang Tang 
289cb3bee03SGeliang Tang 	ret = crypto_comp_compress(tfm, in, inlen, out, &outlen);
290cb3bee03SGeliang Tang 	if (ret) {
291cb3bee03SGeliang Tang 		pr_err("crypto_comp_compress failed, ret = %d!\n", ret);
292cb3bee03SGeliang Tang 		return ret;
2938cfc8ddcSGeliang Tang 	}
2948cfc8ddcSGeliang Tang 
295cb3bee03SGeliang Tang 	return outlen;
296cb3bee03SGeliang Tang }
297cb3bee03SGeliang Tang 
2988cfc8ddcSGeliang Tang static void allocate_buf_for_compression(void)
2998cfc8ddcSGeliang Tang {
30095047b05SKees Cook 	struct crypto_comp *ctx;
30195047b05SKees Cook 	int size;
30295047b05SKees Cook 	char *buf;
30395047b05SKees Cook 
30495047b05SKees Cook 	/* Skip if not built-in or compression backend not selected yet. */
305e698aaf3STobias Regnery 	if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !zbackend)
306cb3bee03SGeliang Tang 		return;
307cb3bee03SGeliang Tang 
30895047b05SKees Cook 	/* Skip if no pstore backend yet or compression init already done. */
30995047b05SKees Cook 	if (!psinfo || tfm)
31095047b05SKees Cook 		return;
31195047b05SKees Cook 
312cb3bee03SGeliang Tang 	if (!crypto_has_comp(zbackend->name, 0, 0)) {
31395047b05SKees Cook 		pr_err("Unknown compression: %s\n", zbackend->name);
314cb3bee03SGeliang Tang 		return;
315cb3bee03SGeliang Tang 	}
316cb3bee03SGeliang Tang 
31795047b05SKees Cook 	size = zbackend->zbufsize(psinfo->bufsize);
31895047b05SKees Cook 	if (size <= 0) {
31995047b05SKees Cook 		pr_err("Invalid compression size for %s: %d\n",
32095047b05SKees Cook 		       zbackend->name, size);
321cb3bee03SGeliang Tang 		return;
322cb3bee03SGeliang Tang 	}
323cb3bee03SGeliang Tang 
32495047b05SKees Cook 	buf = kmalloc(size, GFP_KERNEL);
32595047b05SKees Cook 	if (!buf) {
32695047b05SKees Cook 		pr_err("Failed %d byte compression buffer allocation for: %s\n",
32795047b05SKees Cook 		       size, zbackend->name);
328cb3bee03SGeliang Tang 		return;
3298cfc8ddcSGeliang Tang 	}
33095047b05SKees Cook 
33195047b05SKees Cook 	ctx = crypto_alloc_comp(zbackend->name, 0, 0);
33295047b05SKees Cook 	if (IS_ERR_OR_NULL(ctx)) {
33395047b05SKees Cook 		kfree(buf);
33495047b05SKees Cook 		pr_err("crypto_alloc_comp('%s') failed: %ld\n", zbackend->name,
33595047b05SKees Cook 		       PTR_ERR(ctx));
33695047b05SKees Cook 		return;
33795047b05SKees Cook 	}
33895047b05SKees Cook 
33995047b05SKees Cook 	/* A non-NULL big_oops_buf indicates compression is available. */
34095047b05SKees Cook 	tfm = ctx;
34195047b05SKees Cook 	big_oops_buf_sz = size;
34295047b05SKees Cook 	big_oops_buf = buf;
34395047b05SKees Cook 
3440eed84ffSKees Cook 	pr_info("Using crash dump compression: %s\n", zbackend->name);
3458cfc8ddcSGeliang Tang }
3468cfc8ddcSGeliang Tang 
3478cfc8ddcSGeliang Tang static void free_buf_for_compression(void)
3488cfc8ddcSGeliang Tang {
34995047b05SKees Cook 	if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && tfm)
350cb3bee03SGeliang Tang 		crypto_free_comp(tfm);
351cb3bee03SGeliang Tang 	kfree(big_oops_buf);
352cb3bee03SGeliang Tang 	big_oops_buf = NULL;
353cb3bee03SGeliang Tang 	big_oops_buf_sz = 0;
354ee1d2674SGeliang Tang }
355ee1d2674SGeliang Tang 
356b0aad7a9SAruna Balakrishnaiah /*
357b0aad7a9SAruna Balakrishnaiah  * Called when compression fails, since the printk buffer
358b0aad7a9SAruna Balakrishnaiah  * would be fetched for compression calling it again when
359b0aad7a9SAruna Balakrishnaiah  * compression fails would have moved the iterator of
360b0aad7a9SAruna Balakrishnaiah  * printk buffer which results in fetching old contents.
361b0aad7a9SAruna Balakrishnaiah  * Copy the recent messages from big_oops_buf to psinfo->buf
362b0aad7a9SAruna Balakrishnaiah  */
363b0aad7a9SAruna Balakrishnaiah static size_t copy_kmsg_to_buffer(int hsize, size_t len)
364b0aad7a9SAruna Balakrishnaiah {
365b0aad7a9SAruna Balakrishnaiah 	size_t total_len;
366b0aad7a9SAruna Balakrishnaiah 	size_t diff;
367b0aad7a9SAruna Balakrishnaiah 
368b0aad7a9SAruna Balakrishnaiah 	total_len = hsize + len;
369b0aad7a9SAruna Balakrishnaiah 
370b0aad7a9SAruna Balakrishnaiah 	if (total_len > psinfo->bufsize) {
371b0aad7a9SAruna Balakrishnaiah 		diff = total_len - psinfo->bufsize + hsize;
372b0aad7a9SAruna Balakrishnaiah 		memcpy(psinfo->buf, big_oops_buf, hsize);
373b0aad7a9SAruna Balakrishnaiah 		memcpy(psinfo->buf + hsize, big_oops_buf + diff,
374b0aad7a9SAruna Balakrishnaiah 					psinfo->bufsize - hsize);
375b0aad7a9SAruna Balakrishnaiah 		total_len = psinfo->bufsize;
376b0aad7a9SAruna Balakrishnaiah 	} else
377b0aad7a9SAruna Balakrishnaiah 		memcpy(psinfo->buf, big_oops_buf, total_len);
378b0aad7a9SAruna Balakrishnaiah 
379b0aad7a9SAruna Balakrishnaiah 	return total_len;
380b0aad7a9SAruna Balakrishnaiah }
381b0aad7a9SAruna Balakrishnaiah 
382e581ca81SKees Cook void pstore_record_init(struct pstore_record *record,
383e581ca81SKees Cook 			struct pstore_info *psinfo)
384e581ca81SKees Cook {
385e581ca81SKees Cook 	memset(record, 0, sizeof(*record));
386e581ca81SKees Cook 
387e581ca81SKees Cook 	record->psi = psinfo;
388c7f3c595SKees Cook 
389c7f3c595SKees Cook 	/* Report zeroed timestamp if called before timekeeping has resumed. */
3907aaa822eSKees Cook 	record->time = ns_to_timespec64(ktime_get_real_fast_ns());
391e581ca81SKees Cook }
392e581ca81SKees Cook 
393ca01d6ddSTony Luck /*
3940eed84ffSKees Cook  * callback from kmsg_dump. Save as much as we can (up to kmsg_bytes) from the
3950eed84ffSKees Cook  * end of the buffer.
396ca01d6ddSTony Luck  */
397ca01d6ddSTony Luck static void pstore_dump(struct kmsg_dumper *dumper,
398e2ae715dSKay Sievers 			enum kmsg_dump_reason reason)
399ca01d6ddSTony Luck {
400e2ae715dSKay Sievers 	unsigned long	total = 0;
401381b872cSSeiji Aguchi 	const char	*why;
402b94fdd07SMatthew Garrett 	unsigned int	part = 1;
403abd4d558SDon Zickus 	unsigned long	flags = 0;
40498e44fdaSNamhyung Kim 	int		is_locked;
405e2ae715dSKay Sievers 	int		ret;
406ca01d6ddSTony Luck 
407381b872cSSeiji Aguchi 	why = get_reason_str(reason);
4089f6af27fSTony Luck 
4099f244e9cSSeiji Aguchi 	if (pstore_cannot_block_path(reason)) {
4109f244e9cSSeiji Aguchi 		is_locked = spin_trylock_irqsave(&psinfo->buf_lock, flags);
4119f244e9cSSeiji Aguchi 		if (!is_locked) {
4129f244e9cSSeiji Aguchi 			pr_err("pstore dump routine blocked in %s path, may corrupt error record\n"
4139f244e9cSSeiji Aguchi 				       , in_nmi() ? "NMI" : why);
414959217c8SLi Pengcheng 			return;
4159f244e9cSSeiji Aguchi 		}
41698e44fdaSNamhyung Kim 	} else {
417abd4d558SDon Zickus 		spin_lock_irqsave(&psinfo->buf_lock, flags);
41898e44fdaSNamhyung Kim 		is_locked = 1;
41998e44fdaSNamhyung Kim 	}
420ca01d6ddSTony Luck 	oopscount++;
421ca01d6ddSTony Luck 	while (total < kmsg_bytes) {
422e2ae715dSKay Sievers 		char *dst;
42376cc9580SKees Cook 		size_t dst_size;
42476cc9580SKees Cook 		int header_size;
425b0aad7a9SAruna Balakrishnaiah 		int zipped_len = -1;
42676cc9580SKees Cook 		size_t dump_size;
427e581ca81SKees Cook 		struct pstore_record record;
428e581ca81SKees Cook 
429e581ca81SKees Cook 		pstore_record_init(&record, psinfo);
430e581ca81SKees Cook 		record.type = PSTORE_TYPE_DMESG;
431e581ca81SKees Cook 		record.count = oopscount;
432e581ca81SKees Cook 		record.reason = reason;
433e581ca81SKees Cook 		record.part = part;
434e581ca81SKees Cook 		record.buf = psinfo->buf;
435e2ae715dSKay Sievers 
436f0e2efcfSKonstantin Khlebnikov 		if (big_oops_buf && is_locked) {
437b0aad7a9SAruna Balakrishnaiah 			dst = big_oops_buf;
43876cc9580SKees Cook 			dst_size = big_oops_buf_sz;
439235f6d15SNamhyung Kim 		} else {
440235f6d15SNamhyung Kim 			dst = psinfo->buf;
44176cc9580SKees Cook 			dst_size = psinfo->bufsize;
442235f6d15SNamhyung Kim 		}
443235f6d15SNamhyung Kim 
44476cc9580SKees Cook 		/* Write dump header. */
44576cc9580SKees Cook 		header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why,
44676cc9580SKees Cook 				 oopscount, part);
44776cc9580SKees Cook 		dst_size -= header_size;
448b0aad7a9SAruna Balakrishnaiah 
44976cc9580SKees Cook 		/* Write dump contents. */
45076cc9580SKees Cook 		if (!kmsg_dump_get_buffer(dumper, true, dst + header_size,
45176cc9580SKees Cook 					  dst_size, &dump_size))
452b0aad7a9SAruna Balakrishnaiah 			break;
453b0aad7a9SAruna Balakrishnaiah 
454235f6d15SNamhyung Kim 		if (big_oops_buf && is_locked) {
455b0aad7a9SAruna Balakrishnaiah 			zipped_len = pstore_compress(dst, psinfo->buf,
45676cc9580SKees Cook 						header_size + dump_size,
45776cc9580SKees Cook 						psinfo->bufsize);
458b0aad7a9SAruna Balakrishnaiah 
459b0aad7a9SAruna Balakrishnaiah 			if (zipped_len > 0) {
46076cc9580SKees Cook 				record.compressed = true;
46176cc9580SKees Cook 				record.size = zipped_len;
462b0aad7a9SAruna Balakrishnaiah 			} else {
46376cc9580SKees Cook 				record.size = copy_kmsg_to_buffer(header_size,
46476cc9580SKees Cook 								  dump_size);
465b0aad7a9SAruna Balakrishnaiah 			}
466b0aad7a9SAruna Balakrishnaiah 		} else {
46776cc9580SKees Cook 			record.size = header_size + dump_size;
468b0aad7a9SAruna Balakrishnaiah 		}
469b0aad7a9SAruna Balakrishnaiah 
47076cc9580SKees Cook 		ret = psinfo->write(&record);
471b238b8faSChen Gong 		if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted())
4726dda9266SLuck, Tony 			pstore_new_entry = 1;
473e2ae715dSKay Sievers 
47476cc9580SKees Cook 		total += record.size;
47556280682SMatthew Garrett 		part++;
476ca01d6ddSTony Luck 	}
477abd4d558SDon Zickus 	if (is_locked)
4789f244e9cSSeiji Aguchi 		spin_unlock_irqrestore(&psinfo->buf_lock, flags);
479ca01d6ddSTony Luck }
480ca01d6ddSTony Luck 
481ca01d6ddSTony Luck static struct kmsg_dumper pstore_dumper = {
482ca01d6ddSTony Luck 	.dump = pstore_dump,
483ca01d6ddSTony Luck };
484ca01d6ddSTony Luck 
485306e5c2aSGeliang Tang /*
486306e5c2aSGeliang Tang  * Register with kmsg_dump to save last part of console log on panic.
487306e5c2aSGeliang Tang  */
48818730411SGeliang Tang static void pstore_register_kmsg(void)
48918730411SGeliang Tang {
49018730411SGeliang Tang 	kmsg_dump_register(&pstore_dumper);
49118730411SGeliang Tang }
49218730411SGeliang Tang 
493ee1d2674SGeliang Tang static void pstore_unregister_kmsg(void)
494ee1d2674SGeliang Tang {
495ee1d2674SGeliang Tang 	kmsg_dump_unregister(&pstore_dumper);
496ee1d2674SGeliang Tang }
497ee1d2674SGeliang Tang 
498f29e5956SAnton Vorontsov #ifdef CONFIG_PSTORE_CONSOLE
499f29e5956SAnton Vorontsov static void pstore_console_write(struct console *con, const char *s, unsigned c)
500f29e5956SAnton Vorontsov {
501e581ca81SKees Cook 	struct pstore_record record;
502f29e5956SAnton Vorontsov 
503e581ca81SKees Cook 	pstore_record_init(&record, psinfo);
504e581ca81SKees Cook 	record.type = PSTORE_TYPE_CONSOLE;
505e581ca81SKees Cook 
506b10b4711SKees Cook 	record.buf = (char *)s;
507b10b4711SKees Cook 	record.size = c;
5084c9ec219SKees Cook 	psinfo->write(&record);
509f29e5956SAnton Vorontsov }
510f29e5956SAnton Vorontsov 
511f29e5956SAnton Vorontsov static struct console pstore_console = {
512f29e5956SAnton Vorontsov 	.name	= "pstore",
513f29e5956SAnton Vorontsov 	.write	= pstore_console_write,
514f29e5956SAnton Vorontsov 	.flags	= CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME,
515f29e5956SAnton Vorontsov 	.index	= -1,
516f29e5956SAnton Vorontsov };
517f29e5956SAnton Vorontsov 
518f29e5956SAnton Vorontsov static void pstore_register_console(void)
519f29e5956SAnton Vorontsov {
520f29e5956SAnton Vorontsov 	register_console(&pstore_console);
521f29e5956SAnton Vorontsov }
522ee1d2674SGeliang Tang 
523ee1d2674SGeliang Tang static void pstore_unregister_console(void)
524ee1d2674SGeliang Tang {
525ee1d2674SGeliang Tang 	unregister_console(&pstore_console);
526ee1d2674SGeliang Tang }
527f29e5956SAnton Vorontsov #else
528f29e5956SAnton Vorontsov static void pstore_register_console(void) {}
529ee1d2674SGeliang Tang static void pstore_unregister_console(void) {}
530f29e5956SAnton Vorontsov #endif
531f29e5956SAnton Vorontsov 
5324c9ec219SKees Cook static int pstore_write_user_compat(struct pstore_record *record,
533fdd03118SKees Cook 				    const char __user *buf)
5345bf6d1b9SMark Salyzyn {
53530800d99SKees Cook 	int ret = 0;
5365bf6d1b9SMark Salyzyn 
53730800d99SKees Cook 	if (record->buf)
53830800d99SKees Cook 		return -EINVAL;
5395bf6d1b9SMark Salyzyn 
540077090afSGeliang Tang 	record->buf = memdup_user(buf, record->size);
541dfd6fa39SHirofumi Nakagawa 	if (IS_ERR(record->buf)) {
542077090afSGeliang Tang 		ret = PTR_ERR(record->buf);
54330800d99SKees Cook 		goto out;
5445bf6d1b9SMark Salyzyn 	}
54530800d99SKees Cook 
5464c9ec219SKees Cook 	ret = record->psi->write(record);
54730800d99SKees Cook 
54830800d99SKees Cook 	kfree(record->buf);
549077090afSGeliang Tang out:
55030800d99SKees Cook 	record->buf = NULL;
55130800d99SKees Cook 
55230800d99SKees Cook 	return unlikely(ret < 0) ? ret : record->size;
5535bf6d1b9SMark Salyzyn }
5545bf6d1b9SMark Salyzyn 
555ca01d6ddSTony Luck /*
556ca01d6ddSTony Luck  * platform specific persistent storage driver registers with
557ca01d6ddSTony Luck  * us here. If pstore is already mounted, call the platform
558ca01d6ddSTony Luck  * read function right away to populate the file system. If not
559ca01d6ddSTony Luck  * then the pstore mount code will call us later to fill out
560ca01d6ddSTony Luck  * the file system.
561ca01d6ddSTony Luck  */
562ca01d6ddSTony Luck int pstore_register(struct pstore_info *psi)
563ca01d6ddSTony Luck {
564ca01d6ddSTony Luck 	struct module *owner = psi->owner;
565ca01d6ddSTony Luck 
5660d7cd09aSKees Cook 	if (backend && strcmp(backend, psi->name)) {
5670d7cd09aSKees Cook 		pr_warn("ignoring unexpected backend '%s'\n", psi->name);
5688e48b1a8SLenny Szubowicz 		return -EPERM;
5690d7cd09aSKees Cook 	}
5708e48b1a8SLenny Szubowicz 
5714c9ec219SKees Cook 	/* Sanity check flags. */
5724c9ec219SKees Cook 	if (!psi->flags) {
5734c9ec219SKees Cook 		pr_warn("backend '%s' must support at least one frontend\n",
5744c9ec219SKees Cook 			psi->name);
5754c9ec219SKees Cook 		return -EINVAL;
5764c9ec219SKees Cook 	}
5774c9ec219SKees Cook 
5784c9ec219SKees Cook 	/* Check for required functions. */
5794c9ec219SKees Cook 	if (!psi->read || !psi->write) {
5804c9ec219SKees Cook 		pr_warn("backend '%s' must implement read() and write()\n",
5814c9ec219SKees Cook 			psi->name);
5824c9ec219SKees Cook 		return -EINVAL;
5834c9ec219SKees Cook 	}
5844c9ec219SKees Cook 
585ca01d6ddSTony Luck 	spin_lock(&pstore_lock);
586ca01d6ddSTony Luck 	if (psinfo) {
5870d7cd09aSKees Cook 		pr_warn("backend '%s' already loaded: ignoring '%s'\n",
5880d7cd09aSKees Cook 			psinfo->name, psi->name);
589ca01d6ddSTony Luck 		spin_unlock(&pstore_lock);
590ca01d6ddSTony Luck 		return -EBUSY;
591ca01d6ddSTony Luck 	}
592dee28e72SMatthew Garrett 
5934c9ec219SKees Cook 	if (!psi->write_user)
5944c9ec219SKees Cook 		psi->write_user = pstore_write_user_compat;
595ca01d6ddSTony Luck 	psinfo = psi;
596f6f82851SKees Cook 	mutex_init(&psinfo->read_mutex);
597ca01d6ddSTony Luck 	spin_unlock(&pstore_lock);
598ca01d6ddSTony Luck 
599ca01d6ddSTony Luck 	if (owner && !try_module_get(owner)) {
600ca01d6ddSTony Luck 		psinfo = NULL;
601ca01d6ddSTony Luck 		return -EINVAL;
602ca01d6ddSTony Luck 	}
603ca01d6ddSTony Luck 
604b0aad7a9SAruna Balakrishnaiah 	allocate_buf_for_compression();
605b0aad7a9SAruna Balakrishnaiah 
606ca01d6ddSTony Luck 	if (pstore_is_mounted())
6076dda9266SLuck, Tony 		pstore_get_records(0);
608ca01d6ddSTony Luck 
609c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_DMESG)
61018730411SGeliang Tang 		pstore_register_kmsg();
611c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_CONSOLE)
612f29e5956SAnton Vorontsov 		pstore_register_console();
613c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_FTRACE)
61465f8c95eSAnton Vorontsov 		pstore_register_ftrace();
615c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_PMSG)
6169d5438f4SMark Salyzyn 		pstore_register_pmsg();
617ca01d6ddSTony Luck 
6186330d553SKees Cook 	/* Start watching for new records, if desired. */
619a3f5f075SAnton Vorontsov 	if (pstore_update_ms >= 0) {
620a3f5f075SAnton Vorontsov 		pstore_timer.expires = jiffies +
621a3f5f075SAnton Vorontsov 			msecs_to_jiffies(pstore_update_ms);
6226dda9266SLuck, Tony 		add_timer(&pstore_timer);
623a3f5f075SAnton Vorontsov 	}
6246dda9266SLuck, Tony 
62542222c2aSWang Long 	/*
62642222c2aSWang Long 	 * Update the module parameter backend, so it is visible
62742222c2aSWang Long 	 * through /sys/module/pstore/parameters/backend
62842222c2aSWang Long 	 */
62942222c2aSWang Long 	backend = psi->name;
63042222c2aSWang Long 
631ef748853SFabian Frederick 	pr_info("Registered %s as persistent store backend\n", psi->name);
6328e48b1a8SLenny Szubowicz 
6331344dd86SKees Cook 	module_put(owner);
6341344dd86SKees Cook 
635ca01d6ddSTony Luck 	return 0;
636ca01d6ddSTony Luck }
637ca01d6ddSTony Luck EXPORT_SYMBOL_GPL(pstore_register);
638ca01d6ddSTony Luck 
639ee1d2674SGeliang Tang void pstore_unregister(struct pstore_info *psi)
640ee1d2674SGeliang Tang {
6416330d553SKees Cook 	/* Stop timer and make sure all work has finished. */
6426330d553SKees Cook 	pstore_update_ms = -1;
6436330d553SKees Cook 	del_timer_sync(&pstore_timer);
6446330d553SKees Cook 	flush_work(&pstore_work);
6456330d553SKees Cook 
646c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_PMSG)
647ee1d2674SGeliang Tang 		pstore_unregister_pmsg();
648c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_FTRACE)
649ee1d2674SGeliang Tang 		pstore_unregister_ftrace();
650c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_CONSOLE)
651ee1d2674SGeliang Tang 		pstore_unregister_console();
652c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_DMESG)
653ee1d2674SGeliang Tang 		pstore_unregister_kmsg();
654ee1d2674SGeliang Tang 
655ee1d2674SGeliang Tang 	free_buf_for_compression();
656ee1d2674SGeliang Tang 
657ee1d2674SGeliang Tang 	psinfo = NULL;
658ee1d2674SGeliang Tang 	backend = NULL;
659ee1d2674SGeliang Tang }
660ee1d2674SGeliang Tang EXPORT_SYMBOL_GPL(pstore_unregister);
661ee1d2674SGeliang Tang 
662634f8f51SKees Cook static void decompress_record(struct pstore_record *record)
663634f8f51SKees Cook {
664bdabc8e7SKees Cook 	int ret;
665634f8f51SKees Cook 	int unzipped_len;
666bdabc8e7SKees Cook 	char *unzipped, *workspace;
667634f8f51SKees Cook 
6684a16d1cbSAnkit Kumar 	if (!record->compressed)
6694a16d1cbSAnkit Kumar 		return;
6704a16d1cbSAnkit Kumar 
671634f8f51SKees Cook 	/* Only PSTORE_TYPE_DMESG support compression. */
6724a16d1cbSAnkit Kumar 	if (record->type != PSTORE_TYPE_DMESG) {
673634f8f51SKees Cook 		pr_warn("ignored compressed record type %d\n", record->type);
674634f8f51SKees Cook 		return;
675634f8f51SKees Cook 	}
676634f8f51SKees Cook 
677bdabc8e7SKees Cook 	/* Missing compression buffer means compression was not initialized. */
678634f8f51SKees Cook 	if (!big_oops_buf) {
679bdabc8e7SKees Cook 		pr_warn("no decompression method initialized!\n");
680634f8f51SKees Cook 		return;
681634f8f51SKees Cook 	}
682634f8f51SKees Cook 
683bdabc8e7SKees Cook 	/* Allocate enough space to hold max decompression and ECC. */
684bdabc8e7SKees Cook 	unzipped_len = big_oops_buf_sz;
685bdabc8e7SKees Cook 	workspace = kmalloc(unzipped_len + record->ecc_notice_size,
6867e8cc8dcSKees Cook 			    GFP_KERNEL);
687bdabc8e7SKees Cook 	if (!workspace)
688bdabc8e7SKees Cook 		return;
689bdabc8e7SKees Cook 
690bdabc8e7SKees Cook 	/* After decompression "unzipped_len" is almost certainly smaller. */
691bdabc8e7SKees Cook 	ret = crypto_comp_decompress(tfm, record->buf, record->size,
692bdabc8e7SKees Cook 					  workspace, &unzipped_len);
693bdabc8e7SKees Cook 	if (ret) {
694bdabc8e7SKees Cook 		pr_err("crypto_comp_decompress failed, ret = %d!\n", ret);
695bdabc8e7SKees Cook 		kfree(workspace);
6967e8cc8dcSKees Cook 		return;
6977e8cc8dcSKees Cook 	}
6987e8cc8dcSKees Cook 
6997e8cc8dcSKees Cook 	/* Append ECC notice to decompressed buffer. */
700bdabc8e7SKees Cook 	memcpy(workspace + unzipped_len, record->buf + record->size,
701634f8f51SKees Cook 	       record->ecc_notice_size);
7027e8cc8dcSKees Cook 
703bdabc8e7SKees Cook 	/* Copy decompressed contents into an minimum-sized allocation. */
704bdabc8e7SKees Cook 	unzipped = kmemdup(workspace, unzipped_len + record->ecc_notice_size,
705bdabc8e7SKees Cook 			   GFP_KERNEL);
706bdabc8e7SKees Cook 	kfree(workspace);
707bdabc8e7SKees Cook 	if (!unzipped)
708bdabc8e7SKees Cook 		return;
709bdabc8e7SKees Cook 
710bdabc8e7SKees Cook 	/* Swap out compressed contents with decompressed contents. */
711634f8f51SKees Cook 	kfree(record->buf);
712bdabc8e7SKees Cook 	record->buf = unzipped;
713634f8f51SKees Cook 	record->size = unzipped_len;
714634f8f51SKees Cook 	record->compressed = false;
715634f8f51SKees Cook }
716634f8f51SKees Cook 
717ca01d6ddSTony Luck /*
7183a7d2fd1SKees Cook  * Read all the records from one persistent store backend. Create
7196dda9266SLuck, Tony  * files in our filesystem.  Don't warn about -EEXIST errors
7206dda9266SLuck, Tony  * when we are re-scanning the backing store looking to add new
7216dda9266SLuck, Tony  * error records.
722ca01d6ddSTony Luck  */
7233a7d2fd1SKees Cook void pstore_get_backend_records(struct pstore_info *psi,
7243a7d2fd1SKees Cook 				struct dentry *root, int quiet)
725ca01d6ddSTony Luck {
7262a2b0acfSKees Cook 	int failed = 0;
727656de42eSKees Cook 	unsigned int stop_loop = 65536;
728ca01d6ddSTony Luck 
7293a7d2fd1SKees Cook 	if (!psi || !root)
730ca01d6ddSTony Luck 		return;
731ca01d6ddSTony Luck 
732f6f82851SKees Cook 	mutex_lock(&psi->read_mutex);
7332174f6dfSKees Cook 	if (psi->open && psi->open(psi))
73406cf91b4SChen Gong 		goto out;
73506cf91b4SChen Gong 
7361dfff7ddSKees Cook 	/*
7371dfff7ddSKees Cook 	 * Backend callback read() allocates record.buf. decompress_record()
7381dfff7ddSKees Cook 	 * may reallocate record.buf. On success, pstore_mkfile() will keep
7391dfff7ddSKees Cook 	 * the record.buf, so free it only on failure.
7401dfff7ddSKees Cook 	 */
741656de42eSKees Cook 	for (; stop_loop; stop_loop--) {
7422a2b0acfSKees Cook 		struct pstore_record *record;
7432a2b0acfSKees Cook 		int rc;
7442a2b0acfSKees Cook 
7452a2b0acfSKees Cook 		record = kzalloc(sizeof(*record), GFP_KERNEL);
7462a2b0acfSKees Cook 		if (!record) {
7472a2b0acfSKees Cook 			pr_err("out of memory creating record\n");
7482a2b0acfSKees Cook 			break;
7492a2b0acfSKees Cook 		}
750e581ca81SKees Cook 		pstore_record_init(record, psi);
7512a2b0acfSKees Cook 
7522a2b0acfSKees Cook 		record->size = psi->read(record);
7532a2b0acfSKees Cook 
7542a2b0acfSKees Cook 		/* No more records left in backend? */
755f6525b96SDouglas Anderson 		if (record->size <= 0) {
756f6525b96SDouglas Anderson 			kfree(record);
7572a2b0acfSKees Cook 			break;
758f6525b96SDouglas Anderson 		}
7592a2b0acfSKees Cook 
7602a2b0acfSKees Cook 		decompress_record(record);
7613a7d2fd1SKees Cook 		rc = pstore_mkfile(root, record);
7621dfff7ddSKees Cook 		if (rc) {
76383f70f07SKees Cook 			/* pstore_mkfile() did not take record, so free it. */
7642a2b0acfSKees Cook 			kfree(record->buf);
76583f70f07SKees Cook 			kfree(record);
7661dfff7ddSKees Cook 			if (rc != -EEXIST || !quiet)
7671dfff7ddSKees Cook 				failed++;
7681dfff7ddSKees Cook 		}
769ca01d6ddSTony Luck 	}
7702174f6dfSKees Cook 	if (psi->close)
77106cf91b4SChen Gong 		psi->close(psi);
77206cf91b4SChen Gong out:
773f6f82851SKees Cook 	mutex_unlock(&psi->read_mutex);
774ca01d6ddSTony Luck 
775ca01d6ddSTony Luck 	if (failed)
776656de42eSKees Cook 		pr_warn("failed to create %d record(s) from '%s'\n",
777ca01d6ddSTony Luck 			failed, psi->name);
778656de42eSKees Cook 	if (!stop_loop)
779656de42eSKees Cook 		pr_err("looping? Too many records seen from '%s'\n",
780656de42eSKees Cook 			psi->name);
781ca01d6ddSTony Luck }
782ca01d6ddSTony Luck 
7836dda9266SLuck, Tony static void pstore_dowork(struct work_struct *work)
7846dda9266SLuck, Tony {
7856dda9266SLuck, Tony 	pstore_get_records(1);
7866dda9266SLuck, Tony }
7876dda9266SLuck, Tony 
78824ed960aSKees Cook static void pstore_timefunc(struct timer_list *unused)
7896dda9266SLuck, Tony {
7906dda9266SLuck, Tony 	if (pstore_new_entry) {
7916dda9266SLuck, Tony 		pstore_new_entry = 0;
7926dda9266SLuck, Tony 		schedule_work(&pstore_work);
7936dda9266SLuck, Tony 	}
7946dda9266SLuck, Tony 
7956330d553SKees Cook 	if (pstore_update_ms >= 0)
7966330d553SKees Cook 		mod_timer(&pstore_timer,
7976330d553SKees Cook 			  jiffies + msecs_to_jiffies(pstore_update_ms));
7986dda9266SLuck, Tony }
7996dda9266SLuck, Tony 
800fe1d4758SKees Cook void __init pstore_choose_compression(void)
801fe1d4758SKees Cook {
802fe1d4758SKees Cook 	const struct pstore_zbackend *step;
803fe1d4758SKees Cook 
804fe1d4758SKees Cook 	if (!compress)
805fe1d4758SKees Cook 		return;
806fe1d4758SKees Cook 
807fe1d4758SKees Cook 	for (step = zbackends; step->name; step++) {
808fe1d4758SKees Cook 		if (!strcmp(compress, step->name)) {
809fe1d4758SKees Cook 			zbackend = step;
810fe1d4758SKees Cook 			return;
811fe1d4758SKees Cook 		}
812fe1d4758SKees Cook 	}
813fe1d4758SKees Cook }
814fe1d4758SKees Cook 
815cb095afdSKees Cook static int __init pstore_init(void)
816cb095afdSKees Cook {
817cb095afdSKees Cook 	int ret;
818cb095afdSKees Cook 
819cb095afdSKees Cook 	pstore_choose_compression();
820cb095afdSKees Cook 
82141603165SJoel Fernandes (Google) 	/*
82241603165SJoel Fernandes (Google) 	 * Check if any pstore backends registered earlier but did not
82341603165SJoel Fernandes (Google) 	 * initialize compression because crypto was not ready. If so,
82441603165SJoel Fernandes (Google) 	 * initialize compression now.
82541603165SJoel Fernandes (Google) 	 */
82641603165SJoel Fernandes (Google) 	allocate_buf_for_compression();
82741603165SJoel Fernandes (Google) 
828cb095afdSKees Cook 	ret = pstore_init_fs();
829cb095afdSKees Cook 	if (ret)
830cb095afdSKees Cook 		return ret;
831cb095afdSKees Cook 
832cb095afdSKees Cook 	return 0;
833cb095afdSKees Cook }
83441603165SJoel Fernandes (Google) late_initcall(pstore_init);
835cb095afdSKees Cook 
836cb095afdSKees Cook static void __exit pstore_exit(void)
837cb095afdSKees Cook {
838cb095afdSKees Cook 	pstore_exit_fs();
839cb095afdSKees Cook }
840cb095afdSKees Cook module_exit(pstore_exit)
841cb095afdSKees Cook 
842fe1d4758SKees Cook module_param(compress, charp, 0444);
843fe1d4758SKees Cook MODULE_PARM_DESC(compress, "Pstore compression to use");
844fe1d4758SKees Cook 
845dee28e72SMatthew Garrett module_param(backend, charp, 0444);
846dee28e72SMatthew Garrett MODULE_PARM_DESC(backend, "Pstore backend to use");
847cb095afdSKees Cook 
848cb095afdSKees Cook MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
849cb095afdSKees Cook MODULE_LICENSE("GPL");
850