xref: /openbmc/linux/fs/pstore/platform.c (revision 3524e688b8ee50b0edc76f0e020727eb6c684dbc)
145051539SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2ca01d6ddSTony Luck /*
3ca01d6ddSTony Luck  * Persistent Storage - platform driver interface parts.
4ca01d6ddSTony Luck  *
5f29e5956SAnton Vorontsov  * Copyright (C) 2007-2008 Google, Inc.
6ca01d6ddSTony Luck  * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com>
7ca01d6ddSTony Luck  */
8ca01d6ddSTony Luck 
9ef748853SFabian Frederick #define pr_fmt(fmt) "pstore: " fmt
10ef748853SFabian Frederick 
11ca01d6ddSTony Luck #include <linux/atomic.h>
12ca01d6ddSTony Luck #include <linux/types.h>
13ca01d6ddSTony Luck #include <linux/errno.h>
14ca01d6ddSTony Luck #include <linux/init.h>
15ca01d6ddSTony Luck #include <linux/kmsg_dump.h>
16f29e5956SAnton Vorontsov #include <linux/console.h>
17ca01d6ddSTony Luck #include <linux/module.h>
18ca01d6ddSTony Luck #include <linux/pstore.h>
1958eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
208cfc8ddcSGeliang Tang #include <linux/lzo.h>
218cfc8ddcSGeliang Tang #endif
2258eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
238cfc8ddcSGeliang Tang #include <linux/lz4.h>
248cfc8ddcSGeliang Tang #endif
251021bcf4SGeliang Tang #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
261021bcf4SGeliang Tang #include <linux/zstd.h>
271021bcf4SGeliang Tang #endif
28cb3bee03SGeliang Tang #include <linux/crypto.h>
29ca01d6ddSTony Luck #include <linux/string.h>
306dda9266SLuck, Tony #include <linux/timer.h>
31ca01d6ddSTony Luck #include <linux/slab.h>
32ca01d6ddSTony Luck #include <linux/uaccess.h>
33a3f5f075SAnton Vorontsov #include <linux/jiffies.h>
346dda9266SLuck, Tony #include <linux/workqueue.h>
35ca01d6ddSTony Luck 
36ca01d6ddSTony Luck #include "internal.h"
37ca01d6ddSTony Luck 
38ca01d6ddSTony Luck /*
396dda9266SLuck, Tony  * We defer making "oops" entries appear in pstore - see
406dda9266SLuck, Tony  * whether the system is actually still running well enough
416dda9266SLuck, Tony  * to let someone see the entry
426dda9266SLuck, Tony  */
43521f7288SAnton Vorontsov static int pstore_update_ms = -1;
44a3f5f075SAnton Vorontsov module_param_named(update_ms, pstore_update_ms, int, 0600);
45a3f5f075SAnton Vorontsov MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content "
46521f7288SAnton Vorontsov 		 "(default is -1, which means runtime updates are disabled; "
4778c83c82SKees Cook 		 "enabling this option may not be safe; it may lead to further "
48521f7288SAnton Vorontsov 		 "corruption on Oopses)");
496dda9266SLuck, Tony 
50f0f23e54SJoel Fernandes (Google) /* Names should be in the same order as the enum pstore_type_id */
51f0f23e54SJoel Fernandes (Google) static const char * const pstore_type_names[] = {
52f0f23e54SJoel Fernandes (Google) 	"dmesg",
53f0f23e54SJoel Fernandes (Google) 	"mce",
54f0f23e54SJoel Fernandes (Google) 	"console",
55f0f23e54SJoel Fernandes (Google) 	"ftrace",
56f0f23e54SJoel Fernandes (Google) 	"rtas",
57f0f23e54SJoel Fernandes (Google) 	"powerpc-ofw",
58f0f23e54SJoel Fernandes (Google) 	"powerpc-common",
59f0f23e54SJoel Fernandes (Google) 	"pmsg",
60f0f23e54SJoel Fernandes (Google) 	"powerpc-opal",
61f0f23e54SJoel Fernandes (Google) };
62f0f23e54SJoel Fernandes (Google) 
636dda9266SLuck, Tony static int pstore_new_entry;
646dda9266SLuck, Tony 
6524ed960aSKees Cook static void pstore_timefunc(struct timer_list *);
661d27e3e2SKees Cook static DEFINE_TIMER(pstore_timer, pstore_timefunc);
676dda9266SLuck, Tony 
686dda9266SLuck, Tony static void pstore_dowork(struct work_struct *);
696dda9266SLuck, Tony static DECLARE_WORK(pstore_work, pstore_dowork);
706dda9266SLuck, Tony 
716dda9266SLuck, Tony /*
726248a066SKees Cook  * psinfo_lock protects "psinfo" during calls to
736248a066SKees Cook  * pstore_register(), pstore_unregister(), and
746248a066SKees Cook  * the filesystem mount/unmount routines.
75ca01d6ddSTony Luck  */
76cab12fd0SKees Cook static DEFINE_MUTEX(psinfo_lock);
77060287b8SAnton Vorontsov struct pstore_info *psinfo;
78ca01d6ddSTony Luck 
79dee28e72SMatthew Garrett static char *backend;
80d973f7d8SKees Cook module_param(backend, charp, 0444);
81d973f7d8SKees Cook MODULE_PARM_DESC(backend, "specific backend to use");
82d973f7d8SKees Cook 
83fe1d4758SKees Cook static char *compress =
84fe1d4758SKees Cook #ifdef CONFIG_PSTORE_COMPRESS_DEFAULT
85fe1d4758SKees Cook 		CONFIG_PSTORE_COMPRESS_DEFAULT;
86fe1d4758SKees Cook #else
87fe1d4758SKees Cook 		NULL;
88fe1d4758SKees Cook #endif
89d973f7d8SKees Cook module_param(compress, charp, 0444);
90d973f7d8SKees Cook MODULE_PARM_DESC(compress, "compression to use");
91dee28e72SMatthew Garrett 
92b0aad7a9SAruna Balakrishnaiah /* Compression parameters */
93cb3bee03SGeliang Tang static struct crypto_comp *tfm;
948cfc8ddcSGeliang Tang 
958cfc8ddcSGeliang Tang struct pstore_zbackend {
96cb3bee03SGeliang Tang 	int (*zbufsize)(size_t size);
978cfc8ddcSGeliang Tang 	const char *name;
988cfc8ddcSGeliang Tang };
99b0aad7a9SAruna Balakrishnaiah 
100b0aad7a9SAruna Balakrishnaiah static char *big_oops_buf;
101b0aad7a9SAruna Balakrishnaiah static size_t big_oops_buf_sz;
102b0aad7a9SAruna Balakrishnaiah 
103366f7e7aSLuck, Tony /* How much of the console log to snapshot */
104349d7438SDavid Howells unsigned long kmsg_bytes = PSTORE_DEFAULT_KMSG_BYTES;
105ca01d6ddSTony Luck 
106366f7e7aSLuck, Tony void pstore_set_kmsg_bytes(int bytes)
107ca01d6ddSTony Luck {
108366f7e7aSLuck, Tony 	kmsg_bytes = bytes;
109ca01d6ddSTony Luck }
110ca01d6ddSTony Luck 
111ca01d6ddSTony Luck /* Tag each group of saved records with a sequence number */
112ca01d6ddSTony Luck static int	oopscount;
113ca01d6ddSTony Luck 
114f0f23e54SJoel Fernandes (Google) const char *pstore_type_to_name(enum pstore_type_id type)
115f0f23e54SJoel Fernandes (Google) {
116f0f23e54SJoel Fernandes (Google) 	BUILD_BUG_ON(ARRAY_SIZE(pstore_type_names) != PSTORE_TYPE_MAX);
117f0f23e54SJoel Fernandes (Google) 
118f0f23e54SJoel Fernandes (Google) 	if (WARN_ON_ONCE(type >= PSTORE_TYPE_MAX))
119f0f23e54SJoel Fernandes (Google) 		return "unknown";
120f0f23e54SJoel Fernandes (Google) 
121f0f23e54SJoel Fernandes (Google) 	return pstore_type_names[type];
122f0f23e54SJoel Fernandes (Google) }
123f0f23e54SJoel Fernandes (Google) EXPORT_SYMBOL_GPL(pstore_type_to_name);
124f0f23e54SJoel Fernandes (Google) 
125f0f23e54SJoel Fernandes (Google) enum pstore_type_id pstore_name_to_type(const char *name)
126f0f23e54SJoel Fernandes (Google) {
127f0f23e54SJoel Fernandes (Google) 	int i;
128f0f23e54SJoel Fernandes (Google) 
129f0f23e54SJoel Fernandes (Google) 	for (i = 0; i < PSTORE_TYPE_MAX; i++) {
130f0f23e54SJoel Fernandes (Google) 		if (!strcmp(pstore_type_names[i], name))
131f0f23e54SJoel Fernandes (Google) 			return i;
132f0f23e54SJoel Fernandes (Google) 	}
133f0f23e54SJoel Fernandes (Google) 
134f0f23e54SJoel Fernandes (Google) 	return PSTORE_TYPE_MAX;
135f0f23e54SJoel Fernandes (Google) }
136f0f23e54SJoel Fernandes (Google) EXPORT_SYMBOL_GPL(pstore_name_to_type);
137f0f23e54SJoel Fernandes (Google) 
13878c83c82SKees Cook static void pstore_timer_kick(void)
13978c83c82SKees Cook {
14078c83c82SKees Cook 	if (pstore_update_ms < 0)
14178c83c82SKees Cook 		return;
14278c83c82SKees Cook 
14378c83c82SKees Cook 	mod_timer(&pstore_timer, jiffies + msecs_to_jiffies(pstore_update_ms));
14478c83c82SKees Cook }
14578c83c82SKees Cook 
1469f244e9cSSeiji Aguchi /*
147ea84b580SKees Cook  * Should pstore_dump() wait for a concurrent pstore_dump()? If
148ea84b580SKees Cook  * not, the current pstore_dump() will report a failure to dump
149ea84b580SKees Cook  * and return.
1509f244e9cSSeiji Aguchi  */
151ea84b580SKees Cook static bool pstore_cannot_wait(enum kmsg_dump_reason reason)
152ea84b580SKees Cook {
153ea84b580SKees Cook 	/* In NMI path, pstore shouldn't block regardless of reason. */
1549f244e9cSSeiji Aguchi 	if (in_nmi())
1559f244e9cSSeiji Aguchi 		return true;
1569f244e9cSSeiji Aguchi 
1579f244e9cSSeiji Aguchi 	switch (reason) {
1589f244e9cSSeiji Aguchi 	/* In panic case, other cpus are stopped by smp_send_stop(). */
1599f244e9cSSeiji Aguchi 	case KMSG_DUMP_PANIC:
160ea84b580SKees Cook 	/* Emergency restart shouldn't be blocked. */
1619f244e9cSSeiji Aguchi 	case KMSG_DUMP_EMERG:
1629f244e9cSSeiji Aguchi 		return true;
1639f244e9cSSeiji Aguchi 	default:
1649f244e9cSSeiji Aguchi 		return false;
1659f244e9cSSeiji Aguchi 	}
1669f244e9cSSeiji Aguchi }
1679f244e9cSSeiji Aguchi 
16858eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
169cb3bee03SGeliang Tang static int zbufsize_deflate(size_t size)
170b0aad7a9SAruna Balakrishnaiah {
1717de8fe2fSAruna Balakrishnaiah 	size_t cmpr;
172b0aad7a9SAruna Balakrishnaiah 
173cb3bee03SGeliang Tang 	switch (size) {
1747de8fe2fSAruna Balakrishnaiah 	/* buffer range for efivars */
1757de8fe2fSAruna Balakrishnaiah 	case 1000 ... 2000:
1767de8fe2fSAruna Balakrishnaiah 		cmpr = 56;
1777de8fe2fSAruna Balakrishnaiah 		break;
1787de8fe2fSAruna Balakrishnaiah 	case 2001 ... 3000:
1797de8fe2fSAruna Balakrishnaiah 		cmpr = 54;
1807de8fe2fSAruna Balakrishnaiah 		break;
1817de8fe2fSAruna Balakrishnaiah 	case 3001 ... 3999:
1827de8fe2fSAruna Balakrishnaiah 		cmpr = 52;
1837de8fe2fSAruna Balakrishnaiah 		break;
1847de8fe2fSAruna Balakrishnaiah 	/* buffer range for nvram, erst */
1857de8fe2fSAruna Balakrishnaiah 	case 4000 ... 10000:
1867de8fe2fSAruna Balakrishnaiah 		cmpr = 45;
1877de8fe2fSAruna Balakrishnaiah 		break;
1887de8fe2fSAruna Balakrishnaiah 	default:
1897de8fe2fSAruna Balakrishnaiah 		cmpr = 60;
1907de8fe2fSAruna Balakrishnaiah 		break;
1917de8fe2fSAruna Balakrishnaiah 	}
1927de8fe2fSAruna Balakrishnaiah 
193cb3bee03SGeliang Tang 	return (size * 100) / cmpr;
1948cfc8ddcSGeliang Tang }
1958cfc8ddcSGeliang Tang #endif
1968cfc8ddcSGeliang Tang 
19758eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
198cb3bee03SGeliang Tang static int zbufsize_lzo(size_t size)
1998cfc8ddcSGeliang Tang {
200cb3bee03SGeliang Tang 	return lzo1x_worst_compress(size);
2018cfc8ddcSGeliang Tang }
2028cfc8ddcSGeliang Tang #endif
2038cfc8ddcSGeliang Tang 
20458eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
205cb3bee03SGeliang Tang static int zbufsize_lz4(size_t size)
2068cfc8ddcSGeliang Tang {
207cb3bee03SGeliang Tang 	return LZ4_compressBound(size);
208239b7161SGeliang Tang }
209239b7161SGeliang Tang #endif
210239b7161SGeliang Tang 
21158eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
212cb3bee03SGeliang Tang static int zbufsize_842(size_t size)
213239b7161SGeliang Tang {
21455597406SKees Cook 	return size;
215239b7161SGeliang Tang }
216fe1d4758SKees Cook #endif
2178cfc8ddcSGeliang Tang 
2181021bcf4SGeliang Tang #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
2191021bcf4SGeliang Tang static int zbufsize_zstd(size_t size)
2201021bcf4SGeliang Tang {
2211021bcf4SGeliang Tang 	return ZSTD_compressBound(size);
2221021bcf4SGeliang Tang }
2231021bcf4SGeliang Tang #endif
2241021bcf4SGeliang Tang 
225fe1d4758SKees Cook static const struct pstore_zbackend *zbackend __ro_after_init;
226fe1d4758SKees Cook 
227fe1d4758SKees Cook static const struct pstore_zbackend zbackends[] = {
22858eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
229fe1d4758SKees Cook 	{
230cb3bee03SGeliang Tang 		.zbufsize	= zbufsize_deflate,
231cb3bee03SGeliang Tang 		.name		= "deflate",
232fe1d4758SKees Cook 	},
233fe1d4758SKees Cook #endif
23458eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
235fe1d4758SKees Cook 	{
236cb3bee03SGeliang Tang 		.zbufsize	= zbufsize_lzo,
237fe1d4758SKees Cook 		.name		= "lzo",
238fe1d4758SKees Cook 	},
239fe1d4758SKees Cook #endif
24058eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS)
241fe1d4758SKees Cook 	{
242cb3bee03SGeliang Tang 		.zbufsize	= zbufsize_lz4,
243fe1d4758SKees Cook 		.name		= "lz4",
244fe1d4758SKees Cook 	},
245fe1d4758SKees Cook #endif
24658eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
247fe1d4758SKees Cook 	{
248cb3bee03SGeliang Tang 		.zbufsize	= zbufsize_lz4,
249fe1d4758SKees Cook 		.name		= "lz4hc",
250fe1d4758SKees Cook 	},
251fe1d4758SKees Cook #endif
25258eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
253fe1d4758SKees Cook 	{
254cb3bee03SGeliang Tang 		.zbufsize	= zbufsize_842,
255239b7161SGeliang Tang 		.name		= "842",
256fe1d4758SKees Cook 	},
257fe1d4758SKees Cook #endif
2581021bcf4SGeliang Tang #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
2591021bcf4SGeliang Tang 	{
2601021bcf4SGeliang Tang 		.zbufsize	= zbufsize_zstd,
2611021bcf4SGeliang Tang 		.name		= "zstd",
2621021bcf4SGeliang Tang 	},
2631021bcf4SGeliang Tang #endif
264fe1d4758SKees Cook 	{ }
2658cfc8ddcSGeliang Tang };
2668cfc8ddcSGeliang Tang 
2678cfc8ddcSGeliang Tang static int pstore_compress(const void *in, void *out,
268cb3bee03SGeliang Tang 			   unsigned int inlen, unsigned int outlen)
2698cfc8ddcSGeliang Tang {
270cb3bee03SGeliang Tang 	int ret;
271cb3bee03SGeliang Tang 
272cb3bee03SGeliang Tang 	ret = crypto_comp_compress(tfm, in, inlen, out, &outlen);
273cb3bee03SGeliang Tang 	if (ret) {
274cb3bee03SGeliang Tang 		pr_err("crypto_comp_compress failed, ret = %d!\n", ret);
275cb3bee03SGeliang Tang 		return ret;
2768cfc8ddcSGeliang Tang 	}
2778cfc8ddcSGeliang Tang 
278cb3bee03SGeliang Tang 	return outlen;
279cb3bee03SGeliang Tang }
280cb3bee03SGeliang Tang 
2818cfc8ddcSGeliang Tang static void allocate_buf_for_compression(void)
2828cfc8ddcSGeliang Tang {
28395047b05SKees Cook 	struct crypto_comp *ctx;
28495047b05SKees Cook 	int size;
28595047b05SKees Cook 	char *buf;
28695047b05SKees Cook 
28795047b05SKees Cook 	/* Skip if not built-in or compression backend not selected yet. */
288e698aaf3STobias Regnery 	if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !zbackend)
289cb3bee03SGeliang Tang 		return;
290cb3bee03SGeliang Tang 
29195047b05SKees Cook 	/* Skip if no pstore backend yet or compression init already done. */
29295047b05SKees Cook 	if (!psinfo || tfm)
29395047b05SKees Cook 		return;
29495047b05SKees Cook 
295cb3bee03SGeliang Tang 	if (!crypto_has_comp(zbackend->name, 0, 0)) {
29695047b05SKees Cook 		pr_err("Unknown compression: %s\n", zbackend->name);
297cb3bee03SGeliang Tang 		return;
298cb3bee03SGeliang Tang 	}
299cb3bee03SGeliang Tang 
30095047b05SKees Cook 	size = zbackend->zbufsize(psinfo->bufsize);
30195047b05SKees Cook 	if (size <= 0) {
30295047b05SKees Cook 		pr_err("Invalid compression size for %s: %d\n",
30395047b05SKees Cook 		       zbackend->name, size);
304cb3bee03SGeliang Tang 		return;
305cb3bee03SGeliang Tang 	}
306cb3bee03SGeliang Tang 
30795047b05SKees Cook 	buf = kmalloc(size, GFP_KERNEL);
30895047b05SKees Cook 	if (!buf) {
30995047b05SKees Cook 		pr_err("Failed %d byte compression buffer allocation for: %s\n",
31095047b05SKees Cook 		       size, zbackend->name);
311cb3bee03SGeliang Tang 		return;
3128cfc8ddcSGeliang Tang 	}
31395047b05SKees Cook 
31495047b05SKees Cook 	ctx = crypto_alloc_comp(zbackend->name, 0, 0);
31595047b05SKees Cook 	if (IS_ERR_OR_NULL(ctx)) {
31695047b05SKees Cook 		kfree(buf);
31795047b05SKees Cook 		pr_err("crypto_alloc_comp('%s') failed: %ld\n", zbackend->name,
31895047b05SKees Cook 		       PTR_ERR(ctx));
31995047b05SKees Cook 		return;
32095047b05SKees Cook 	}
32195047b05SKees Cook 
32295047b05SKees Cook 	/* A non-NULL big_oops_buf indicates compression is available. */
32395047b05SKees Cook 	tfm = ctx;
32495047b05SKees Cook 	big_oops_buf_sz = size;
32595047b05SKees Cook 	big_oops_buf = buf;
32695047b05SKees Cook 
3270eed84ffSKees Cook 	pr_info("Using crash dump compression: %s\n", zbackend->name);
3288cfc8ddcSGeliang Tang }
3298cfc8ddcSGeliang Tang 
3308cfc8ddcSGeliang Tang static void free_buf_for_compression(void)
3318cfc8ddcSGeliang Tang {
332a9fb94a9SPi-Hsun Shih 	if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && tfm) {
333cb3bee03SGeliang Tang 		crypto_free_comp(tfm);
334a9fb94a9SPi-Hsun Shih 		tfm = NULL;
335a9fb94a9SPi-Hsun Shih 	}
336cb3bee03SGeliang Tang 	kfree(big_oops_buf);
337cb3bee03SGeliang Tang 	big_oops_buf = NULL;
338cb3bee03SGeliang Tang 	big_oops_buf_sz = 0;
339ee1d2674SGeliang Tang }
340ee1d2674SGeliang Tang 
341b0aad7a9SAruna Balakrishnaiah /*
342b0aad7a9SAruna Balakrishnaiah  * Called when compression fails, since the printk buffer
343b0aad7a9SAruna Balakrishnaiah  * would be fetched for compression calling it again when
344b0aad7a9SAruna Balakrishnaiah  * compression fails would have moved the iterator of
345b0aad7a9SAruna Balakrishnaiah  * printk buffer which results in fetching old contents.
346b0aad7a9SAruna Balakrishnaiah  * Copy the recent messages from big_oops_buf to psinfo->buf
347b0aad7a9SAruna Balakrishnaiah  */
348b0aad7a9SAruna Balakrishnaiah static size_t copy_kmsg_to_buffer(int hsize, size_t len)
349b0aad7a9SAruna Balakrishnaiah {
350b0aad7a9SAruna Balakrishnaiah 	size_t total_len;
351b0aad7a9SAruna Balakrishnaiah 	size_t diff;
352b0aad7a9SAruna Balakrishnaiah 
353b0aad7a9SAruna Balakrishnaiah 	total_len = hsize + len;
354b0aad7a9SAruna Balakrishnaiah 
355b0aad7a9SAruna Balakrishnaiah 	if (total_len > psinfo->bufsize) {
356b0aad7a9SAruna Balakrishnaiah 		diff = total_len - psinfo->bufsize + hsize;
357b0aad7a9SAruna Balakrishnaiah 		memcpy(psinfo->buf, big_oops_buf, hsize);
358b0aad7a9SAruna Balakrishnaiah 		memcpy(psinfo->buf + hsize, big_oops_buf + diff,
359b0aad7a9SAruna Balakrishnaiah 					psinfo->bufsize - hsize);
360b0aad7a9SAruna Balakrishnaiah 		total_len = psinfo->bufsize;
361b0aad7a9SAruna Balakrishnaiah 	} else
362b0aad7a9SAruna Balakrishnaiah 		memcpy(psinfo->buf, big_oops_buf, total_len);
363b0aad7a9SAruna Balakrishnaiah 
364b0aad7a9SAruna Balakrishnaiah 	return total_len;
365b0aad7a9SAruna Balakrishnaiah }
366b0aad7a9SAruna Balakrishnaiah 
367e581ca81SKees Cook void pstore_record_init(struct pstore_record *record,
368e581ca81SKees Cook 			struct pstore_info *psinfo)
369e581ca81SKees Cook {
370e581ca81SKees Cook 	memset(record, 0, sizeof(*record));
371e581ca81SKees Cook 
372e581ca81SKees Cook 	record->psi = psinfo;
373c7f3c595SKees Cook 
374c7f3c595SKees Cook 	/* Report zeroed timestamp if called before timekeeping has resumed. */
3757aaa822eSKees Cook 	record->time = ns_to_timespec64(ktime_get_real_fast_ns());
376e581ca81SKees Cook }
377e581ca81SKees Cook 
378ca01d6ddSTony Luck /*
3790eed84ffSKees Cook  * callback from kmsg_dump. Save as much as we can (up to kmsg_bytes) from the
3800eed84ffSKees Cook  * end of the buffer.
381ca01d6ddSTony Luck  */
382ca01d6ddSTony Luck static void pstore_dump(struct kmsg_dumper *dumper,
383e2ae715dSKay Sievers 			enum kmsg_dump_reason reason)
384ca01d6ddSTony Luck {
385e2ae715dSKay Sievers 	unsigned long	total = 0;
386381b872cSSeiji Aguchi 	const char	*why;
387b94fdd07SMatthew Garrett 	unsigned int	part = 1;
388e2ae715dSKay Sievers 	int		ret;
389ca01d6ddSTony Luck 
390fb13cb8aSKees Cook 	why = kmsg_dump_reason_str(reason);
3919f6af27fSTony Luck 
392ea84b580SKees Cook 	if (down_trylock(&psinfo->buf_lock)) {
393ea84b580SKees Cook 		/* Failed to acquire lock: give up if we cannot wait. */
394ea84b580SKees Cook 		if (pstore_cannot_wait(reason)) {
395ea84b580SKees Cook 			pr_err("dump skipped in %s path: may corrupt error record\n",
396ea84b580SKees Cook 				in_nmi() ? "NMI" : why);
397959217c8SLi Pengcheng 			return;
3989f244e9cSSeiji Aguchi 		}
399ea84b580SKees Cook 		if (down_interruptible(&psinfo->buf_lock)) {
400ea84b580SKees Cook 			pr_err("could not grab semaphore?!\n");
401ea84b580SKees Cook 			return;
40298e44fdaSNamhyung Kim 		}
403ea84b580SKees Cook 	}
404ea84b580SKees Cook 
405ca01d6ddSTony Luck 	oopscount++;
406ca01d6ddSTony Luck 	while (total < kmsg_bytes) {
407e2ae715dSKay Sievers 		char *dst;
40876cc9580SKees Cook 		size_t dst_size;
40976cc9580SKees Cook 		int header_size;
410b0aad7a9SAruna Balakrishnaiah 		int zipped_len = -1;
41176cc9580SKees Cook 		size_t dump_size;
412e581ca81SKees Cook 		struct pstore_record record;
413e581ca81SKees Cook 
414e581ca81SKees Cook 		pstore_record_init(&record, psinfo);
415e581ca81SKees Cook 		record.type = PSTORE_TYPE_DMESG;
416e581ca81SKees Cook 		record.count = oopscount;
417e581ca81SKees Cook 		record.reason = reason;
418e581ca81SKees Cook 		record.part = part;
419e581ca81SKees Cook 		record.buf = psinfo->buf;
420e2ae715dSKay Sievers 
421ea84b580SKees Cook 		if (big_oops_buf) {
422b0aad7a9SAruna Balakrishnaiah 			dst = big_oops_buf;
42376cc9580SKees Cook 			dst_size = big_oops_buf_sz;
424235f6d15SNamhyung Kim 		} else {
425235f6d15SNamhyung Kim 			dst = psinfo->buf;
42676cc9580SKees Cook 			dst_size = psinfo->bufsize;
427235f6d15SNamhyung Kim 		}
428235f6d15SNamhyung Kim 
42976cc9580SKees Cook 		/* Write dump header. */
43076cc9580SKees Cook 		header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why,
43176cc9580SKees Cook 				 oopscount, part);
43276cc9580SKees Cook 		dst_size -= header_size;
433b0aad7a9SAruna Balakrishnaiah 
43476cc9580SKees Cook 		/* Write dump contents. */
43576cc9580SKees Cook 		if (!kmsg_dump_get_buffer(dumper, true, dst + header_size,
43676cc9580SKees Cook 					  dst_size, &dump_size))
437b0aad7a9SAruna Balakrishnaiah 			break;
438b0aad7a9SAruna Balakrishnaiah 
439ea84b580SKees Cook 		if (big_oops_buf) {
440b0aad7a9SAruna Balakrishnaiah 			zipped_len = pstore_compress(dst, psinfo->buf,
44176cc9580SKees Cook 						header_size + dump_size,
44276cc9580SKees Cook 						psinfo->bufsize);
443b0aad7a9SAruna Balakrishnaiah 
444b0aad7a9SAruna Balakrishnaiah 			if (zipped_len > 0) {
44576cc9580SKees Cook 				record.compressed = true;
44676cc9580SKees Cook 				record.size = zipped_len;
447b0aad7a9SAruna Balakrishnaiah 			} else {
44876cc9580SKees Cook 				record.size = copy_kmsg_to_buffer(header_size,
44976cc9580SKees Cook 								  dump_size);
450b0aad7a9SAruna Balakrishnaiah 			}
451b0aad7a9SAruna Balakrishnaiah 		} else {
45276cc9580SKees Cook 			record.size = header_size + dump_size;
453b0aad7a9SAruna Balakrishnaiah 		}
454b0aad7a9SAruna Balakrishnaiah 
45576cc9580SKees Cook 		ret = psinfo->write(&record);
45678c83c82SKees Cook 		if (ret == 0 && reason == KMSG_DUMP_OOPS) {
4576dda9266SLuck, Tony 			pstore_new_entry = 1;
45878c83c82SKees Cook 			pstore_timer_kick();
45978c83c82SKees Cook 		}
460e2ae715dSKay Sievers 
46176cc9580SKees Cook 		total += record.size;
46256280682SMatthew Garrett 		part++;
463ca01d6ddSTony Luck 	}
464ea84b580SKees Cook 
465ea84b580SKees Cook 	up(&psinfo->buf_lock);
466ca01d6ddSTony Luck }
467ca01d6ddSTony Luck 
468ca01d6ddSTony Luck static struct kmsg_dumper pstore_dumper = {
469ca01d6ddSTony Luck 	.dump = pstore_dump,
470ca01d6ddSTony Luck };
471ca01d6ddSTony Luck 
472306e5c2aSGeliang Tang /*
473306e5c2aSGeliang Tang  * Register with kmsg_dump to save last part of console log on panic.
474306e5c2aSGeliang Tang  */
47518730411SGeliang Tang static void pstore_register_kmsg(void)
47618730411SGeliang Tang {
47718730411SGeliang Tang 	kmsg_dump_register(&pstore_dumper);
47818730411SGeliang Tang }
47918730411SGeliang Tang 
480ee1d2674SGeliang Tang static void pstore_unregister_kmsg(void)
481ee1d2674SGeliang Tang {
482ee1d2674SGeliang Tang 	kmsg_dump_unregister(&pstore_dumper);
483ee1d2674SGeliang Tang }
484ee1d2674SGeliang Tang 
485f29e5956SAnton Vorontsov #ifdef CONFIG_PSTORE_CONSOLE
486f29e5956SAnton Vorontsov static void pstore_console_write(struct console *con, const char *s, unsigned c)
487f29e5956SAnton Vorontsov {
488e581ca81SKees Cook 	struct pstore_record record;
489f29e5956SAnton Vorontsov 
4904c6c4d34SYue Hu 	if (!c)
4914c6c4d34SYue Hu 		return;
4924c6c4d34SYue Hu 
493e581ca81SKees Cook 	pstore_record_init(&record, psinfo);
494e581ca81SKees Cook 	record.type = PSTORE_TYPE_CONSOLE;
495e581ca81SKees Cook 
496b10b4711SKees Cook 	record.buf = (char *)s;
497b10b4711SKees Cook 	record.size = c;
4984c9ec219SKees Cook 	psinfo->write(&record);
499f29e5956SAnton Vorontsov }
500f29e5956SAnton Vorontsov 
501f29e5956SAnton Vorontsov static struct console pstore_console = {
502f29e5956SAnton Vorontsov 	.write	= pstore_console_write,
503f29e5956SAnton Vorontsov 	.index	= -1,
504f29e5956SAnton Vorontsov };
505f29e5956SAnton Vorontsov 
506f29e5956SAnton Vorontsov static void pstore_register_console(void)
507f29e5956SAnton Vorontsov {
508d195c390SKees Cook 	/* Show which backend is going to get console writes. */
509d195c390SKees Cook 	strscpy(pstore_console.name, psinfo->name,
510d195c390SKees Cook 		sizeof(pstore_console.name));
511b7753fc7SKees Cook 	/*
512b7753fc7SKees Cook 	 * Always initialize flags here since prior unregister_console()
513b7753fc7SKees Cook 	 * calls may have changed settings (specifically CON_ENABLED).
514b7753fc7SKees Cook 	 */
515b7753fc7SKees Cook 	pstore_console.flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME;
516f29e5956SAnton Vorontsov 	register_console(&pstore_console);
517f29e5956SAnton Vorontsov }
518ee1d2674SGeliang Tang 
519ee1d2674SGeliang Tang static void pstore_unregister_console(void)
520ee1d2674SGeliang Tang {
521ee1d2674SGeliang Tang 	unregister_console(&pstore_console);
522ee1d2674SGeliang Tang }
523f29e5956SAnton Vorontsov #else
524f29e5956SAnton Vorontsov static void pstore_register_console(void) {}
525ee1d2674SGeliang Tang static void pstore_unregister_console(void) {}
526f29e5956SAnton Vorontsov #endif
527f29e5956SAnton Vorontsov 
5284c9ec219SKees Cook static int pstore_write_user_compat(struct pstore_record *record,
529fdd03118SKees Cook 				    const char __user *buf)
5305bf6d1b9SMark Salyzyn {
53130800d99SKees Cook 	int ret = 0;
5325bf6d1b9SMark Salyzyn 
53330800d99SKees Cook 	if (record->buf)
53430800d99SKees Cook 		return -EINVAL;
5355bf6d1b9SMark Salyzyn 
536077090afSGeliang Tang 	record->buf = memdup_user(buf, record->size);
537dfd6fa39SHirofumi Nakagawa 	if (IS_ERR(record->buf)) {
538077090afSGeliang Tang 		ret = PTR_ERR(record->buf);
53930800d99SKees Cook 		goto out;
5405bf6d1b9SMark Salyzyn 	}
54130800d99SKees Cook 
5424c9ec219SKees Cook 	ret = record->psi->write(record);
54330800d99SKees Cook 
54430800d99SKees Cook 	kfree(record->buf);
545077090afSGeliang Tang out:
54630800d99SKees Cook 	record->buf = NULL;
54730800d99SKees Cook 
54830800d99SKees Cook 	return unlikely(ret < 0) ? ret : record->size;
5495bf6d1b9SMark Salyzyn }
5505bf6d1b9SMark Salyzyn 
551ca01d6ddSTony Luck /*
552ca01d6ddSTony Luck  * platform specific persistent storage driver registers with
553ca01d6ddSTony Luck  * us here. If pstore is already mounted, call the platform
554ca01d6ddSTony Luck  * read function right away to populate the file system. If not
555ca01d6ddSTony Luck  * then the pstore mount code will call us later to fill out
556ca01d6ddSTony Luck  * the file system.
557ca01d6ddSTony Luck  */
558ca01d6ddSTony Luck int pstore_register(struct pstore_info *psi)
559ca01d6ddSTony Luck {
5600d7cd09aSKees Cook 	if (backend && strcmp(backend, psi->name)) {
5610d7cd09aSKees Cook 		pr_warn("ignoring unexpected backend '%s'\n", psi->name);
5628e48b1a8SLenny Szubowicz 		return -EPERM;
5630d7cd09aSKees Cook 	}
5648e48b1a8SLenny Szubowicz 
5654c9ec219SKees Cook 	/* Sanity check flags. */
5664c9ec219SKees Cook 	if (!psi->flags) {
5674c9ec219SKees Cook 		pr_warn("backend '%s' must support at least one frontend\n",
5684c9ec219SKees Cook 			psi->name);
5694c9ec219SKees Cook 		return -EINVAL;
5704c9ec219SKees Cook 	}
5714c9ec219SKees Cook 
5724c9ec219SKees Cook 	/* Check for required functions. */
5734c9ec219SKees Cook 	if (!psi->read || !psi->write) {
5744c9ec219SKees Cook 		pr_warn("backend '%s' must implement read() and write()\n",
5754c9ec219SKees Cook 			psi->name);
5764c9ec219SKees Cook 		return -EINVAL;
5774c9ec219SKees Cook 	}
5784c9ec219SKees Cook 
579cab12fd0SKees Cook 	mutex_lock(&psinfo_lock);
580ca01d6ddSTony Luck 	if (psinfo) {
5810d7cd09aSKees Cook 		pr_warn("backend '%s' already loaded: ignoring '%s'\n",
5820d7cd09aSKees Cook 			psinfo->name, psi->name);
583cab12fd0SKees Cook 		mutex_unlock(&psinfo_lock);
584ca01d6ddSTony Luck 		return -EBUSY;
585ca01d6ddSTony Luck 	}
586dee28e72SMatthew Garrett 
5874c9ec219SKees Cook 	if (!psi->write_user)
5884c9ec219SKees Cook 		psi->write_user = pstore_write_user_compat;
589ca01d6ddSTony Luck 	psinfo = psi;
590f6f82851SKees Cook 	mutex_init(&psinfo->read_mutex);
591ea84b580SKees Cook 	sema_init(&psinfo->buf_lock, 1);
592ca01d6ddSTony Luck 
5938880fa32SKees Cook 	if (psi->flags & PSTORE_FLAGS_DMESG)
594b0aad7a9SAruna Balakrishnaiah 		allocate_buf_for_compression();
595b0aad7a9SAruna Balakrishnaiah 
5966dda9266SLuck, Tony 	pstore_get_records(0);
597ca01d6ddSTony Luck 
598*3524e688SPavel Tatashin 	if (psi->flags & PSTORE_FLAGS_DMESG) {
599*3524e688SPavel Tatashin 		pstore_dumper.max_reason = psinfo->max_reason;
60018730411SGeliang Tang 		pstore_register_kmsg();
601*3524e688SPavel Tatashin 	}
602c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_CONSOLE)
603f29e5956SAnton Vorontsov 		pstore_register_console();
604c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_FTRACE)
60565f8c95eSAnton Vorontsov 		pstore_register_ftrace();
606c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_PMSG)
6079d5438f4SMark Salyzyn 		pstore_register_pmsg();
608ca01d6ddSTony Luck 
6096330d553SKees Cook 	/* Start watching for new records, if desired. */
61078c83c82SKees Cook 	pstore_timer_kick();
6116dda9266SLuck, Tony 
61242222c2aSWang Long 	/*
61342222c2aSWang Long 	 * Update the module parameter backend, so it is visible
61442222c2aSWang Long 	 * through /sys/module/pstore/parameters/backend
61542222c2aSWang Long 	 */
616563ca40dSKees Cook 	backend = kstrdup(psi->name, GFP_KERNEL);
61742222c2aSWang Long 
618ef748853SFabian Frederick 	pr_info("Registered %s as persistent store backend\n", psi->name);
6198e48b1a8SLenny Szubowicz 
6206248a066SKees Cook 	mutex_unlock(&psinfo_lock);
621ca01d6ddSTony Luck 	return 0;
622ca01d6ddSTony Luck }
623ca01d6ddSTony Luck EXPORT_SYMBOL_GPL(pstore_register);
624ca01d6ddSTony Luck 
625ee1d2674SGeliang Tang void pstore_unregister(struct pstore_info *psi)
626ee1d2674SGeliang Tang {
6276248a066SKees Cook 	/* It's okay to unregister nothing. */
6286248a066SKees Cook 	if (!psi)
6296248a066SKees Cook 		return;
6306248a066SKees Cook 
6316248a066SKees Cook 	mutex_lock(&psinfo_lock);
6326248a066SKees Cook 
6336248a066SKees Cook 	/* Only one backend can be registered at a time. */
6346248a066SKees Cook 	if (WARN_ON(psi != psinfo)) {
6356248a066SKees Cook 		mutex_unlock(&psinfo_lock);
6366248a066SKees Cook 		return;
6376248a066SKees Cook 	}
6386248a066SKees Cook 
63978c83c82SKees Cook 	/* Unregister all callbacks. */
640c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_PMSG)
641ee1d2674SGeliang Tang 		pstore_unregister_pmsg();
642c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_FTRACE)
643ee1d2674SGeliang Tang 		pstore_unregister_ftrace();
644c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_CONSOLE)
645ee1d2674SGeliang Tang 		pstore_unregister_console();
646c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_DMESG)
647ee1d2674SGeliang Tang 		pstore_unregister_kmsg();
648ee1d2674SGeliang Tang 
64978c83c82SKees Cook 	/* Stop timer and make sure all work has finished. */
65078c83c82SKees Cook 	del_timer_sync(&pstore_timer);
65178c83c82SKees Cook 	flush_work(&pstore_work);
65278c83c82SKees Cook 
653609e28bbSKees Cook 	/* Remove all backend records from filesystem tree. */
654609e28bbSKees Cook 	pstore_put_backend_records(psi);
655609e28bbSKees Cook 
656ee1d2674SGeliang Tang 	free_buf_for_compression();
657ee1d2674SGeliang Tang 
658ee1d2674SGeliang Tang 	psinfo = NULL;
659563ca40dSKees Cook 	kfree(backend);
660ee1d2674SGeliang Tang 	backend = NULL;
6616248a066SKees Cook 	mutex_unlock(&psinfo_lock);
662ee1d2674SGeliang Tang }
663ee1d2674SGeliang Tang EXPORT_SYMBOL_GPL(pstore_unregister);
664ee1d2674SGeliang Tang 
665634f8f51SKees Cook static void decompress_record(struct pstore_record *record)
666634f8f51SKees Cook {
667bdabc8e7SKees Cook 	int ret;
668634f8f51SKees Cook 	int unzipped_len;
669bdabc8e7SKees Cook 	char *unzipped, *workspace;
670634f8f51SKees Cook 
6714a16d1cbSAnkit Kumar 	if (!record->compressed)
6724a16d1cbSAnkit Kumar 		return;
6734a16d1cbSAnkit Kumar 
674634f8f51SKees Cook 	/* Only PSTORE_TYPE_DMESG support compression. */
6754a16d1cbSAnkit Kumar 	if (record->type != PSTORE_TYPE_DMESG) {
676634f8f51SKees Cook 		pr_warn("ignored compressed record type %d\n", record->type);
677634f8f51SKees Cook 		return;
678634f8f51SKees Cook 	}
679634f8f51SKees Cook 
680bdabc8e7SKees Cook 	/* Missing compression buffer means compression was not initialized. */
681634f8f51SKees Cook 	if (!big_oops_buf) {
682bdabc8e7SKees Cook 		pr_warn("no decompression method initialized!\n");
683634f8f51SKees Cook 		return;
684634f8f51SKees Cook 	}
685634f8f51SKees Cook 
686bdabc8e7SKees Cook 	/* Allocate enough space to hold max decompression and ECC. */
687bdabc8e7SKees Cook 	unzipped_len = big_oops_buf_sz;
688bdabc8e7SKees Cook 	workspace = kmalloc(unzipped_len + record->ecc_notice_size,
6897e8cc8dcSKees Cook 			    GFP_KERNEL);
690bdabc8e7SKees Cook 	if (!workspace)
691bdabc8e7SKees Cook 		return;
692bdabc8e7SKees Cook 
693bdabc8e7SKees Cook 	/* After decompression "unzipped_len" is almost certainly smaller. */
694bdabc8e7SKees Cook 	ret = crypto_comp_decompress(tfm, record->buf, record->size,
695bdabc8e7SKees Cook 					  workspace, &unzipped_len);
696bdabc8e7SKees Cook 	if (ret) {
697bdabc8e7SKees Cook 		pr_err("crypto_comp_decompress failed, ret = %d!\n", ret);
698bdabc8e7SKees Cook 		kfree(workspace);
6997e8cc8dcSKees Cook 		return;
7007e8cc8dcSKees Cook 	}
7017e8cc8dcSKees Cook 
7027e8cc8dcSKees Cook 	/* Append ECC notice to decompressed buffer. */
703bdabc8e7SKees Cook 	memcpy(workspace + unzipped_len, record->buf + record->size,
704634f8f51SKees Cook 	       record->ecc_notice_size);
7057e8cc8dcSKees Cook 
706bdabc8e7SKees Cook 	/* Copy decompressed contents into an minimum-sized allocation. */
707bdabc8e7SKees Cook 	unzipped = kmemdup(workspace, unzipped_len + record->ecc_notice_size,
708bdabc8e7SKees Cook 			   GFP_KERNEL);
709bdabc8e7SKees Cook 	kfree(workspace);
710bdabc8e7SKees Cook 	if (!unzipped)
711bdabc8e7SKees Cook 		return;
712bdabc8e7SKees Cook 
713bdabc8e7SKees Cook 	/* Swap out compressed contents with decompressed contents. */
714634f8f51SKees Cook 	kfree(record->buf);
715bdabc8e7SKees Cook 	record->buf = unzipped;
716634f8f51SKees Cook 	record->size = unzipped_len;
717634f8f51SKees Cook 	record->compressed = false;
718634f8f51SKees Cook }
719634f8f51SKees Cook 
720ca01d6ddSTony Luck /*
7213a7d2fd1SKees Cook  * Read all the records from one persistent store backend. Create
7226dda9266SLuck, Tony  * files in our filesystem.  Don't warn about -EEXIST errors
7236dda9266SLuck, Tony  * when we are re-scanning the backing store looking to add new
7246dda9266SLuck, Tony  * error records.
725ca01d6ddSTony Luck  */
7263a7d2fd1SKees Cook void pstore_get_backend_records(struct pstore_info *psi,
7273a7d2fd1SKees Cook 				struct dentry *root, int quiet)
728ca01d6ddSTony Luck {
7292a2b0acfSKees Cook 	int failed = 0;
730656de42eSKees Cook 	unsigned int stop_loop = 65536;
731ca01d6ddSTony Luck 
7323a7d2fd1SKees Cook 	if (!psi || !root)
733ca01d6ddSTony Luck 		return;
734ca01d6ddSTony Luck 
735f6f82851SKees Cook 	mutex_lock(&psi->read_mutex);
7362174f6dfSKees Cook 	if (psi->open && psi->open(psi))
73706cf91b4SChen Gong 		goto out;
73806cf91b4SChen Gong 
7391dfff7ddSKees Cook 	/*
7401dfff7ddSKees Cook 	 * Backend callback read() allocates record.buf. decompress_record()
7411dfff7ddSKees Cook 	 * may reallocate record.buf. On success, pstore_mkfile() will keep
7421dfff7ddSKees Cook 	 * the record.buf, so free it only on failure.
7431dfff7ddSKees Cook 	 */
744656de42eSKees Cook 	for (; stop_loop; stop_loop--) {
7452a2b0acfSKees Cook 		struct pstore_record *record;
7462a2b0acfSKees Cook 		int rc;
7472a2b0acfSKees Cook 
7482a2b0acfSKees Cook 		record = kzalloc(sizeof(*record), GFP_KERNEL);
7492a2b0acfSKees Cook 		if (!record) {
7502a2b0acfSKees Cook 			pr_err("out of memory creating record\n");
7512a2b0acfSKees Cook 			break;
7522a2b0acfSKees Cook 		}
753e581ca81SKees Cook 		pstore_record_init(record, psi);
7542a2b0acfSKees Cook 
7552a2b0acfSKees Cook 		record->size = psi->read(record);
7562a2b0acfSKees Cook 
7572a2b0acfSKees Cook 		/* No more records left in backend? */
758f6525b96SDouglas Anderson 		if (record->size <= 0) {
759f6525b96SDouglas Anderson 			kfree(record);
7602a2b0acfSKees Cook 			break;
761f6525b96SDouglas Anderson 		}
7622a2b0acfSKees Cook 
7632a2b0acfSKees Cook 		decompress_record(record);
7643a7d2fd1SKees Cook 		rc = pstore_mkfile(root, record);
7651dfff7ddSKees Cook 		if (rc) {
76683f70f07SKees Cook 			/* pstore_mkfile() did not take record, so free it. */
7672a2b0acfSKees Cook 			kfree(record->buf);
76883f70f07SKees Cook 			kfree(record);
7691dfff7ddSKees Cook 			if (rc != -EEXIST || !quiet)
7701dfff7ddSKees Cook 				failed++;
7711dfff7ddSKees Cook 		}
772ca01d6ddSTony Luck 	}
7732174f6dfSKees Cook 	if (psi->close)
77406cf91b4SChen Gong 		psi->close(psi);
77506cf91b4SChen Gong out:
776f6f82851SKees Cook 	mutex_unlock(&psi->read_mutex);
777ca01d6ddSTony Luck 
778ca01d6ddSTony Luck 	if (failed)
779656de42eSKees Cook 		pr_warn("failed to create %d record(s) from '%s'\n",
780ca01d6ddSTony Luck 			failed, psi->name);
781656de42eSKees Cook 	if (!stop_loop)
782656de42eSKees Cook 		pr_err("looping? Too many records seen from '%s'\n",
783656de42eSKees Cook 			psi->name);
784ca01d6ddSTony Luck }
785ca01d6ddSTony Luck 
7866dda9266SLuck, Tony static void pstore_dowork(struct work_struct *work)
7876dda9266SLuck, Tony {
7886dda9266SLuck, Tony 	pstore_get_records(1);
7896dda9266SLuck, Tony }
7906dda9266SLuck, Tony 
79124ed960aSKees Cook static void pstore_timefunc(struct timer_list *unused)
7926dda9266SLuck, Tony {
7936dda9266SLuck, Tony 	if (pstore_new_entry) {
7946dda9266SLuck, Tony 		pstore_new_entry = 0;
7956dda9266SLuck, Tony 		schedule_work(&pstore_work);
7966dda9266SLuck, Tony 	}
7976dda9266SLuck, Tony 
79878c83c82SKees Cook 	pstore_timer_kick();
7996dda9266SLuck, Tony }
8006dda9266SLuck, Tony 
8018d82cee2SBen Dooks (Codethink) static void __init pstore_choose_compression(void)
802fe1d4758SKees Cook {
803fe1d4758SKees Cook 	const struct pstore_zbackend *step;
804fe1d4758SKees Cook 
805fe1d4758SKees Cook 	if (!compress)
806fe1d4758SKees Cook 		return;
807fe1d4758SKees Cook 
808fe1d4758SKees Cook 	for (step = zbackends; step->name; step++) {
809fe1d4758SKees Cook 		if (!strcmp(compress, step->name)) {
810fe1d4758SKees Cook 			zbackend = step;
811fe1d4758SKees Cook 			return;
812fe1d4758SKees Cook 		}
813fe1d4758SKees Cook 	}
814fe1d4758SKees Cook }
815fe1d4758SKees Cook 
816cb095afdSKees Cook static int __init pstore_init(void)
817cb095afdSKees Cook {
818cb095afdSKees Cook 	int ret;
819cb095afdSKees Cook 
820cb095afdSKees Cook 	pstore_choose_compression();
821cb095afdSKees Cook 
82241603165SJoel Fernandes (Google) 	/*
82341603165SJoel Fernandes (Google) 	 * Check if any pstore backends registered earlier but did not
82441603165SJoel Fernandes (Google) 	 * initialize compression because crypto was not ready. If so,
82541603165SJoel Fernandes (Google) 	 * initialize compression now.
82641603165SJoel Fernandes (Google) 	 */
82741603165SJoel Fernandes (Google) 	allocate_buf_for_compression();
82841603165SJoel Fernandes (Google) 
829cb095afdSKees Cook 	ret = pstore_init_fs();
830cb095afdSKees Cook 	if (ret)
8318a57d6d4Schenqiwu 		free_buf_for_compression();
832cb095afdSKees Cook 
8338a57d6d4Schenqiwu 	return ret;
834cb095afdSKees Cook }
83541603165SJoel Fernandes (Google) late_initcall(pstore_init);
836cb095afdSKees Cook 
837cb095afdSKees Cook static void __exit pstore_exit(void)
838cb095afdSKees Cook {
839cb095afdSKees Cook 	pstore_exit_fs();
840cb095afdSKees Cook }
841cb095afdSKees Cook module_exit(pstore_exit)
842cb095afdSKees Cook 
843cb095afdSKees Cook MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
844cb095afdSKees Cook MODULE_LICENSE("GPL");
845