xref: /openbmc/linux/fs/pstore/platform.c (revision 4c6c4d34536744f2c9e171ef5bb548a06a525501)
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 {
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 
408ea84b580SKees Cook 	if (down_trylock(&psinfo->buf_lock)) {
409ea84b580SKees Cook 		/* Failed to acquire lock: give up if we cannot wait. */
410ea84b580SKees Cook 		if (pstore_cannot_wait(reason)) {
411ea84b580SKees Cook 			pr_err("dump skipped in %s path: may corrupt error record\n",
412ea84b580SKees Cook 				in_nmi() ? "NMI" : why);
413959217c8SLi Pengcheng 			return;
4149f244e9cSSeiji Aguchi 		}
415ea84b580SKees Cook 		if (down_interruptible(&psinfo->buf_lock)) {
416ea84b580SKees Cook 			pr_err("could not grab semaphore?!\n");
417ea84b580SKees Cook 			return;
41898e44fdaSNamhyung Kim 		}
419ea84b580SKees Cook 	}
420ea84b580SKees 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 
437ea84b580SKees 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 
455ea84b580SKees 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 	}
478ea84b580SKees Cook 
479ea84b580SKees 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 
504*4c6c4d34SYue Hu 	if (!c)
505*4c6c4d34SYue Hu 		return;
506*4c6c4d34SYue Hu 
507e581ca81SKees Cook 	pstore_record_init(&record, psinfo);
508e581ca81SKees Cook 	record.type = PSTORE_TYPE_CONSOLE;
509e581ca81SKees Cook 
510b10b4711SKees Cook 	record.buf = (char *)s;
511b10b4711SKees Cook 	record.size = c;
5124c9ec219SKees Cook 	psinfo->write(&record);
513f29e5956SAnton Vorontsov }
514f29e5956SAnton Vorontsov 
515f29e5956SAnton Vorontsov static struct console pstore_console = {
516f29e5956SAnton Vorontsov 	.name	= "pstore",
517f29e5956SAnton Vorontsov 	.write	= pstore_console_write,
518f29e5956SAnton Vorontsov 	.flags	= CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME,
519f29e5956SAnton Vorontsov 	.index	= -1,
520f29e5956SAnton Vorontsov };
521f29e5956SAnton Vorontsov 
522f29e5956SAnton Vorontsov static void pstore_register_console(void)
523f29e5956SAnton Vorontsov {
524f29e5956SAnton Vorontsov 	register_console(&pstore_console);
525f29e5956SAnton Vorontsov }
526ee1d2674SGeliang Tang 
527ee1d2674SGeliang Tang static void pstore_unregister_console(void)
528ee1d2674SGeliang Tang {
529ee1d2674SGeliang Tang 	unregister_console(&pstore_console);
530ee1d2674SGeliang Tang }
531f29e5956SAnton Vorontsov #else
532f29e5956SAnton Vorontsov static void pstore_register_console(void) {}
533ee1d2674SGeliang Tang static void pstore_unregister_console(void) {}
534f29e5956SAnton Vorontsov #endif
535f29e5956SAnton Vorontsov 
5364c9ec219SKees Cook static int pstore_write_user_compat(struct pstore_record *record,
537fdd03118SKees Cook 				    const char __user *buf)
5385bf6d1b9SMark Salyzyn {
53930800d99SKees Cook 	int ret = 0;
5405bf6d1b9SMark Salyzyn 
54130800d99SKees Cook 	if (record->buf)
54230800d99SKees Cook 		return -EINVAL;
5435bf6d1b9SMark Salyzyn 
544077090afSGeliang Tang 	record->buf = memdup_user(buf, record->size);
545dfd6fa39SHirofumi Nakagawa 	if (IS_ERR(record->buf)) {
546077090afSGeliang Tang 		ret = PTR_ERR(record->buf);
54730800d99SKees Cook 		goto out;
5485bf6d1b9SMark Salyzyn 	}
54930800d99SKees Cook 
5504c9ec219SKees Cook 	ret = record->psi->write(record);
55130800d99SKees Cook 
55230800d99SKees Cook 	kfree(record->buf);
553077090afSGeliang Tang out:
55430800d99SKees Cook 	record->buf = NULL;
55530800d99SKees Cook 
55630800d99SKees Cook 	return unlikely(ret < 0) ? ret : record->size;
5575bf6d1b9SMark Salyzyn }
5585bf6d1b9SMark Salyzyn 
559ca01d6ddSTony Luck /*
560ca01d6ddSTony Luck  * platform specific persistent storage driver registers with
561ca01d6ddSTony Luck  * us here. If pstore is already mounted, call the platform
562ca01d6ddSTony Luck  * read function right away to populate the file system. If not
563ca01d6ddSTony Luck  * then the pstore mount code will call us later to fill out
564ca01d6ddSTony Luck  * the file system.
565ca01d6ddSTony Luck  */
566ca01d6ddSTony Luck int pstore_register(struct pstore_info *psi)
567ca01d6ddSTony Luck {
568ca01d6ddSTony Luck 	struct module *owner = psi->owner;
569ca01d6ddSTony Luck 
5700d7cd09aSKees Cook 	if (backend && strcmp(backend, psi->name)) {
5710d7cd09aSKees Cook 		pr_warn("ignoring unexpected backend '%s'\n", psi->name);
5728e48b1a8SLenny Szubowicz 		return -EPERM;
5730d7cd09aSKees Cook 	}
5748e48b1a8SLenny Szubowicz 
5754c9ec219SKees Cook 	/* Sanity check flags. */
5764c9ec219SKees Cook 	if (!psi->flags) {
5774c9ec219SKees Cook 		pr_warn("backend '%s' must support at least one frontend\n",
5784c9ec219SKees Cook 			psi->name);
5794c9ec219SKees Cook 		return -EINVAL;
5804c9ec219SKees Cook 	}
5814c9ec219SKees Cook 
5824c9ec219SKees Cook 	/* Check for required functions. */
5834c9ec219SKees Cook 	if (!psi->read || !psi->write) {
5844c9ec219SKees Cook 		pr_warn("backend '%s' must implement read() and write()\n",
5854c9ec219SKees Cook 			psi->name);
5864c9ec219SKees Cook 		return -EINVAL;
5874c9ec219SKees Cook 	}
5884c9ec219SKees Cook 
589ca01d6ddSTony Luck 	spin_lock(&pstore_lock);
590ca01d6ddSTony Luck 	if (psinfo) {
5910d7cd09aSKees Cook 		pr_warn("backend '%s' already loaded: ignoring '%s'\n",
5920d7cd09aSKees Cook 			psinfo->name, psi->name);
593ca01d6ddSTony Luck 		spin_unlock(&pstore_lock);
594ca01d6ddSTony Luck 		return -EBUSY;
595ca01d6ddSTony Luck 	}
596dee28e72SMatthew Garrett 
5974c9ec219SKees Cook 	if (!psi->write_user)
5984c9ec219SKees Cook 		psi->write_user = pstore_write_user_compat;
599ca01d6ddSTony Luck 	psinfo = psi;
600f6f82851SKees Cook 	mutex_init(&psinfo->read_mutex);
601ea84b580SKees Cook 	sema_init(&psinfo->buf_lock, 1);
602ca01d6ddSTony Luck 	spin_unlock(&pstore_lock);
603ca01d6ddSTony Luck 
604ca01d6ddSTony Luck 	if (owner && !try_module_get(owner)) {
605ca01d6ddSTony Luck 		psinfo = NULL;
606ca01d6ddSTony Luck 		return -EINVAL;
607ca01d6ddSTony Luck 	}
608ca01d6ddSTony Luck 
609b0aad7a9SAruna Balakrishnaiah 	allocate_buf_for_compression();
610b0aad7a9SAruna Balakrishnaiah 
611ca01d6ddSTony Luck 	if (pstore_is_mounted())
6126dda9266SLuck, Tony 		pstore_get_records(0);
613ca01d6ddSTony Luck 
614c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_DMESG)
61518730411SGeliang Tang 		pstore_register_kmsg();
616c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_CONSOLE)
617f29e5956SAnton Vorontsov 		pstore_register_console();
618c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_FTRACE)
61965f8c95eSAnton Vorontsov 		pstore_register_ftrace();
620c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_PMSG)
6219d5438f4SMark Salyzyn 		pstore_register_pmsg();
622ca01d6ddSTony Luck 
6236330d553SKees Cook 	/* Start watching for new records, if desired. */
624a3f5f075SAnton Vorontsov 	if (pstore_update_ms >= 0) {
625a3f5f075SAnton Vorontsov 		pstore_timer.expires = jiffies +
626a3f5f075SAnton Vorontsov 			msecs_to_jiffies(pstore_update_ms);
6276dda9266SLuck, Tony 		add_timer(&pstore_timer);
628a3f5f075SAnton Vorontsov 	}
6296dda9266SLuck, Tony 
63042222c2aSWang Long 	/*
63142222c2aSWang Long 	 * Update the module parameter backend, so it is visible
63242222c2aSWang Long 	 * through /sys/module/pstore/parameters/backend
63342222c2aSWang Long 	 */
63442222c2aSWang Long 	backend = psi->name;
63542222c2aSWang Long 
636ef748853SFabian Frederick 	pr_info("Registered %s as persistent store backend\n", psi->name);
6378e48b1a8SLenny Szubowicz 
6381344dd86SKees Cook 	module_put(owner);
6391344dd86SKees Cook 
640ca01d6ddSTony Luck 	return 0;
641ca01d6ddSTony Luck }
642ca01d6ddSTony Luck EXPORT_SYMBOL_GPL(pstore_register);
643ca01d6ddSTony Luck 
644ee1d2674SGeliang Tang void pstore_unregister(struct pstore_info *psi)
645ee1d2674SGeliang Tang {
6466330d553SKees Cook 	/* Stop timer and make sure all work has finished. */
6476330d553SKees Cook 	pstore_update_ms = -1;
6486330d553SKees Cook 	del_timer_sync(&pstore_timer);
6496330d553SKees Cook 	flush_work(&pstore_work);
6506330d553SKees Cook 
651c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_PMSG)
652ee1d2674SGeliang Tang 		pstore_unregister_pmsg();
653c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_FTRACE)
654ee1d2674SGeliang Tang 		pstore_unregister_ftrace();
655c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_CONSOLE)
656ee1d2674SGeliang Tang 		pstore_unregister_console();
657c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_DMESG)
658ee1d2674SGeliang Tang 		pstore_unregister_kmsg();
659ee1d2674SGeliang Tang 
660ee1d2674SGeliang Tang 	free_buf_for_compression();
661ee1d2674SGeliang Tang 
662ee1d2674SGeliang Tang 	psinfo = NULL;
663ee1d2674SGeliang Tang 	backend = NULL;
664ee1d2674SGeliang Tang }
665ee1d2674SGeliang Tang EXPORT_SYMBOL_GPL(pstore_unregister);
666ee1d2674SGeliang Tang 
667634f8f51SKees Cook static void decompress_record(struct pstore_record *record)
668634f8f51SKees Cook {
669bdabc8e7SKees Cook 	int ret;
670634f8f51SKees Cook 	int unzipped_len;
671bdabc8e7SKees Cook 	char *unzipped, *workspace;
672634f8f51SKees Cook 
6734a16d1cbSAnkit Kumar 	if (!record->compressed)
6744a16d1cbSAnkit Kumar 		return;
6754a16d1cbSAnkit Kumar 
676634f8f51SKees Cook 	/* Only PSTORE_TYPE_DMESG support compression. */
6774a16d1cbSAnkit Kumar 	if (record->type != PSTORE_TYPE_DMESG) {
678634f8f51SKees Cook 		pr_warn("ignored compressed record type %d\n", record->type);
679634f8f51SKees Cook 		return;
680634f8f51SKees Cook 	}
681634f8f51SKees Cook 
682bdabc8e7SKees Cook 	/* Missing compression buffer means compression was not initialized. */
683634f8f51SKees Cook 	if (!big_oops_buf) {
684bdabc8e7SKees Cook 		pr_warn("no decompression method initialized!\n");
685634f8f51SKees Cook 		return;
686634f8f51SKees Cook 	}
687634f8f51SKees Cook 
688bdabc8e7SKees Cook 	/* Allocate enough space to hold max decompression and ECC. */
689bdabc8e7SKees Cook 	unzipped_len = big_oops_buf_sz;
690bdabc8e7SKees Cook 	workspace = kmalloc(unzipped_len + record->ecc_notice_size,
6917e8cc8dcSKees Cook 			    GFP_KERNEL);
692bdabc8e7SKees Cook 	if (!workspace)
693bdabc8e7SKees Cook 		return;
694bdabc8e7SKees Cook 
695bdabc8e7SKees Cook 	/* After decompression "unzipped_len" is almost certainly smaller. */
696bdabc8e7SKees Cook 	ret = crypto_comp_decompress(tfm, record->buf, record->size,
697bdabc8e7SKees Cook 					  workspace, &unzipped_len);
698bdabc8e7SKees Cook 	if (ret) {
699bdabc8e7SKees Cook 		pr_err("crypto_comp_decompress failed, ret = %d!\n", ret);
700bdabc8e7SKees Cook 		kfree(workspace);
7017e8cc8dcSKees Cook 		return;
7027e8cc8dcSKees Cook 	}
7037e8cc8dcSKees Cook 
7047e8cc8dcSKees Cook 	/* Append ECC notice to decompressed buffer. */
705bdabc8e7SKees Cook 	memcpy(workspace + unzipped_len, record->buf + record->size,
706634f8f51SKees Cook 	       record->ecc_notice_size);
7077e8cc8dcSKees Cook 
708bdabc8e7SKees Cook 	/* Copy decompressed contents into an minimum-sized allocation. */
709bdabc8e7SKees Cook 	unzipped = kmemdup(workspace, unzipped_len + record->ecc_notice_size,
710bdabc8e7SKees Cook 			   GFP_KERNEL);
711bdabc8e7SKees Cook 	kfree(workspace);
712bdabc8e7SKees Cook 	if (!unzipped)
713bdabc8e7SKees Cook 		return;
714bdabc8e7SKees Cook 
715bdabc8e7SKees Cook 	/* Swap out compressed contents with decompressed contents. */
716634f8f51SKees Cook 	kfree(record->buf);
717bdabc8e7SKees Cook 	record->buf = unzipped;
718634f8f51SKees Cook 	record->size = unzipped_len;
719634f8f51SKees Cook 	record->compressed = false;
720634f8f51SKees Cook }
721634f8f51SKees Cook 
722ca01d6ddSTony Luck /*
7233a7d2fd1SKees Cook  * Read all the records from one persistent store backend. Create
7246dda9266SLuck, Tony  * files in our filesystem.  Don't warn about -EEXIST errors
7256dda9266SLuck, Tony  * when we are re-scanning the backing store looking to add new
7266dda9266SLuck, Tony  * error records.
727ca01d6ddSTony Luck  */
7283a7d2fd1SKees Cook void pstore_get_backend_records(struct pstore_info *psi,
7293a7d2fd1SKees Cook 				struct dentry *root, int quiet)
730ca01d6ddSTony Luck {
7312a2b0acfSKees Cook 	int failed = 0;
732656de42eSKees Cook 	unsigned int stop_loop = 65536;
733ca01d6ddSTony Luck 
7343a7d2fd1SKees Cook 	if (!psi || !root)
735ca01d6ddSTony Luck 		return;
736ca01d6ddSTony Luck 
737f6f82851SKees Cook 	mutex_lock(&psi->read_mutex);
7382174f6dfSKees Cook 	if (psi->open && psi->open(psi))
73906cf91b4SChen Gong 		goto out;
74006cf91b4SChen Gong 
7411dfff7ddSKees Cook 	/*
7421dfff7ddSKees Cook 	 * Backend callback read() allocates record.buf. decompress_record()
7431dfff7ddSKees Cook 	 * may reallocate record.buf. On success, pstore_mkfile() will keep
7441dfff7ddSKees Cook 	 * the record.buf, so free it only on failure.
7451dfff7ddSKees Cook 	 */
746656de42eSKees Cook 	for (; stop_loop; stop_loop--) {
7472a2b0acfSKees Cook 		struct pstore_record *record;
7482a2b0acfSKees Cook 		int rc;
7492a2b0acfSKees Cook 
7502a2b0acfSKees Cook 		record = kzalloc(sizeof(*record), GFP_KERNEL);
7512a2b0acfSKees Cook 		if (!record) {
7522a2b0acfSKees Cook 			pr_err("out of memory creating record\n");
7532a2b0acfSKees Cook 			break;
7542a2b0acfSKees Cook 		}
755e581ca81SKees Cook 		pstore_record_init(record, psi);
7562a2b0acfSKees Cook 
7572a2b0acfSKees Cook 		record->size = psi->read(record);
7582a2b0acfSKees Cook 
7592a2b0acfSKees Cook 		/* No more records left in backend? */
760f6525b96SDouglas Anderson 		if (record->size <= 0) {
761f6525b96SDouglas Anderson 			kfree(record);
7622a2b0acfSKees Cook 			break;
763f6525b96SDouglas Anderson 		}
7642a2b0acfSKees Cook 
7652a2b0acfSKees Cook 		decompress_record(record);
7663a7d2fd1SKees Cook 		rc = pstore_mkfile(root, record);
7671dfff7ddSKees Cook 		if (rc) {
76883f70f07SKees Cook 			/* pstore_mkfile() did not take record, so free it. */
7692a2b0acfSKees Cook 			kfree(record->buf);
77083f70f07SKees Cook 			kfree(record);
7711dfff7ddSKees Cook 			if (rc != -EEXIST || !quiet)
7721dfff7ddSKees Cook 				failed++;
7731dfff7ddSKees Cook 		}
774ca01d6ddSTony Luck 	}
7752174f6dfSKees Cook 	if (psi->close)
77606cf91b4SChen Gong 		psi->close(psi);
77706cf91b4SChen Gong out:
778f6f82851SKees Cook 	mutex_unlock(&psi->read_mutex);
779ca01d6ddSTony Luck 
780ca01d6ddSTony Luck 	if (failed)
781656de42eSKees Cook 		pr_warn("failed to create %d record(s) from '%s'\n",
782ca01d6ddSTony Luck 			failed, psi->name);
783656de42eSKees Cook 	if (!stop_loop)
784656de42eSKees Cook 		pr_err("looping? Too many records seen from '%s'\n",
785656de42eSKees Cook 			psi->name);
786ca01d6ddSTony Luck }
787ca01d6ddSTony Luck 
7886dda9266SLuck, Tony static void pstore_dowork(struct work_struct *work)
7896dda9266SLuck, Tony {
7906dda9266SLuck, Tony 	pstore_get_records(1);
7916dda9266SLuck, Tony }
7926dda9266SLuck, Tony 
79324ed960aSKees Cook static void pstore_timefunc(struct timer_list *unused)
7946dda9266SLuck, Tony {
7956dda9266SLuck, Tony 	if (pstore_new_entry) {
7966dda9266SLuck, Tony 		pstore_new_entry = 0;
7976dda9266SLuck, Tony 		schedule_work(&pstore_work);
7986dda9266SLuck, Tony 	}
7996dda9266SLuck, Tony 
8006330d553SKees Cook 	if (pstore_update_ms >= 0)
8016330d553SKees Cook 		mod_timer(&pstore_timer,
8026330d553SKees Cook 			  jiffies + msecs_to_jiffies(pstore_update_ms));
8036dda9266SLuck, Tony }
8046dda9266SLuck, Tony 
805fe1d4758SKees Cook void __init pstore_choose_compression(void)
806fe1d4758SKees Cook {
807fe1d4758SKees Cook 	const struct pstore_zbackend *step;
808fe1d4758SKees Cook 
809fe1d4758SKees Cook 	if (!compress)
810fe1d4758SKees Cook 		return;
811fe1d4758SKees Cook 
812fe1d4758SKees Cook 	for (step = zbackends; step->name; step++) {
813fe1d4758SKees Cook 		if (!strcmp(compress, step->name)) {
814fe1d4758SKees Cook 			zbackend = step;
815fe1d4758SKees Cook 			return;
816fe1d4758SKees Cook 		}
817fe1d4758SKees Cook 	}
818fe1d4758SKees Cook }
819fe1d4758SKees Cook 
820cb095afdSKees Cook static int __init pstore_init(void)
821cb095afdSKees Cook {
822cb095afdSKees Cook 	int ret;
823cb095afdSKees Cook 
824cb095afdSKees Cook 	pstore_choose_compression();
825cb095afdSKees Cook 
82641603165SJoel Fernandes (Google) 	/*
82741603165SJoel Fernandes (Google) 	 * Check if any pstore backends registered earlier but did not
82841603165SJoel Fernandes (Google) 	 * initialize compression because crypto was not ready. If so,
82941603165SJoel Fernandes (Google) 	 * initialize compression now.
83041603165SJoel Fernandes (Google) 	 */
83141603165SJoel Fernandes (Google) 	allocate_buf_for_compression();
83241603165SJoel Fernandes (Google) 
833cb095afdSKees Cook 	ret = pstore_init_fs();
834cb095afdSKees Cook 	if (ret)
835cb095afdSKees Cook 		return ret;
836cb095afdSKees Cook 
837cb095afdSKees Cook 	return 0;
838cb095afdSKees Cook }
83941603165SJoel Fernandes (Google) late_initcall(pstore_init);
840cb095afdSKees Cook 
841cb095afdSKees Cook static void __exit pstore_exit(void)
842cb095afdSKees Cook {
843cb095afdSKees Cook 	pstore_exit_fs();
844cb095afdSKees Cook }
845cb095afdSKees Cook module_exit(pstore_exit)
846cb095afdSKees Cook 
847fe1d4758SKees Cook module_param(compress, charp, 0444);
848fe1d4758SKees Cook MODULE_PARM_DESC(compress, "Pstore compression to use");
849fe1d4758SKees Cook 
850dee28e72SMatthew Garrett module_param(backend, charp, 0444);
851dee28e72SMatthew Garrett MODULE_PARM_DESC(backend, "Pstore backend to use");
852cb095afdSKees Cook 
853cb095afdSKees Cook MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
854cb095afdSKees Cook MODULE_LICENSE("GPL");
855