xref: /openbmc/linux/fs/pstore/platform.c (revision ea84b580b95521644429cc6748b6c2bf27c8b0f3)
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 /*
165*ea84b580SKees Cook  * Should pstore_dump() wait for a concurrent pstore_dump()? If
166*ea84b580SKees Cook  * not, the current pstore_dump() will report a failure to dump
167*ea84b580SKees Cook  * and return.
1689f244e9cSSeiji Aguchi  */
169*ea84b580SKees Cook static bool pstore_cannot_wait(enum kmsg_dump_reason reason)
170*ea84b580SKees Cook {
171*ea84b580SKees 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:
178*ea84b580SKees 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 {
35095047b05SKees Cook 	if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && tfm)
351cb3bee03SGeliang Tang 		crypto_free_comp(tfm);
352cb3bee03SGeliang Tang 	kfree(big_oops_buf);
353cb3bee03SGeliang Tang 	big_oops_buf = NULL;
354cb3bee03SGeliang Tang 	big_oops_buf_sz = 0;
355ee1d2674SGeliang Tang }
356ee1d2674SGeliang Tang 
357b0aad7a9SAruna Balakrishnaiah /*
358b0aad7a9SAruna Balakrishnaiah  * Called when compression fails, since the printk buffer
359b0aad7a9SAruna Balakrishnaiah  * would be fetched for compression calling it again when
360b0aad7a9SAruna Balakrishnaiah  * compression fails would have moved the iterator of
361b0aad7a9SAruna Balakrishnaiah  * printk buffer which results in fetching old contents.
362b0aad7a9SAruna Balakrishnaiah  * Copy the recent messages from big_oops_buf to psinfo->buf
363b0aad7a9SAruna Balakrishnaiah  */
364b0aad7a9SAruna Balakrishnaiah static size_t copy_kmsg_to_buffer(int hsize, size_t len)
365b0aad7a9SAruna Balakrishnaiah {
366b0aad7a9SAruna Balakrishnaiah 	size_t total_len;
367b0aad7a9SAruna Balakrishnaiah 	size_t diff;
368b0aad7a9SAruna Balakrishnaiah 
369b0aad7a9SAruna Balakrishnaiah 	total_len = hsize + len;
370b0aad7a9SAruna Balakrishnaiah 
371b0aad7a9SAruna Balakrishnaiah 	if (total_len > psinfo->bufsize) {
372b0aad7a9SAruna Balakrishnaiah 		diff = total_len - psinfo->bufsize + hsize;
373b0aad7a9SAruna Balakrishnaiah 		memcpy(psinfo->buf, big_oops_buf, hsize);
374b0aad7a9SAruna Balakrishnaiah 		memcpy(psinfo->buf + hsize, big_oops_buf + diff,
375b0aad7a9SAruna Balakrishnaiah 					psinfo->bufsize - hsize);
376b0aad7a9SAruna Balakrishnaiah 		total_len = psinfo->bufsize;
377b0aad7a9SAruna Balakrishnaiah 	} else
378b0aad7a9SAruna Balakrishnaiah 		memcpy(psinfo->buf, big_oops_buf, total_len);
379b0aad7a9SAruna Balakrishnaiah 
380b0aad7a9SAruna Balakrishnaiah 	return total_len;
381b0aad7a9SAruna Balakrishnaiah }
382b0aad7a9SAruna Balakrishnaiah 
383e581ca81SKees Cook void pstore_record_init(struct pstore_record *record,
384e581ca81SKees Cook 			struct pstore_info *psinfo)
385e581ca81SKees Cook {
386e581ca81SKees Cook 	memset(record, 0, sizeof(*record));
387e581ca81SKees Cook 
388e581ca81SKees Cook 	record->psi = psinfo;
389c7f3c595SKees Cook 
390c7f3c595SKees Cook 	/* Report zeroed timestamp if called before timekeeping has resumed. */
3917aaa822eSKees Cook 	record->time = ns_to_timespec64(ktime_get_real_fast_ns());
392e581ca81SKees Cook }
393e581ca81SKees Cook 
394ca01d6ddSTony Luck /*
3950eed84ffSKees Cook  * callback from kmsg_dump. Save as much as we can (up to kmsg_bytes) from the
3960eed84ffSKees Cook  * end of the buffer.
397ca01d6ddSTony Luck  */
398ca01d6ddSTony Luck static void pstore_dump(struct kmsg_dumper *dumper,
399e2ae715dSKay Sievers 			enum kmsg_dump_reason reason)
400ca01d6ddSTony Luck {
401e2ae715dSKay Sievers 	unsigned long	total = 0;
402381b872cSSeiji Aguchi 	const char	*why;
403b94fdd07SMatthew Garrett 	unsigned int	part = 1;
404e2ae715dSKay Sievers 	int		ret;
405ca01d6ddSTony Luck 
406381b872cSSeiji Aguchi 	why = get_reason_str(reason);
4079f6af27fSTony Luck 
408*ea84b580SKees Cook 	if (down_trylock(&psinfo->buf_lock)) {
409*ea84b580SKees Cook 		/* Failed to acquire lock: give up if we cannot wait. */
410*ea84b580SKees Cook 		if (pstore_cannot_wait(reason)) {
411*ea84b580SKees Cook 			pr_err("dump skipped in %s path: may corrupt error record\n",
412*ea84b580SKees Cook 				in_nmi() ? "NMI" : why);
413959217c8SLi Pengcheng 			return;
4149f244e9cSSeiji Aguchi 		}
415*ea84b580SKees Cook 		if (down_interruptible(&psinfo->buf_lock)) {
416*ea84b580SKees Cook 			pr_err("could not grab semaphore?!\n");
417*ea84b580SKees Cook 			return;
41898e44fdaSNamhyung Kim 		}
419*ea84b580SKees Cook 	}
420*ea84b580SKees Cook 
421ca01d6ddSTony Luck 	oopscount++;
422ca01d6ddSTony Luck 	while (total < kmsg_bytes) {
423e2ae715dSKay Sievers 		char *dst;
42476cc9580SKees Cook 		size_t dst_size;
42576cc9580SKees Cook 		int header_size;
426b0aad7a9SAruna Balakrishnaiah 		int zipped_len = -1;
42776cc9580SKees Cook 		size_t dump_size;
428e581ca81SKees Cook 		struct pstore_record record;
429e581ca81SKees Cook 
430e581ca81SKees Cook 		pstore_record_init(&record, psinfo);
431e581ca81SKees Cook 		record.type = PSTORE_TYPE_DMESG;
432e581ca81SKees Cook 		record.count = oopscount;
433e581ca81SKees Cook 		record.reason = reason;
434e581ca81SKees Cook 		record.part = part;
435e581ca81SKees Cook 		record.buf = psinfo->buf;
436e2ae715dSKay Sievers 
437*ea84b580SKees Cook 		if (big_oops_buf) {
438b0aad7a9SAruna Balakrishnaiah 			dst = big_oops_buf;
43976cc9580SKees Cook 			dst_size = big_oops_buf_sz;
440235f6d15SNamhyung Kim 		} else {
441235f6d15SNamhyung Kim 			dst = psinfo->buf;
44276cc9580SKees Cook 			dst_size = psinfo->bufsize;
443235f6d15SNamhyung Kim 		}
444235f6d15SNamhyung Kim 
44576cc9580SKees Cook 		/* Write dump header. */
44676cc9580SKees Cook 		header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why,
44776cc9580SKees Cook 				 oopscount, part);
44876cc9580SKees Cook 		dst_size -= header_size;
449b0aad7a9SAruna Balakrishnaiah 
45076cc9580SKees Cook 		/* Write dump contents. */
45176cc9580SKees Cook 		if (!kmsg_dump_get_buffer(dumper, true, dst + header_size,
45276cc9580SKees Cook 					  dst_size, &dump_size))
453b0aad7a9SAruna Balakrishnaiah 			break;
454b0aad7a9SAruna Balakrishnaiah 
455*ea84b580SKees Cook 		if (big_oops_buf) {
456b0aad7a9SAruna Balakrishnaiah 			zipped_len = pstore_compress(dst, psinfo->buf,
45776cc9580SKees Cook 						header_size + dump_size,
45876cc9580SKees Cook 						psinfo->bufsize);
459b0aad7a9SAruna Balakrishnaiah 
460b0aad7a9SAruna Balakrishnaiah 			if (zipped_len > 0) {
46176cc9580SKees Cook 				record.compressed = true;
46276cc9580SKees Cook 				record.size = zipped_len;
463b0aad7a9SAruna Balakrishnaiah 			} else {
46476cc9580SKees Cook 				record.size = copy_kmsg_to_buffer(header_size,
46576cc9580SKees Cook 								  dump_size);
466b0aad7a9SAruna Balakrishnaiah 			}
467b0aad7a9SAruna Balakrishnaiah 		} else {
46876cc9580SKees Cook 			record.size = header_size + dump_size;
469b0aad7a9SAruna Balakrishnaiah 		}
470b0aad7a9SAruna Balakrishnaiah 
47176cc9580SKees Cook 		ret = psinfo->write(&record);
472b238b8faSChen Gong 		if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted())
4736dda9266SLuck, Tony 			pstore_new_entry = 1;
474e2ae715dSKay Sievers 
47576cc9580SKees Cook 		total += record.size;
47656280682SMatthew Garrett 		part++;
477ca01d6ddSTony Luck 	}
478*ea84b580SKees Cook 
479*ea84b580SKees Cook 	up(&psinfo->buf_lock);
480ca01d6ddSTony Luck }
481ca01d6ddSTony Luck 
482ca01d6ddSTony Luck static struct kmsg_dumper pstore_dumper = {
483ca01d6ddSTony Luck 	.dump = pstore_dump,
484ca01d6ddSTony Luck };
485ca01d6ddSTony Luck 
486306e5c2aSGeliang Tang /*
487306e5c2aSGeliang Tang  * Register with kmsg_dump to save last part of console log on panic.
488306e5c2aSGeliang Tang  */
48918730411SGeliang Tang static void pstore_register_kmsg(void)
49018730411SGeliang Tang {
49118730411SGeliang Tang 	kmsg_dump_register(&pstore_dumper);
49218730411SGeliang Tang }
49318730411SGeliang Tang 
494ee1d2674SGeliang Tang static void pstore_unregister_kmsg(void)
495ee1d2674SGeliang Tang {
496ee1d2674SGeliang Tang 	kmsg_dump_unregister(&pstore_dumper);
497ee1d2674SGeliang Tang }
498ee1d2674SGeliang Tang 
499f29e5956SAnton Vorontsov #ifdef CONFIG_PSTORE_CONSOLE
500f29e5956SAnton Vorontsov static void pstore_console_write(struct console *con, const char *s, unsigned c)
501f29e5956SAnton Vorontsov {
502e581ca81SKees Cook 	struct pstore_record record;
503f29e5956SAnton Vorontsov 
504e581ca81SKees Cook 	pstore_record_init(&record, psinfo);
505e581ca81SKees Cook 	record.type = PSTORE_TYPE_CONSOLE;
506e581ca81SKees Cook 
507b10b4711SKees Cook 	record.buf = (char *)s;
508b10b4711SKees Cook 	record.size = c;
5094c9ec219SKees Cook 	psinfo->write(&record);
510f29e5956SAnton Vorontsov }
511f29e5956SAnton Vorontsov 
512f29e5956SAnton Vorontsov static struct console pstore_console = {
513f29e5956SAnton Vorontsov 	.name	= "pstore",
514f29e5956SAnton Vorontsov 	.write	= pstore_console_write,
515f29e5956SAnton Vorontsov 	.flags	= CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME,
516f29e5956SAnton Vorontsov 	.index	= -1,
517f29e5956SAnton Vorontsov };
518f29e5956SAnton Vorontsov 
519f29e5956SAnton Vorontsov static void pstore_register_console(void)
520f29e5956SAnton Vorontsov {
521f29e5956SAnton Vorontsov 	register_console(&pstore_console);
522f29e5956SAnton Vorontsov }
523ee1d2674SGeliang Tang 
524ee1d2674SGeliang Tang static void pstore_unregister_console(void)
525ee1d2674SGeliang Tang {
526ee1d2674SGeliang Tang 	unregister_console(&pstore_console);
527ee1d2674SGeliang Tang }
528f29e5956SAnton Vorontsov #else
529f29e5956SAnton Vorontsov static void pstore_register_console(void) {}
530ee1d2674SGeliang Tang static void pstore_unregister_console(void) {}
531f29e5956SAnton Vorontsov #endif
532f29e5956SAnton Vorontsov 
5334c9ec219SKees Cook static int pstore_write_user_compat(struct pstore_record *record,
534fdd03118SKees Cook 				    const char __user *buf)
5355bf6d1b9SMark Salyzyn {
53630800d99SKees Cook 	int ret = 0;
5375bf6d1b9SMark Salyzyn 
53830800d99SKees Cook 	if (record->buf)
53930800d99SKees Cook 		return -EINVAL;
5405bf6d1b9SMark Salyzyn 
541077090afSGeliang Tang 	record->buf = memdup_user(buf, record->size);
542dfd6fa39SHirofumi Nakagawa 	if (IS_ERR(record->buf)) {
543077090afSGeliang Tang 		ret = PTR_ERR(record->buf);
54430800d99SKees Cook 		goto out;
5455bf6d1b9SMark Salyzyn 	}
54630800d99SKees Cook 
5474c9ec219SKees Cook 	ret = record->psi->write(record);
54830800d99SKees Cook 
54930800d99SKees Cook 	kfree(record->buf);
550077090afSGeliang Tang out:
55130800d99SKees Cook 	record->buf = NULL;
55230800d99SKees Cook 
55330800d99SKees Cook 	return unlikely(ret < 0) ? ret : record->size;
5545bf6d1b9SMark Salyzyn }
5555bf6d1b9SMark Salyzyn 
556ca01d6ddSTony Luck /*
557ca01d6ddSTony Luck  * platform specific persistent storage driver registers with
558ca01d6ddSTony Luck  * us here. If pstore is already mounted, call the platform
559ca01d6ddSTony Luck  * read function right away to populate the file system. If not
560ca01d6ddSTony Luck  * then the pstore mount code will call us later to fill out
561ca01d6ddSTony Luck  * the file system.
562ca01d6ddSTony Luck  */
563ca01d6ddSTony Luck int pstore_register(struct pstore_info *psi)
564ca01d6ddSTony Luck {
565ca01d6ddSTony Luck 	struct module *owner = psi->owner;
566ca01d6ddSTony Luck 
5670d7cd09aSKees Cook 	if (backend && strcmp(backend, psi->name)) {
5680d7cd09aSKees Cook 		pr_warn("ignoring unexpected backend '%s'\n", psi->name);
5698e48b1a8SLenny Szubowicz 		return -EPERM;
5700d7cd09aSKees Cook 	}
5718e48b1a8SLenny Szubowicz 
5724c9ec219SKees Cook 	/* Sanity check flags. */
5734c9ec219SKees Cook 	if (!psi->flags) {
5744c9ec219SKees Cook 		pr_warn("backend '%s' must support at least one frontend\n",
5754c9ec219SKees Cook 			psi->name);
5764c9ec219SKees Cook 		return -EINVAL;
5774c9ec219SKees Cook 	}
5784c9ec219SKees Cook 
5794c9ec219SKees Cook 	/* Check for required functions. */
5804c9ec219SKees Cook 	if (!psi->read || !psi->write) {
5814c9ec219SKees Cook 		pr_warn("backend '%s' must implement read() and write()\n",
5824c9ec219SKees Cook 			psi->name);
5834c9ec219SKees Cook 		return -EINVAL;
5844c9ec219SKees Cook 	}
5854c9ec219SKees Cook 
586ca01d6ddSTony Luck 	spin_lock(&pstore_lock);
587ca01d6ddSTony Luck 	if (psinfo) {
5880d7cd09aSKees Cook 		pr_warn("backend '%s' already loaded: ignoring '%s'\n",
5890d7cd09aSKees Cook 			psinfo->name, psi->name);
590ca01d6ddSTony Luck 		spin_unlock(&pstore_lock);
591ca01d6ddSTony Luck 		return -EBUSY;
592ca01d6ddSTony Luck 	}
593dee28e72SMatthew Garrett 
5944c9ec219SKees Cook 	if (!psi->write_user)
5954c9ec219SKees Cook 		psi->write_user = pstore_write_user_compat;
596ca01d6ddSTony Luck 	psinfo = psi;
597f6f82851SKees Cook 	mutex_init(&psinfo->read_mutex);
598*ea84b580SKees Cook 	sema_init(&psinfo->buf_lock, 1);
599ca01d6ddSTony Luck 	spin_unlock(&pstore_lock);
600ca01d6ddSTony Luck 
601ca01d6ddSTony Luck 	if (owner && !try_module_get(owner)) {
602ca01d6ddSTony Luck 		psinfo = NULL;
603ca01d6ddSTony Luck 		return -EINVAL;
604ca01d6ddSTony Luck 	}
605ca01d6ddSTony Luck 
606b0aad7a9SAruna Balakrishnaiah 	allocate_buf_for_compression();
607b0aad7a9SAruna Balakrishnaiah 
608ca01d6ddSTony Luck 	if (pstore_is_mounted())
6096dda9266SLuck, Tony 		pstore_get_records(0);
610ca01d6ddSTony Luck 
611c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_DMESG)
61218730411SGeliang Tang 		pstore_register_kmsg();
613c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_CONSOLE)
614f29e5956SAnton Vorontsov 		pstore_register_console();
615c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_FTRACE)
61665f8c95eSAnton Vorontsov 		pstore_register_ftrace();
617c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_PMSG)
6189d5438f4SMark Salyzyn 		pstore_register_pmsg();
619ca01d6ddSTony Luck 
6206330d553SKees Cook 	/* Start watching for new records, if desired. */
621a3f5f075SAnton Vorontsov 	if (pstore_update_ms >= 0) {
622a3f5f075SAnton Vorontsov 		pstore_timer.expires = jiffies +
623a3f5f075SAnton Vorontsov 			msecs_to_jiffies(pstore_update_ms);
6246dda9266SLuck, Tony 		add_timer(&pstore_timer);
625a3f5f075SAnton Vorontsov 	}
6266dda9266SLuck, Tony 
62742222c2aSWang Long 	/*
62842222c2aSWang Long 	 * Update the module parameter backend, so it is visible
62942222c2aSWang Long 	 * through /sys/module/pstore/parameters/backend
63042222c2aSWang Long 	 */
63142222c2aSWang Long 	backend = psi->name;
63242222c2aSWang Long 
633ef748853SFabian Frederick 	pr_info("Registered %s as persistent store backend\n", psi->name);
6348e48b1a8SLenny Szubowicz 
6351344dd86SKees Cook 	module_put(owner);
6361344dd86SKees Cook 
637ca01d6ddSTony Luck 	return 0;
638ca01d6ddSTony Luck }
639ca01d6ddSTony Luck EXPORT_SYMBOL_GPL(pstore_register);
640ca01d6ddSTony Luck 
641ee1d2674SGeliang Tang void pstore_unregister(struct pstore_info *psi)
642ee1d2674SGeliang Tang {
6436330d553SKees Cook 	/* Stop timer and make sure all work has finished. */
6446330d553SKees Cook 	pstore_update_ms = -1;
6456330d553SKees Cook 	del_timer_sync(&pstore_timer);
6466330d553SKees Cook 	flush_work(&pstore_work);
6476330d553SKees Cook 
648c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_PMSG)
649ee1d2674SGeliang Tang 		pstore_unregister_pmsg();
650c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_FTRACE)
651ee1d2674SGeliang Tang 		pstore_unregister_ftrace();
652c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_CONSOLE)
653ee1d2674SGeliang Tang 		pstore_unregister_console();
654c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_DMESG)
655ee1d2674SGeliang Tang 		pstore_unregister_kmsg();
656ee1d2674SGeliang Tang 
657ee1d2674SGeliang Tang 	free_buf_for_compression();
658ee1d2674SGeliang Tang 
659ee1d2674SGeliang Tang 	psinfo = NULL;
660ee1d2674SGeliang Tang 	backend = NULL;
661ee1d2674SGeliang Tang }
662ee1d2674SGeliang Tang EXPORT_SYMBOL_GPL(pstore_unregister);
663ee1d2674SGeliang Tang 
664634f8f51SKees Cook static void decompress_record(struct pstore_record *record)
665634f8f51SKees Cook {
666bdabc8e7SKees Cook 	int ret;
667634f8f51SKees Cook 	int unzipped_len;
668bdabc8e7SKees Cook 	char *unzipped, *workspace;
669634f8f51SKees Cook 
6704a16d1cbSAnkit Kumar 	if (!record->compressed)
6714a16d1cbSAnkit Kumar 		return;
6724a16d1cbSAnkit Kumar 
673634f8f51SKees Cook 	/* Only PSTORE_TYPE_DMESG support compression. */
6744a16d1cbSAnkit Kumar 	if (record->type != PSTORE_TYPE_DMESG) {
675634f8f51SKees Cook 		pr_warn("ignored compressed record type %d\n", record->type);
676634f8f51SKees Cook 		return;
677634f8f51SKees Cook 	}
678634f8f51SKees Cook 
679bdabc8e7SKees Cook 	/* Missing compression buffer means compression was not initialized. */
680634f8f51SKees Cook 	if (!big_oops_buf) {
681bdabc8e7SKees Cook 		pr_warn("no decompression method initialized!\n");
682634f8f51SKees Cook 		return;
683634f8f51SKees Cook 	}
684634f8f51SKees Cook 
685bdabc8e7SKees Cook 	/* Allocate enough space to hold max decompression and ECC. */
686bdabc8e7SKees Cook 	unzipped_len = big_oops_buf_sz;
687bdabc8e7SKees Cook 	workspace = kmalloc(unzipped_len + record->ecc_notice_size,
6887e8cc8dcSKees Cook 			    GFP_KERNEL);
689bdabc8e7SKees Cook 	if (!workspace)
690bdabc8e7SKees Cook 		return;
691bdabc8e7SKees Cook 
692bdabc8e7SKees Cook 	/* After decompression "unzipped_len" is almost certainly smaller. */
693bdabc8e7SKees Cook 	ret = crypto_comp_decompress(tfm, record->buf, record->size,
694bdabc8e7SKees Cook 					  workspace, &unzipped_len);
695bdabc8e7SKees Cook 	if (ret) {
696bdabc8e7SKees Cook 		pr_err("crypto_comp_decompress failed, ret = %d!\n", ret);
697bdabc8e7SKees Cook 		kfree(workspace);
6987e8cc8dcSKees Cook 		return;
6997e8cc8dcSKees Cook 	}
7007e8cc8dcSKees Cook 
7017e8cc8dcSKees Cook 	/* Append ECC notice to decompressed buffer. */
702bdabc8e7SKees Cook 	memcpy(workspace + unzipped_len, record->buf + record->size,
703634f8f51SKees Cook 	       record->ecc_notice_size);
7047e8cc8dcSKees Cook 
705bdabc8e7SKees Cook 	/* Copy decompressed contents into an minimum-sized allocation. */
706bdabc8e7SKees Cook 	unzipped = kmemdup(workspace, unzipped_len + record->ecc_notice_size,
707bdabc8e7SKees Cook 			   GFP_KERNEL);
708bdabc8e7SKees Cook 	kfree(workspace);
709bdabc8e7SKees Cook 	if (!unzipped)
710bdabc8e7SKees Cook 		return;
711bdabc8e7SKees Cook 
712bdabc8e7SKees Cook 	/* Swap out compressed contents with decompressed contents. */
713634f8f51SKees Cook 	kfree(record->buf);
714bdabc8e7SKees Cook 	record->buf = unzipped;
715634f8f51SKees Cook 	record->size = unzipped_len;
716634f8f51SKees Cook 	record->compressed = false;
717634f8f51SKees Cook }
718634f8f51SKees Cook 
719ca01d6ddSTony Luck /*
7203a7d2fd1SKees Cook  * Read all the records from one persistent store backend. Create
7216dda9266SLuck, Tony  * files in our filesystem.  Don't warn about -EEXIST errors
7226dda9266SLuck, Tony  * when we are re-scanning the backing store looking to add new
7236dda9266SLuck, Tony  * error records.
724ca01d6ddSTony Luck  */
7253a7d2fd1SKees Cook void pstore_get_backend_records(struct pstore_info *psi,
7263a7d2fd1SKees Cook 				struct dentry *root, int quiet)
727ca01d6ddSTony Luck {
7282a2b0acfSKees Cook 	int failed = 0;
729656de42eSKees Cook 	unsigned int stop_loop = 65536;
730ca01d6ddSTony Luck 
7313a7d2fd1SKees Cook 	if (!psi || !root)
732ca01d6ddSTony Luck 		return;
733ca01d6ddSTony Luck 
734f6f82851SKees Cook 	mutex_lock(&psi->read_mutex);
7352174f6dfSKees Cook 	if (psi->open && psi->open(psi))
73606cf91b4SChen Gong 		goto out;
73706cf91b4SChen Gong 
7381dfff7ddSKees Cook 	/*
7391dfff7ddSKees Cook 	 * Backend callback read() allocates record.buf. decompress_record()
7401dfff7ddSKees Cook 	 * may reallocate record.buf. On success, pstore_mkfile() will keep
7411dfff7ddSKees Cook 	 * the record.buf, so free it only on failure.
7421dfff7ddSKees Cook 	 */
743656de42eSKees Cook 	for (; stop_loop; stop_loop--) {
7442a2b0acfSKees Cook 		struct pstore_record *record;
7452a2b0acfSKees Cook 		int rc;
7462a2b0acfSKees Cook 
7472a2b0acfSKees Cook 		record = kzalloc(sizeof(*record), GFP_KERNEL);
7482a2b0acfSKees Cook 		if (!record) {
7492a2b0acfSKees Cook 			pr_err("out of memory creating record\n");
7502a2b0acfSKees Cook 			break;
7512a2b0acfSKees Cook 		}
752e581ca81SKees Cook 		pstore_record_init(record, psi);
7532a2b0acfSKees Cook 
7542a2b0acfSKees Cook 		record->size = psi->read(record);
7552a2b0acfSKees Cook 
7562a2b0acfSKees Cook 		/* No more records left in backend? */
757f6525b96SDouglas Anderson 		if (record->size <= 0) {
758f6525b96SDouglas Anderson 			kfree(record);
7592a2b0acfSKees Cook 			break;
760f6525b96SDouglas Anderson 		}
7612a2b0acfSKees Cook 
7622a2b0acfSKees Cook 		decompress_record(record);
7633a7d2fd1SKees Cook 		rc = pstore_mkfile(root, record);
7641dfff7ddSKees Cook 		if (rc) {
76583f70f07SKees Cook 			/* pstore_mkfile() did not take record, so free it. */
7662a2b0acfSKees Cook 			kfree(record->buf);
76783f70f07SKees Cook 			kfree(record);
7681dfff7ddSKees Cook 			if (rc != -EEXIST || !quiet)
7691dfff7ddSKees Cook 				failed++;
7701dfff7ddSKees Cook 		}
771ca01d6ddSTony Luck 	}
7722174f6dfSKees Cook 	if (psi->close)
77306cf91b4SChen Gong 		psi->close(psi);
77406cf91b4SChen Gong out:
775f6f82851SKees Cook 	mutex_unlock(&psi->read_mutex);
776ca01d6ddSTony Luck 
777ca01d6ddSTony Luck 	if (failed)
778656de42eSKees Cook 		pr_warn("failed to create %d record(s) from '%s'\n",
779ca01d6ddSTony Luck 			failed, psi->name);
780656de42eSKees Cook 	if (!stop_loop)
781656de42eSKees Cook 		pr_err("looping? Too many records seen from '%s'\n",
782656de42eSKees Cook 			psi->name);
783ca01d6ddSTony Luck }
784ca01d6ddSTony Luck 
7856dda9266SLuck, Tony static void pstore_dowork(struct work_struct *work)
7866dda9266SLuck, Tony {
7876dda9266SLuck, Tony 	pstore_get_records(1);
7886dda9266SLuck, Tony }
7896dda9266SLuck, Tony 
79024ed960aSKees Cook static void pstore_timefunc(struct timer_list *unused)
7916dda9266SLuck, Tony {
7926dda9266SLuck, Tony 	if (pstore_new_entry) {
7936dda9266SLuck, Tony 		pstore_new_entry = 0;
7946dda9266SLuck, Tony 		schedule_work(&pstore_work);
7956dda9266SLuck, Tony 	}
7966dda9266SLuck, Tony 
7976330d553SKees Cook 	if (pstore_update_ms >= 0)
7986330d553SKees Cook 		mod_timer(&pstore_timer,
7996330d553SKees Cook 			  jiffies + msecs_to_jiffies(pstore_update_ms));
8006dda9266SLuck, Tony }
8016dda9266SLuck, Tony 
802fe1d4758SKees Cook void __init pstore_choose_compression(void)
803fe1d4758SKees Cook {
804fe1d4758SKees Cook 	const struct pstore_zbackend *step;
805fe1d4758SKees Cook 
806fe1d4758SKees Cook 	if (!compress)
807fe1d4758SKees Cook 		return;
808fe1d4758SKees Cook 
809fe1d4758SKees Cook 	for (step = zbackends; step->name; step++) {
810fe1d4758SKees Cook 		if (!strcmp(compress, step->name)) {
811fe1d4758SKees Cook 			zbackend = step;
812fe1d4758SKees Cook 			return;
813fe1d4758SKees Cook 		}
814fe1d4758SKees Cook 	}
815fe1d4758SKees Cook }
816fe1d4758SKees Cook 
817cb095afdSKees Cook static int __init pstore_init(void)
818cb095afdSKees Cook {
819cb095afdSKees Cook 	int ret;
820cb095afdSKees Cook 
821cb095afdSKees Cook 	pstore_choose_compression();
822cb095afdSKees Cook 
82341603165SJoel Fernandes (Google) 	/*
82441603165SJoel Fernandes (Google) 	 * Check if any pstore backends registered earlier but did not
82541603165SJoel Fernandes (Google) 	 * initialize compression because crypto was not ready. If so,
82641603165SJoel Fernandes (Google) 	 * initialize compression now.
82741603165SJoel Fernandes (Google) 	 */
82841603165SJoel Fernandes (Google) 	allocate_buf_for_compression();
82941603165SJoel Fernandes (Google) 
830cb095afdSKees Cook 	ret = pstore_init_fs();
831cb095afdSKees Cook 	if (ret)
832cb095afdSKees Cook 		return ret;
833cb095afdSKees Cook 
834cb095afdSKees Cook 	return 0;
835cb095afdSKees Cook }
83641603165SJoel Fernandes (Google) late_initcall(pstore_init);
837cb095afdSKees Cook 
838cb095afdSKees Cook static void __exit pstore_exit(void)
839cb095afdSKees Cook {
840cb095afdSKees Cook 	pstore_exit_fs();
841cb095afdSKees Cook }
842cb095afdSKees Cook module_exit(pstore_exit)
843cb095afdSKees Cook 
844fe1d4758SKees Cook module_param(compress, charp, 0444);
845fe1d4758SKees Cook MODULE_PARM_DESC(compress, "Pstore compression to use");
846fe1d4758SKees Cook 
847dee28e72SMatthew Garrett module_param(backend, charp, 0444);
848dee28e72SMatthew Garrett MODULE_PARM_DESC(backend, "Pstore backend to use");
849cb095afdSKees Cook 
850cb095afdSKees Cook MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
851cb095afdSKees Cook MODULE_LICENSE("GPL");
852