xref: /openbmc/linux/fs/pstore/platform.c (revision a9fb94a99bb515d8720ba8440ce3aba84aec80f8)
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 
62f0f23e54SJoel Fernandes (Google) /* Names should be in the same order as the enum pstore_type_id */
63f0f23e54SJoel Fernandes (Google) static const char * const pstore_type_names[] = {
64f0f23e54SJoel Fernandes (Google) 	"dmesg",
65f0f23e54SJoel Fernandes (Google) 	"mce",
66f0f23e54SJoel Fernandes (Google) 	"console",
67f0f23e54SJoel Fernandes (Google) 	"ftrace",
68f0f23e54SJoel Fernandes (Google) 	"rtas",
69f0f23e54SJoel Fernandes (Google) 	"powerpc-ofw",
70f0f23e54SJoel Fernandes (Google) 	"powerpc-common",
71f0f23e54SJoel Fernandes (Google) 	"pmsg",
72f0f23e54SJoel Fernandes (Google) 	"powerpc-opal",
73f0f23e54SJoel Fernandes (Google) };
74f0f23e54SJoel 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 
120f0f23e54SJoel Fernandes (Google) const char *pstore_type_to_name(enum pstore_type_id type)
121f0f23e54SJoel Fernandes (Google) {
122f0f23e54SJoel Fernandes (Google) 	BUILD_BUG_ON(ARRAY_SIZE(pstore_type_names) != PSTORE_TYPE_MAX);
123f0f23e54SJoel Fernandes (Google) 
124f0f23e54SJoel Fernandes (Google) 	if (WARN_ON_ONCE(type >= PSTORE_TYPE_MAX))
125f0f23e54SJoel Fernandes (Google) 		return "unknown";
126f0f23e54SJoel Fernandes (Google) 
127f0f23e54SJoel Fernandes (Google) 	return pstore_type_names[type];
128f0f23e54SJoel Fernandes (Google) }
129f0f23e54SJoel Fernandes (Google) EXPORT_SYMBOL_GPL(pstore_type_to_name);
130f0f23e54SJoel Fernandes (Google) 
131f0f23e54SJoel Fernandes (Google) enum pstore_type_id pstore_name_to_type(const char *name)
132f0f23e54SJoel Fernandes (Google) {
133f0f23e54SJoel Fernandes (Google) 	int i;
134f0f23e54SJoel Fernandes (Google) 
135f0f23e54SJoel Fernandes (Google) 	for (i = 0; i < PSTORE_TYPE_MAX; i++) {
136f0f23e54SJoel Fernandes (Google) 		if (!strcmp(pstore_type_names[i], name))
137f0f23e54SJoel Fernandes (Google) 			return i;
138f0f23e54SJoel Fernandes (Google) 	}
139f0f23e54SJoel Fernandes (Google) 
140f0f23e54SJoel Fernandes (Google) 	return PSTORE_TYPE_MAX;
141f0f23e54SJoel Fernandes (Google) }
142f0f23e54SJoel Fernandes (Google) EXPORT_SYMBOL_GPL(pstore_name_to_type);
143f0f23e54SJoel 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 /*
165ea84b580SKees Cook  * Should pstore_dump() wait for a concurrent pstore_dump()? If
166ea84b580SKees Cook  * not, the current pstore_dump() will report a failure to dump
167ea84b580SKees Cook  * and return.
1689f244e9cSSeiji Aguchi  */
169ea84b580SKees Cook static bool pstore_cannot_wait(enum kmsg_dump_reason reason)
170ea84b580SKees Cook {
171ea84b580SKees Cook 	/* In NMI path, pstore shouldn't block regardless of reason. */
1729f244e9cSSeiji Aguchi 	if (in_nmi())
1739f244e9cSSeiji Aguchi 		return true;
1749f244e9cSSeiji Aguchi 
1759f244e9cSSeiji Aguchi 	switch (reason) {
1769f244e9cSSeiji Aguchi 	/* In panic case, other cpus are stopped by smp_send_stop(). */
1779f244e9cSSeiji Aguchi 	case KMSG_DUMP_PANIC:
178ea84b580SKees Cook 	/* Emergency restart shouldn't be blocked. */
1799f244e9cSSeiji Aguchi 	case KMSG_DUMP_EMERG:
1809f244e9cSSeiji Aguchi 		return true;
1819f244e9cSSeiji Aguchi 	default:
1829f244e9cSSeiji Aguchi 		return false;
1839f244e9cSSeiji Aguchi 	}
1849f244e9cSSeiji Aguchi }
1859f244e9cSSeiji Aguchi 
18658eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
187cb3bee03SGeliang Tang static int zbufsize_deflate(size_t size)
188b0aad7a9SAruna Balakrishnaiah {
1897de8fe2fSAruna Balakrishnaiah 	size_t cmpr;
190b0aad7a9SAruna Balakrishnaiah 
191cb3bee03SGeliang Tang 	switch (size) {
1927de8fe2fSAruna Balakrishnaiah 	/* buffer range for efivars */
1937de8fe2fSAruna Balakrishnaiah 	case 1000 ... 2000:
1947de8fe2fSAruna Balakrishnaiah 		cmpr = 56;
1957de8fe2fSAruna Balakrishnaiah 		break;
1967de8fe2fSAruna Balakrishnaiah 	case 2001 ... 3000:
1977de8fe2fSAruna Balakrishnaiah 		cmpr = 54;
1987de8fe2fSAruna Balakrishnaiah 		break;
1997de8fe2fSAruna Balakrishnaiah 	case 3001 ... 3999:
2007de8fe2fSAruna Balakrishnaiah 		cmpr = 52;
2017de8fe2fSAruna Balakrishnaiah 		break;
2027de8fe2fSAruna Balakrishnaiah 	/* buffer range for nvram, erst */
2037de8fe2fSAruna Balakrishnaiah 	case 4000 ... 10000:
2047de8fe2fSAruna Balakrishnaiah 		cmpr = 45;
2057de8fe2fSAruna Balakrishnaiah 		break;
2067de8fe2fSAruna Balakrishnaiah 	default:
2077de8fe2fSAruna Balakrishnaiah 		cmpr = 60;
2087de8fe2fSAruna Balakrishnaiah 		break;
2097de8fe2fSAruna Balakrishnaiah 	}
2107de8fe2fSAruna Balakrishnaiah 
211cb3bee03SGeliang Tang 	return (size * 100) / cmpr;
2128cfc8ddcSGeliang Tang }
2138cfc8ddcSGeliang Tang #endif
2148cfc8ddcSGeliang Tang 
21558eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
216cb3bee03SGeliang Tang static int zbufsize_lzo(size_t size)
2178cfc8ddcSGeliang Tang {
218cb3bee03SGeliang Tang 	return lzo1x_worst_compress(size);
2198cfc8ddcSGeliang Tang }
2208cfc8ddcSGeliang Tang #endif
2218cfc8ddcSGeliang Tang 
22258eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
223cb3bee03SGeliang Tang static int zbufsize_lz4(size_t size)
2248cfc8ddcSGeliang Tang {
225cb3bee03SGeliang Tang 	return LZ4_compressBound(size);
226239b7161SGeliang Tang }
227239b7161SGeliang Tang #endif
228239b7161SGeliang Tang 
22958eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
230cb3bee03SGeliang Tang static int zbufsize_842(size_t size)
231239b7161SGeliang Tang {
23255597406SKees Cook 	return size;
233239b7161SGeliang Tang }
234fe1d4758SKees Cook #endif
2358cfc8ddcSGeliang Tang 
2361021bcf4SGeliang Tang #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
2371021bcf4SGeliang Tang static int zbufsize_zstd(size_t size)
2381021bcf4SGeliang Tang {
2391021bcf4SGeliang Tang 	return ZSTD_compressBound(size);
2401021bcf4SGeliang Tang }
2411021bcf4SGeliang Tang #endif
2421021bcf4SGeliang Tang 
243fe1d4758SKees Cook static const struct pstore_zbackend *zbackend __ro_after_init;
244fe1d4758SKees Cook 
245fe1d4758SKees Cook static const struct pstore_zbackend zbackends[] = {
24658eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
247fe1d4758SKees Cook 	{
248cb3bee03SGeliang Tang 		.zbufsize	= zbufsize_deflate,
249cb3bee03SGeliang Tang 		.name		= "deflate",
250fe1d4758SKees Cook 	},
251fe1d4758SKees Cook #endif
25258eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
253fe1d4758SKees Cook 	{
254cb3bee03SGeliang Tang 		.zbufsize	= zbufsize_lzo,
255fe1d4758SKees Cook 		.name		= "lzo",
256fe1d4758SKees Cook 	},
257fe1d4758SKees Cook #endif
25858eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS)
259fe1d4758SKees Cook 	{
260cb3bee03SGeliang Tang 		.zbufsize	= zbufsize_lz4,
261fe1d4758SKees Cook 		.name		= "lz4",
262fe1d4758SKees Cook 	},
263fe1d4758SKees Cook #endif
26458eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
265fe1d4758SKees Cook 	{
266cb3bee03SGeliang Tang 		.zbufsize	= zbufsize_lz4,
267fe1d4758SKees Cook 		.name		= "lz4hc",
268fe1d4758SKees Cook 	},
269fe1d4758SKees Cook #endif
27058eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
271fe1d4758SKees Cook 	{
272cb3bee03SGeliang Tang 		.zbufsize	= zbufsize_842,
273239b7161SGeliang Tang 		.name		= "842",
274fe1d4758SKees Cook 	},
275fe1d4758SKees Cook #endif
2761021bcf4SGeliang Tang #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
2771021bcf4SGeliang Tang 	{
2781021bcf4SGeliang Tang 		.zbufsize	= zbufsize_zstd,
2791021bcf4SGeliang Tang 		.name		= "zstd",
2801021bcf4SGeliang Tang 	},
2811021bcf4SGeliang Tang #endif
282fe1d4758SKees Cook 	{ }
2838cfc8ddcSGeliang Tang };
2848cfc8ddcSGeliang Tang 
2858cfc8ddcSGeliang Tang static int pstore_compress(const void *in, void *out,
286cb3bee03SGeliang Tang 			   unsigned int inlen, unsigned int outlen)
2878cfc8ddcSGeliang Tang {
288cb3bee03SGeliang Tang 	int ret;
289cb3bee03SGeliang Tang 
290cb3bee03SGeliang Tang 	ret = crypto_comp_compress(tfm, in, inlen, out, &outlen);
291cb3bee03SGeliang Tang 	if (ret) {
292cb3bee03SGeliang Tang 		pr_err("crypto_comp_compress failed, ret = %d!\n", ret);
293cb3bee03SGeliang Tang 		return ret;
2948cfc8ddcSGeliang Tang 	}
2958cfc8ddcSGeliang Tang 
296cb3bee03SGeliang Tang 	return outlen;
297cb3bee03SGeliang Tang }
298cb3bee03SGeliang Tang 
2998cfc8ddcSGeliang Tang static void allocate_buf_for_compression(void)
3008cfc8ddcSGeliang Tang {
30195047b05SKees Cook 	struct crypto_comp *ctx;
30295047b05SKees Cook 	int size;
30395047b05SKees Cook 	char *buf;
30495047b05SKees Cook 
30595047b05SKees Cook 	/* Skip if not built-in or compression backend not selected yet. */
306e698aaf3STobias Regnery 	if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !zbackend)
307cb3bee03SGeliang Tang 		return;
308cb3bee03SGeliang Tang 
30995047b05SKees Cook 	/* Skip if no pstore backend yet or compression init already done. */
31095047b05SKees Cook 	if (!psinfo || tfm)
31195047b05SKees Cook 		return;
31295047b05SKees Cook 
313cb3bee03SGeliang Tang 	if (!crypto_has_comp(zbackend->name, 0, 0)) {
31495047b05SKees Cook 		pr_err("Unknown compression: %s\n", zbackend->name);
315cb3bee03SGeliang Tang 		return;
316cb3bee03SGeliang Tang 	}
317cb3bee03SGeliang Tang 
31895047b05SKees Cook 	size = zbackend->zbufsize(psinfo->bufsize);
31995047b05SKees Cook 	if (size <= 0) {
32095047b05SKees Cook 		pr_err("Invalid compression size for %s: %d\n",
32195047b05SKees Cook 		       zbackend->name, size);
322cb3bee03SGeliang Tang 		return;
323cb3bee03SGeliang Tang 	}
324cb3bee03SGeliang Tang 
32595047b05SKees Cook 	buf = kmalloc(size, GFP_KERNEL);
32695047b05SKees Cook 	if (!buf) {
32795047b05SKees Cook 		pr_err("Failed %d byte compression buffer allocation for: %s\n",
32895047b05SKees Cook 		       size, zbackend->name);
329cb3bee03SGeliang Tang 		return;
3308cfc8ddcSGeliang Tang 	}
33195047b05SKees Cook 
33295047b05SKees Cook 	ctx = crypto_alloc_comp(zbackend->name, 0, 0);
33395047b05SKees Cook 	if (IS_ERR_OR_NULL(ctx)) {
33495047b05SKees Cook 		kfree(buf);
33595047b05SKees Cook 		pr_err("crypto_alloc_comp('%s') failed: %ld\n", zbackend->name,
33695047b05SKees Cook 		       PTR_ERR(ctx));
33795047b05SKees Cook 		return;
33895047b05SKees Cook 	}
33995047b05SKees Cook 
34095047b05SKees Cook 	/* A non-NULL big_oops_buf indicates compression is available. */
34195047b05SKees Cook 	tfm = ctx;
34295047b05SKees Cook 	big_oops_buf_sz = size;
34395047b05SKees Cook 	big_oops_buf = buf;
34495047b05SKees Cook 
3450eed84ffSKees Cook 	pr_info("Using crash dump compression: %s\n", zbackend->name);
3468cfc8ddcSGeliang Tang }
3478cfc8ddcSGeliang Tang 
3488cfc8ddcSGeliang Tang static void free_buf_for_compression(void)
3498cfc8ddcSGeliang Tang {
350*a9fb94a9SPi-Hsun Shih 	if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && tfm) {
351cb3bee03SGeliang Tang 		crypto_free_comp(tfm);
352*a9fb94a9SPi-Hsun Shih 		tfm = NULL;
353*a9fb94a9SPi-Hsun Shih 	}
354cb3bee03SGeliang Tang 	kfree(big_oops_buf);
355cb3bee03SGeliang Tang 	big_oops_buf = NULL;
356cb3bee03SGeliang Tang 	big_oops_buf_sz = 0;
357ee1d2674SGeliang Tang }
358ee1d2674SGeliang Tang 
359b0aad7a9SAruna Balakrishnaiah /*
360b0aad7a9SAruna Balakrishnaiah  * Called when compression fails, since the printk buffer
361b0aad7a9SAruna Balakrishnaiah  * would be fetched for compression calling it again when
362b0aad7a9SAruna Balakrishnaiah  * compression fails would have moved the iterator of
363b0aad7a9SAruna Balakrishnaiah  * printk buffer which results in fetching old contents.
364b0aad7a9SAruna Balakrishnaiah  * Copy the recent messages from big_oops_buf to psinfo->buf
365b0aad7a9SAruna Balakrishnaiah  */
366b0aad7a9SAruna Balakrishnaiah static size_t copy_kmsg_to_buffer(int hsize, size_t len)
367b0aad7a9SAruna Balakrishnaiah {
368b0aad7a9SAruna Balakrishnaiah 	size_t total_len;
369b0aad7a9SAruna Balakrishnaiah 	size_t diff;
370b0aad7a9SAruna Balakrishnaiah 
371b0aad7a9SAruna Balakrishnaiah 	total_len = hsize + len;
372b0aad7a9SAruna Balakrishnaiah 
373b0aad7a9SAruna Balakrishnaiah 	if (total_len > psinfo->bufsize) {
374b0aad7a9SAruna Balakrishnaiah 		diff = total_len - psinfo->bufsize + hsize;
375b0aad7a9SAruna Balakrishnaiah 		memcpy(psinfo->buf, big_oops_buf, hsize);
376b0aad7a9SAruna Balakrishnaiah 		memcpy(psinfo->buf + hsize, big_oops_buf + diff,
377b0aad7a9SAruna Balakrishnaiah 					psinfo->bufsize - hsize);
378b0aad7a9SAruna Balakrishnaiah 		total_len = psinfo->bufsize;
379b0aad7a9SAruna Balakrishnaiah 	} else
380b0aad7a9SAruna Balakrishnaiah 		memcpy(psinfo->buf, big_oops_buf, total_len);
381b0aad7a9SAruna Balakrishnaiah 
382b0aad7a9SAruna Balakrishnaiah 	return total_len;
383b0aad7a9SAruna Balakrishnaiah }
384b0aad7a9SAruna Balakrishnaiah 
385e581ca81SKees Cook void pstore_record_init(struct pstore_record *record,
386e581ca81SKees Cook 			struct pstore_info *psinfo)
387e581ca81SKees Cook {
388e581ca81SKees Cook 	memset(record, 0, sizeof(*record));
389e581ca81SKees Cook 
390e581ca81SKees Cook 	record->psi = psinfo;
391c7f3c595SKees Cook 
392c7f3c595SKees Cook 	/* Report zeroed timestamp if called before timekeeping has resumed. */
3937aaa822eSKees Cook 	record->time = ns_to_timespec64(ktime_get_real_fast_ns());
394e581ca81SKees Cook }
395e581ca81SKees Cook 
396ca01d6ddSTony Luck /*
3970eed84ffSKees Cook  * callback from kmsg_dump. Save as much as we can (up to kmsg_bytes) from the
3980eed84ffSKees Cook  * end of the buffer.
399ca01d6ddSTony Luck  */
400ca01d6ddSTony Luck static void pstore_dump(struct kmsg_dumper *dumper,
401e2ae715dSKay Sievers 			enum kmsg_dump_reason reason)
402ca01d6ddSTony Luck {
403e2ae715dSKay Sievers 	unsigned long	total = 0;
404381b872cSSeiji Aguchi 	const char	*why;
405b94fdd07SMatthew Garrett 	unsigned int	part = 1;
406e2ae715dSKay Sievers 	int		ret;
407ca01d6ddSTony Luck 
408381b872cSSeiji Aguchi 	why = get_reason_str(reason);
4099f6af27fSTony Luck 
410ea84b580SKees Cook 	if (down_trylock(&psinfo->buf_lock)) {
411ea84b580SKees Cook 		/* Failed to acquire lock: give up if we cannot wait. */
412ea84b580SKees Cook 		if (pstore_cannot_wait(reason)) {
413ea84b580SKees Cook 			pr_err("dump skipped in %s path: may corrupt error record\n",
414ea84b580SKees Cook 				in_nmi() ? "NMI" : why);
415959217c8SLi Pengcheng 			return;
4169f244e9cSSeiji Aguchi 		}
417ea84b580SKees Cook 		if (down_interruptible(&psinfo->buf_lock)) {
418ea84b580SKees Cook 			pr_err("could not grab semaphore?!\n");
419ea84b580SKees Cook 			return;
42098e44fdaSNamhyung Kim 		}
421ea84b580SKees Cook 	}
422ea84b580SKees Cook 
423ca01d6ddSTony Luck 	oopscount++;
424ca01d6ddSTony Luck 	while (total < kmsg_bytes) {
425e2ae715dSKay Sievers 		char *dst;
42676cc9580SKees Cook 		size_t dst_size;
42776cc9580SKees Cook 		int header_size;
428b0aad7a9SAruna Balakrishnaiah 		int zipped_len = -1;
42976cc9580SKees Cook 		size_t dump_size;
430e581ca81SKees Cook 		struct pstore_record record;
431e581ca81SKees Cook 
432e581ca81SKees Cook 		pstore_record_init(&record, psinfo);
433e581ca81SKees Cook 		record.type = PSTORE_TYPE_DMESG;
434e581ca81SKees Cook 		record.count = oopscount;
435e581ca81SKees Cook 		record.reason = reason;
436e581ca81SKees Cook 		record.part = part;
437e581ca81SKees Cook 		record.buf = psinfo->buf;
438e2ae715dSKay Sievers 
439ea84b580SKees Cook 		if (big_oops_buf) {
440b0aad7a9SAruna Balakrishnaiah 			dst = big_oops_buf;
44176cc9580SKees Cook 			dst_size = big_oops_buf_sz;
442235f6d15SNamhyung Kim 		} else {
443235f6d15SNamhyung Kim 			dst = psinfo->buf;
44476cc9580SKees Cook 			dst_size = psinfo->bufsize;
445235f6d15SNamhyung Kim 		}
446235f6d15SNamhyung Kim 
44776cc9580SKees Cook 		/* Write dump header. */
44876cc9580SKees Cook 		header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why,
44976cc9580SKees Cook 				 oopscount, part);
45076cc9580SKees Cook 		dst_size -= header_size;
451b0aad7a9SAruna Balakrishnaiah 
45276cc9580SKees Cook 		/* Write dump contents. */
45376cc9580SKees Cook 		if (!kmsg_dump_get_buffer(dumper, true, dst + header_size,
45476cc9580SKees Cook 					  dst_size, &dump_size))
455b0aad7a9SAruna Balakrishnaiah 			break;
456b0aad7a9SAruna Balakrishnaiah 
457ea84b580SKees Cook 		if (big_oops_buf) {
458b0aad7a9SAruna Balakrishnaiah 			zipped_len = pstore_compress(dst, psinfo->buf,
45976cc9580SKees Cook 						header_size + dump_size,
46076cc9580SKees Cook 						psinfo->bufsize);
461b0aad7a9SAruna Balakrishnaiah 
462b0aad7a9SAruna Balakrishnaiah 			if (zipped_len > 0) {
46376cc9580SKees Cook 				record.compressed = true;
46476cc9580SKees Cook 				record.size = zipped_len;
465b0aad7a9SAruna Balakrishnaiah 			} else {
46676cc9580SKees Cook 				record.size = copy_kmsg_to_buffer(header_size,
46776cc9580SKees Cook 								  dump_size);
468b0aad7a9SAruna Balakrishnaiah 			}
469b0aad7a9SAruna Balakrishnaiah 		} else {
47076cc9580SKees Cook 			record.size = header_size + dump_size;
471b0aad7a9SAruna Balakrishnaiah 		}
472b0aad7a9SAruna Balakrishnaiah 
47376cc9580SKees Cook 		ret = psinfo->write(&record);
474b238b8faSChen Gong 		if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted())
4756dda9266SLuck, Tony 			pstore_new_entry = 1;
476e2ae715dSKay Sievers 
47776cc9580SKees Cook 		total += record.size;
47856280682SMatthew Garrett 		part++;
479ca01d6ddSTony Luck 	}
480ea84b580SKees Cook 
481ea84b580SKees Cook 	up(&psinfo->buf_lock);
482ca01d6ddSTony Luck }
483ca01d6ddSTony Luck 
484ca01d6ddSTony Luck static struct kmsg_dumper pstore_dumper = {
485ca01d6ddSTony Luck 	.dump = pstore_dump,
486ca01d6ddSTony Luck };
487ca01d6ddSTony Luck 
488306e5c2aSGeliang Tang /*
489306e5c2aSGeliang Tang  * Register with kmsg_dump to save last part of console log on panic.
490306e5c2aSGeliang Tang  */
49118730411SGeliang Tang static void pstore_register_kmsg(void)
49218730411SGeliang Tang {
49318730411SGeliang Tang 	kmsg_dump_register(&pstore_dumper);
49418730411SGeliang Tang }
49518730411SGeliang Tang 
496ee1d2674SGeliang Tang static void pstore_unregister_kmsg(void)
497ee1d2674SGeliang Tang {
498ee1d2674SGeliang Tang 	kmsg_dump_unregister(&pstore_dumper);
499ee1d2674SGeliang Tang }
500ee1d2674SGeliang Tang 
501f29e5956SAnton Vorontsov #ifdef CONFIG_PSTORE_CONSOLE
502f29e5956SAnton Vorontsov static void pstore_console_write(struct console *con, const char *s, unsigned c)
503f29e5956SAnton Vorontsov {
504e581ca81SKees Cook 	struct pstore_record record;
505f29e5956SAnton Vorontsov 
5064c6c4d34SYue Hu 	if (!c)
5074c6c4d34SYue Hu 		return;
5084c6c4d34SYue Hu 
509e581ca81SKees Cook 	pstore_record_init(&record, psinfo);
510e581ca81SKees Cook 	record.type = PSTORE_TYPE_CONSOLE;
511e581ca81SKees Cook 
512b10b4711SKees Cook 	record.buf = (char *)s;
513b10b4711SKees Cook 	record.size = c;
5144c9ec219SKees Cook 	psinfo->write(&record);
515f29e5956SAnton Vorontsov }
516f29e5956SAnton Vorontsov 
517f29e5956SAnton Vorontsov static struct console pstore_console = {
518f29e5956SAnton Vorontsov 	.name	= "pstore",
519f29e5956SAnton Vorontsov 	.write	= pstore_console_write,
520f29e5956SAnton Vorontsov 	.flags	= CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME,
521f29e5956SAnton Vorontsov 	.index	= -1,
522f29e5956SAnton Vorontsov };
523f29e5956SAnton Vorontsov 
524f29e5956SAnton Vorontsov static void pstore_register_console(void)
525f29e5956SAnton Vorontsov {
526f29e5956SAnton Vorontsov 	register_console(&pstore_console);
527f29e5956SAnton Vorontsov }
528ee1d2674SGeliang Tang 
529ee1d2674SGeliang Tang static void pstore_unregister_console(void)
530ee1d2674SGeliang Tang {
531ee1d2674SGeliang Tang 	unregister_console(&pstore_console);
532ee1d2674SGeliang Tang }
533f29e5956SAnton Vorontsov #else
534f29e5956SAnton Vorontsov static void pstore_register_console(void) {}
535ee1d2674SGeliang Tang static void pstore_unregister_console(void) {}
536f29e5956SAnton Vorontsov #endif
537f29e5956SAnton Vorontsov 
5384c9ec219SKees Cook static int pstore_write_user_compat(struct pstore_record *record,
539fdd03118SKees Cook 				    const char __user *buf)
5405bf6d1b9SMark Salyzyn {
54130800d99SKees Cook 	int ret = 0;
5425bf6d1b9SMark Salyzyn 
54330800d99SKees Cook 	if (record->buf)
54430800d99SKees Cook 		return -EINVAL;
5455bf6d1b9SMark Salyzyn 
546077090afSGeliang Tang 	record->buf = memdup_user(buf, record->size);
547dfd6fa39SHirofumi Nakagawa 	if (IS_ERR(record->buf)) {
548077090afSGeliang Tang 		ret = PTR_ERR(record->buf);
54930800d99SKees Cook 		goto out;
5505bf6d1b9SMark Salyzyn 	}
55130800d99SKees Cook 
5524c9ec219SKees Cook 	ret = record->psi->write(record);
55330800d99SKees Cook 
55430800d99SKees Cook 	kfree(record->buf);
555077090afSGeliang Tang out:
55630800d99SKees Cook 	record->buf = NULL;
55730800d99SKees Cook 
55830800d99SKees Cook 	return unlikely(ret < 0) ? ret : record->size;
5595bf6d1b9SMark Salyzyn }
5605bf6d1b9SMark Salyzyn 
561ca01d6ddSTony Luck /*
562ca01d6ddSTony Luck  * platform specific persistent storage driver registers with
563ca01d6ddSTony Luck  * us here. If pstore is already mounted, call the platform
564ca01d6ddSTony Luck  * read function right away to populate the file system. If not
565ca01d6ddSTony Luck  * then the pstore mount code will call us later to fill out
566ca01d6ddSTony Luck  * the file system.
567ca01d6ddSTony Luck  */
568ca01d6ddSTony Luck int pstore_register(struct pstore_info *psi)
569ca01d6ddSTony Luck {
570ca01d6ddSTony Luck 	struct module *owner = psi->owner;
571ca01d6ddSTony Luck 
5720d7cd09aSKees Cook 	if (backend && strcmp(backend, psi->name)) {
5730d7cd09aSKees Cook 		pr_warn("ignoring unexpected backend '%s'\n", psi->name);
5748e48b1a8SLenny Szubowicz 		return -EPERM;
5750d7cd09aSKees Cook 	}
5768e48b1a8SLenny Szubowicz 
5774c9ec219SKees Cook 	/* Sanity check flags. */
5784c9ec219SKees Cook 	if (!psi->flags) {
5794c9ec219SKees Cook 		pr_warn("backend '%s' must support at least one frontend\n",
5804c9ec219SKees Cook 			psi->name);
5814c9ec219SKees Cook 		return -EINVAL;
5824c9ec219SKees Cook 	}
5834c9ec219SKees Cook 
5844c9ec219SKees Cook 	/* Check for required functions. */
5854c9ec219SKees Cook 	if (!psi->read || !psi->write) {
5864c9ec219SKees Cook 		pr_warn("backend '%s' must implement read() and write()\n",
5874c9ec219SKees Cook 			psi->name);
5884c9ec219SKees Cook 		return -EINVAL;
5894c9ec219SKees Cook 	}
5904c9ec219SKees Cook 
591ca01d6ddSTony Luck 	spin_lock(&pstore_lock);
592ca01d6ddSTony Luck 	if (psinfo) {
5930d7cd09aSKees Cook 		pr_warn("backend '%s' already loaded: ignoring '%s'\n",
5940d7cd09aSKees Cook 			psinfo->name, psi->name);
595ca01d6ddSTony Luck 		spin_unlock(&pstore_lock);
596ca01d6ddSTony Luck 		return -EBUSY;
597ca01d6ddSTony Luck 	}
598dee28e72SMatthew Garrett 
5994c9ec219SKees Cook 	if (!psi->write_user)
6004c9ec219SKees Cook 		psi->write_user = pstore_write_user_compat;
601ca01d6ddSTony Luck 	psinfo = psi;
602f6f82851SKees Cook 	mutex_init(&psinfo->read_mutex);
603ea84b580SKees Cook 	sema_init(&psinfo->buf_lock, 1);
604ca01d6ddSTony Luck 	spin_unlock(&pstore_lock);
605ca01d6ddSTony Luck 
606ca01d6ddSTony Luck 	if (owner && !try_module_get(owner)) {
607ca01d6ddSTony Luck 		psinfo = NULL;
608ca01d6ddSTony Luck 		return -EINVAL;
609ca01d6ddSTony Luck 	}
610ca01d6ddSTony Luck 
611b0aad7a9SAruna Balakrishnaiah 	allocate_buf_for_compression();
612b0aad7a9SAruna Balakrishnaiah 
613ca01d6ddSTony Luck 	if (pstore_is_mounted())
6146dda9266SLuck, Tony 		pstore_get_records(0);
615ca01d6ddSTony Luck 
616c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_DMESG)
61718730411SGeliang Tang 		pstore_register_kmsg();
618c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_CONSOLE)
619f29e5956SAnton Vorontsov 		pstore_register_console();
620c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_FTRACE)
62165f8c95eSAnton Vorontsov 		pstore_register_ftrace();
622c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_PMSG)
6239d5438f4SMark Salyzyn 		pstore_register_pmsg();
624ca01d6ddSTony Luck 
6256330d553SKees Cook 	/* Start watching for new records, if desired. */
626a3f5f075SAnton Vorontsov 	if (pstore_update_ms >= 0) {
627a3f5f075SAnton Vorontsov 		pstore_timer.expires = jiffies +
628a3f5f075SAnton Vorontsov 			msecs_to_jiffies(pstore_update_ms);
6296dda9266SLuck, Tony 		add_timer(&pstore_timer);
630a3f5f075SAnton Vorontsov 	}
6316dda9266SLuck, Tony 
63242222c2aSWang Long 	/*
63342222c2aSWang Long 	 * Update the module parameter backend, so it is visible
63442222c2aSWang Long 	 * through /sys/module/pstore/parameters/backend
63542222c2aSWang Long 	 */
63642222c2aSWang Long 	backend = psi->name;
63742222c2aSWang Long 
638ef748853SFabian Frederick 	pr_info("Registered %s as persistent store backend\n", psi->name);
6398e48b1a8SLenny Szubowicz 
6401344dd86SKees Cook 	module_put(owner);
6411344dd86SKees Cook 
642ca01d6ddSTony Luck 	return 0;
643ca01d6ddSTony Luck }
644ca01d6ddSTony Luck EXPORT_SYMBOL_GPL(pstore_register);
645ca01d6ddSTony Luck 
646ee1d2674SGeliang Tang void pstore_unregister(struct pstore_info *psi)
647ee1d2674SGeliang Tang {
6486330d553SKees Cook 	/* Stop timer and make sure all work has finished. */
6496330d553SKees Cook 	pstore_update_ms = -1;
6506330d553SKees Cook 	del_timer_sync(&pstore_timer);
6516330d553SKees Cook 	flush_work(&pstore_work);
6526330d553SKees Cook 
653c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_PMSG)
654ee1d2674SGeliang Tang 		pstore_unregister_pmsg();
655c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_FTRACE)
656ee1d2674SGeliang Tang 		pstore_unregister_ftrace();
657c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_CONSOLE)
658ee1d2674SGeliang Tang 		pstore_unregister_console();
659c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_DMESG)
660ee1d2674SGeliang Tang 		pstore_unregister_kmsg();
661ee1d2674SGeliang Tang 
662ee1d2674SGeliang Tang 	free_buf_for_compression();
663ee1d2674SGeliang Tang 
664ee1d2674SGeliang Tang 	psinfo = NULL;
665ee1d2674SGeliang Tang 	backend = NULL;
666ee1d2674SGeliang Tang }
667ee1d2674SGeliang Tang EXPORT_SYMBOL_GPL(pstore_unregister);
668ee1d2674SGeliang Tang 
669634f8f51SKees Cook static void decompress_record(struct pstore_record *record)
670634f8f51SKees Cook {
671bdabc8e7SKees Cook 	int ret;
672634f8f51SKees Cook 	int unzipped_len;
673bdabc8e7SKees Cook 	char *unzipped, *workspace;
674634f8f51SKees Cook 
6754a16d1cbSAnkit Kumar 	if (!record->compressed)
6764a16d1cbSAnkit Kumar 		return;
6774a16d1cbSAnkit Kumar 
678634f8f51SKees Cook 	/* Only PSTORE_TYPE_DMESG support compression. */
6794a16d1cbSAnkit Kumar 	if (record->type != PSTORE_TYPE_DMESG) {
680634f8f51SKees Cook 		pr_warn("ignored compressed record type %d\n", record->type);
681634f8f51SKees Cook 		return;
682634f8f51SKees Cook 	}
683634f8f51SKees Cook 
684bdabc8e7SKees Cook 	/* Missing compression buffer means compression was not initialized. */
685634f8f51SKees Cook 	if (!big_oops_buf) {
686bdabc8e7SKees Cook 		pr_warn("no decompression method initialized!\n");
687634f8f51SKees Cook 		return;
688634f8f51SKees Cook 	}
689634f8f51SKees Cook 
690bdabc8e7SKees Cook 	/* Allocate enough space to hold max decompression and ECC. */
691bdabc8e7SKees Cook 	unzipped_len = big_oops_buf_sz;
692bdabc8e7SKees Cook 	workspace = kmalloc(unzipped_len + record->ecc_notice_size,
6937e8cc8dcSKees Cook 			    GFP_KERNEL);
694bdabc8e7SKees Cook 	if (!workspace)
695bdabc8e7SKees Cook 		return;
696bdabc8e7SKees Cook 
697bdabc8e7SKees Cook 	/* After decompression "unzipped_len" is almost certainly smaller. */
698bdabc8e7SKees Cook 	ret = crypto_comp_decompress(tfm, record->buf, record->size,
699bdabc8e7SKees Cook 					  workspace, &unzipped_len);
700bdabc8e7SKees Cook 	if (ret) {
701bdabc8e7SKees Cook 		pr_err("crypto_comp_decompress failed, ret = %d!\n", ret);
702bdabc8e7SKees Cook 		kfree(workspace);
7037e8cc8dcSKees Cook 		return;
7047e8cc8dcSKees Cook 	}
7057e8cc8dcSKees Cook 
7067e8cc8dcSKees Cook 	/* Append ECC notice to decompressed buffer. */
707bdabc8e7SKees Cook 	memcpy(workspace + unzipped_len, record->buf + record->size,
708634f8f51SKees Cook 	       record->ecc_notice_size);
7097e8cc8dcSKees Cook 
710bdabc8e7SKees Cook 	/* Copy decompressed contents into an minimum-sized allocation. */
711bdabc8e7SKees Cook 	unzipped = kmemdup(workspace, unzipped_len + record->ecc_notice_size,
712bdabc8e7SKees Cook 			   GFP_KERNEL);
713bdabc8e7SKees Cook 	kfree(workspace);
714bdabc8e7SKees Cook 	if (!unzipped)
715bdabc8e7SKees Cook 		return;
716bdabc8e7SKees Cook 
717bdabc8e7SKees Cook 	/* Swap out compressed contents with decompressed contents. */
718634f8f51SKees Cook 	kfree(record->buf);
719bdabc8e7SKees Cook 	record->buf = unzipped;
720634f8f51SKees Cook 	record->size = unzipped_len;
721634f8f51SKees Cook 	record->compressed = false;
722634f8f51SKees Cook }
723634f8f51SKees Cook 
724ca01d6ddSTony Luck /*
7253a7d2fd1SKees Cook  * Read all the records from one persistent store backend. Create
7266dda9266SLuck, Tony  * files in our filesystem.  Don't warn about -EEXIST errors
7276dda9266SLuck, Tony  * when we are re-scanning the backing store looking to add new
7286dda9266SLuck, Tony  * error records.
729ca01d6ddSTony Luck  */
7303a7d2fd1SKees Cook void pstore_get_backend_records(struct pstore_info *psi,
7313a7d2fd1SKees Cook 				struct dentry *root, int quiet)
732ca01d6ddSTony Luck {
7332a2b0acfSKees Cook 	int failed = 0;
734656de42eSKees Cook 	unsigned int stop_loop = 65536;
735ca01d6ddSTony Luck 
7363a7d2fd1SKees Cook 	if (!psi || !root)
737ca01d6ddSTony Luck 		return;
738ca01d6ddSTony Luck 
739f6f82851SKees Cook 	mutex_lock(&psi->read_mutex);
7402174f6dfSKees Cook 	if (psi->open && psi->open(psi))
74106cf91b4SChen Gong 		goto out;
74206cf91b4SChen Gong 
7431dfff7ddSKees Cook 	/*
7441dfff7ddSKees Cook 	 * Backend callback read() allocates record.buf. decompress_record()
7451dfff7ddSKees Cook 	 * may reallocate record.buf. On success, pstore_mkfile() will keep
7461dfff7ddSKees Cook 	 * the record.buf, so free it only on failure.
7471dfff7ddSKees Cook 	 */
748656de42eSKees Cook 	for (; stop_loop; stop_loop--) {
7492a2b0acfSKees Cook 		struct pstore_record *record;
7502a2b0acfSKees Cook 		int rc;
7512a2b0acfSKees Cook 
7522a2b0acfSKees Cook 		record = kzalloc(sizeof(*record), GFP_KERNEL);
7532a2b0acfSKees Cook 		if (!record) {
7542a2b0acfSKees Cook 			pr_err("out of memory creating record\n");
7552a2b0acfSKees Cook 			break;
7562a2b0acfSKees Cook 		}
757e581ca81SKees Cook 		pstore_record_init(record, psi);
7582a2b0acfSKees Cook 
7592a2b0acfSKees Cook 		record->size = psi->read(record);
7602a2b0acfSKees Cook 
7612a2b0acfSKees Cook 		/* No more records left in backend? */
762f6525b96SDouglas Anderson 		if (record->size <= 0) {
763f6525b96SDouglas Anderson 			kfree(record);
7642a2b0acfSKees Cook 			break;
765f6525b96SDouglas Anderson 		}
7662a2b0acfSKees Cook 
7672a2b0acfSKees Cook 		decompress_record(record);
7683a7d2fd1SKees Cook 		rc = pstore_mkfile(root, record);
7691dfff7ddSKees Cook 		if (rc) {
77083f70f07SKees Cook 			/* pstore_mkfile() did not take record, so free it. */
7712a2b0acfSKees Cook 			kfree(record->buf);
77283f70f07SKees Cook 			kfree(record);
7731dfff7ddSKees Cook 			if (rc != -EEXIST || !quiet)
7741dfff7ddSKees Cook 				failed++;
7751dfff7ddSKees Cook 		}
776ca01d6ddSTony Luck 	}
7772174f6dfSKees Cook 	if (psi->close)
77806cf91b4SChen Gong 		psi->close(psi);
77906cf91b4SChen Gong out:
780f6f82851SKees Cook 	mutex_unlock(&psi->read_mutex);
781ca01d6ddSTony Luck 
782ca01d6ddSTony Luck 	if (failed)
783656de42eSKees Cook 		pr_warn("failed to create %d record(s) from '%s'\n",
784ca01d6ddSTony Luck 			failed, psi->name);
785656de42eSKees Cook 	if (!stop_loop)
786656de42eSKees Cook 		pr_err("looping? Too many records seen from '%s'\n",
787656de42eSKees Cook 			psi->name);
788ca01d6ddSTony Luck }
789ca01d6ddSTony Luck 
7906dda9266SLuck, Tony static void pstore_dowork(struct work_struct *work)
7916dda9266SLuck, Tony {
7926dda9266SLuck, Tony 	pstore_get_records(1);
7936dda9266SLuck, Tony }
7946dda9266SLuck, Tony 
79524ed960aSKees Cook static void pstore_timefunc(struct timer_list *unused)
7966dda9266SLuck, Tony {
7976dda9266SLuck, Tony 	if (pstore_new_entry) {
7986dda9266SLuck, Tony 		pstore_new_entry = 0;
7996dda9266SLuck, Tony 		schedule_work(&pstore_work);
8006dda9266SLuck, Tony 	}
8016dda9266SLuck, Tony 
8026330d553SKees Cook 	if (pstore_update_ms >= 0)
8036330d553SKees Cook 		mod_timer(&pstore_timer,
8046330d553SKees Cook 			  jiffies + msecs_to_jiffies(pstore_update_ms));
8056dda9266SLuck, Tony }
8066dda9266SLuck, Tony 
807fe1d4758SKees Cook void __init pstore_choose_compression(void)
808fe1d4758SKees Cook {
809fe1d4758SKees Cook 	const struct pstore_zbackend *step;
810fe1d4758SKees Cook 
811fe1d4758SKees Cook 	if (!compress)
812fe1d4758SKees Cook 		return;
813fe1d4758SKees Cook 
814fe1d4758SKees Cook 	for (step = zbackends; step->name; step++) {
815fe1d4758SKees Cook 		if (!strcmp(compress, step->name)) {
816fe1d4758SKees Cook 			zbackend = step;
817fe1d4758SKees Cook 			return;
818fe1d4758SKees Cook 		}
819fe1d4758SKees Cook 	}
820fe1d4758SKees Cook }
821fe1d4758SKees Cook 
822cb095afdSKees Cook static int __init pstore_init(void)
823cb095afdSKees Cook {
824cb095afdSKees Cook 	int ret;
825cb095afdSKees Cook 
826cb095afdSKees Cook 	pstore_choose_compression();
827cb095afdSKees Cook 
82841603165SJoel Fernandes (Google) 	/*
82941603165SJoel Fernandes (Google) 	 * Check if any pstore backends registered earlier but did not
83041603165SJoel Fernandes (Google) 	 * initialize compression because crypto was not ready. If so,
83141603165SJoel Fernandes (Google) 	 * initialize compression now.
83241603165SJoel Fernandes (Google) 	 */
83341603165SJoel Fernandes (Google) 	allocate_buf_for_compression();
83441603165SJoel Fernandes (Google) 
835cb095afdSKees Cook 	ret = pstore_init_fs();
836cb095afdSKees Cook 	if (ret)
837cb095afdSKees Cook 		return ret;
838cb095afdSKees Cook 
839cb095afdSKees Cook 	return 0;
840cb095afdSKees Cook }
84141603165SJoel Fernandes (Google) late_initcall(pstore_init);
842cb095afdSKees Cook 
843cb095afdSKees Cook static void __exit pstore_exit(void)
844cb095afdSKees Cook {
845cb095afdSKees Cook 	pstore_exit_fs();
846cb095afdSKees Cook }
847cb095afdSKees Cook module_exit(pstore_exit)
848cb095afdSKees Cook 
849fe1d4758SKees Cook module_param(compress, charp, 0444);
850fe1d4758SKees Cook MODULE_PARM_DESC(compress, "Pstore compression to use");
851fe1d4758SKees Cook 
852dee28e72SMatthew Garrett module_param(backend, charp, 0444);
853dee28e72SMatthew Garrett MODULE_PARM_DESC(backend, "Pstore backend to use");
854cb095afdSKees Cook 
855cb095afdSKees Cook MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
856cb095afdSKees Cook MODULE_LICENSE("GPL");
857