xref: /openbmc/linux/fs/pstore/platform.c (revision d195c39052d1da278a00a6744ce59c383b67b191)
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;
80fe1d4758SKees Cook static char *compress =
81fe1d4758SKees Cook #ifdef CONFIG_PSTORE_COMPRESS_DEFAULT
82fe1d4758SKees Cook 		CONFIG_PSTORE_COMPRESS_DEFAULT;
83fe1d4758SKees Cook #else
84fe1d4758SKees Cook 		NULL;
85fe1d4758SKees Cook #endif
86dee28e72SMatthew Garrett 
87b0aad7a9SAruna Balakrishnaiah /* Compression parameters */
88cb3bee03SGeliang Tang static struct crypto_comp *tfm;
898cfc8ddcSGeliang Tang 
908cfc8ddcSGeliang Tang struct pstore_zbackend {
91cb3bee03SGeliang Tang 	int (*zbufsize)(size_t size);
928cfc8ddcSGeliang Tang 	const char *name;
938cfc8ddcSGeliang Tang };
94b0aad7a9SAruna Balakrishnaiah 
95b0aad7a9SAruna Balakrishnaiah static char *big_oops_buf;
96b0aad7a9SAruna Balakrishnaiah static size_t big_oops_buf_sz;
97b0aad7a9SAruna Balakrishnaiah 
98366f7e7aSLuck, Tony /* How much of the console log to snapshot */
99349d7438SDavid Howells unsigned long kmsg_bytes = PSTORE_DEFAULT_KMSG_BYTES;
100ca01d6ddSTony Luck 
101366f7e7aSLuck, Tony void pstore_set_kmsg_bytes(int bytes)
102ca01d6ddSTony Luck {
103366f7e7aSLuck, Tony 	kmsg_bytes = bytes;
104ca01d6ddSTony Luck }
105ca01d6ddSTony Luck 
106ca01d6ddSTony Luck /* Tag each group of saved records with a sequence number */
107ca01d6ddSTony Luck static int	oopscount;
108ca01d6ddSTony Luck 
109f0f23e54SJoel Fernandes (Google) const char *pstore_type_to_name(enum pstore_type_id type)
110f0f23e54SJoel Fernandes (Google) {
111f0f23e54SJoel Fernandes (Google) 	BUILD_BUG_ON(ARRAY_SIZE(pstore_type_names) != PSTORE_TYPE_MAX);
112f0f23e54SJoel Fernandes (Google) 
113f0f23e54SJoel Fernandes (Google) 	if (WARN_ON_ONCE(type >= PSTORE_TYPE_MAX))
114f0f23e54SJoel Fernandes (Google) 		return "unknown";
115f0f23e54SJoel Fernandes (Google) 
116f0f23e54SJoel Fernandes (Google) 	return pstore_type_names[type];
117f0f23e54SJoel Fernandes (Google) }
118f0f23e54SJoel Fernandes (Google) EXPORT_SYMBOL_GPL(pstore_type_to_name);
119f0f23e54SJoel Fernandes (Google) 
120f0f23e54SJoel Fernandes (Google) enum pstore_type_id pstore_name_to_type(const char *name)
121f0f23e54SJoel Fernandes (Google) {
122f0f23e54SJoel Fernandes (Google) 	int i;
123f0f23e54SJoel Fernandes (Google) 
124f0f23e54SJoel Fernandes (Google) 	for (i = 0; i < PSTORE_TYPE_MAX; i++) {
125f0f23e54SJoel Fernandes (Google) 		if (!strcmp(pstore_type_names[i], name))
126f0f23e54SJoel Fernandes (Google) 			return i;
127f0f23e54SJoel Fernandes (Google) 	}
128f0f23e54SJoel Fernandes (Google) 
129f0f23e54SJoel Fernandes (Google) 	return PSTORE_TYPE_MAX;
130f0f23e54SJoel Fernandes (Google) }
131f0f23e54SJoel Fernandes (Google) EXPORT_SYMBOL_GPL(pstore_name_to_type);
132f0f23e54SJoel Fernandes (Google) 
133381b872cSSeiji Aguchi static const char *get_reason_str(enum kmsg_dump_reason reason)
134381b872cSSeiji Aguchi {
135381b872cSSeiji Aguchi 	switch (reason) {
136381b872cSSeiji Aguchi 	case KMSG_DUMP_PANIC:
137381b872cSSeiji Aguchi 		return "Panic";
138381b872cSSeiji Aguchi 	case KMSG_DUMP_OOPS:
139381b872cSSeiji Aguchi 		return "Oops";
140381b872cSSeiji Aguchi 	case KMSG_DUMP_EMERG:
141381b872cSSeiji Aguchi 		return "Emergency";
142381b872cSSeiji Aguchi 	case KMSG_DUMP_RESTART:
143381b872cSSeiji Aguchi 		return "Restart";
144381b872cSSeiji Aguchi 	case KMSG_DUMP_HALT:
145381b872cSSeiji Aguchi 		return "Halt";
146381b872cSSeiji Aguchi 	case KMSG_DUMP_POWEROFF:
147381b872cSSeiji Aguchi 		return "Poweroff";
148381b872cSSeiji Aguchi 	default:
149381b872cSSeiji Aguchi 		return "Unknown";
150381b872cSSeiji Aguchi 	}
151381b872cSSeiji Aguchi }
1529f6af27fSTony Luck 
15378c83c82SKees Cook static void pstore_timer_kick(void)
15478c83c82SKees Cook {
15578c83c82SKees Cook 	if (pstore_update_ms < 0)
15678c83c82SKees Cook 		return;
15778c83c82SKees Cook 
15878c83c82SKees Cook 	mod_timer(&pstore_timer, jiffies + msecs_to_jiffies(pstore_update_ms));
15978c83c82SKees Cook }
16078c83c82SKees Cook 
1619f244e9cSSeiji Aguchi /*
162ea84b580SKees Cook  * Should pstore_dump() wait for a concurrent pstore_dump()? If
163ea84b580SKees Cook  * not, the current pstore_dump() will report a failure to dump
164ea84b580SKees Cook  * and return.
1659f244e9cSSeiji Aguchi  */
166ea84b580SKees Cook static bool pstore_cannot_wait(enum kmsg_dump_reason reason)
167ea84b580SKees Cook {
168ea84b580SKees Cook 	/* In NMI path, pstore shouldn't block regardless of reason. */
1699f244e9cSSeiji Aguchi 	if (in_nmi())
1709f244e9cSSeiji Aguchi 		return true;
1719f244e9cSSeiji Aguchi 
1729f244e9cSSeiji Aguchi 	switch (reason) {
1739f244e9cSSeiji Aguchi 	/* In panic case, other cpus are stopped by smp_send_stop(). */
1749f244e9cSSeiji Aguchi 	case KMSG_DUMP_PANIC:
175ea84b580SKees Cook 	/* Emergency restart shouldn't be blocked. */
1769f244e9cSSeiji Aguchi 	case KMSG_DUMP_EMERG:
1779f244e9cSSeiji Aguchi 		return true;
1789f244e9cSSeiji Aguchi 	default:
1799f244e9cSSeiji Aguchi 		return false;
1809f244e9cSSeiji Aguchi 	}
1819f244e9cSSeiji Aguchi }
1829f244e9cSSeiji Aguchi 
18358eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
184cb3bee03SGeliang Tang static int zbufsize_deflate(size_t size)
185b0aad7a9SAruna Balakrishnaiah {
1867de8fe2fSAruna Balakrishnaiah 	size_t cmpr;
187b0aad7a9SAruna Balakrishnaiah 
188cb3bee03SGeliang Tang 	switch (size) {
1897de8fe2fSAruna Balakrishnaiah 	/* buffer range for efivars */
1907de8fe2fSAruna Balakrishnaiah 	case 1000 ... 2000:
1917de8fe2fSAruna Balakrishnaiah 		cmpr = 56;
1927de8fe2fSAruna Balakrishnaiah 		break;
1937de8fe2fSAruna Balakrishnaiah 	case 2001 ... 3000:
1947de8fe2fSAruna Balakrishnaiah 		cmpr = 54;
1957de8fe2fSAruna Balakrishnaiah 		break;
1967de8fe2fSAruna Balakrishnaiah 	case 3001 ... 3999:
1977de8fe2fSAruna Balakrishnaiah 		cmpr = 52;
1987de8fe2fSAruna Balakrishnaiah 		break;
1997de8fe2fSAruna Balakrishnaiah 	/* buffer range for nvram, erst */
2007de8fe2fSAruna Balakrishnaiah 	case 4000 ... 10000:
2017de8fe2fSAruna Balakrishnaiah 		cmpr = 45;
2027de8fe2fSAruna Balakrishnaiah 		break;
2037de8fe2fSAruna Balakrishnaiah 	default:
2047de8fe2fSAruna Balakrishnaiah 		cmpr = 60;
2057de8fe2fSAruna Balakrishnaiah 		break;
2067de8fe2fSAruna Balakrishnaiah 	}
2077de8fe2fSAruna Balakrishnaiah 
208cb3bee03SGeliang Tang 	return (size * 100) / cmpr;
2098cfc8ddcSGeliang Tang }
2108cfc8ddcSGeliang Tang #endif
2118cfc8ddcSGeliang Tang 
21258eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
213cb3bee03SGeliang Tang static int zbufsize_lzo(size_t size)
2148cfc8ddcSGeliang Tang {
215cb3bee03SGeliang Tang 	return lzo1x_worst_compress(size);
2168cfc8ddcSGeliang Tang }
2178cfc8ddcSGeliang Tang #endif
2188cfc8ddcSGeliang Tang 
21958eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
220cb3bee03SGeliang Tang static int zbufsize_lz4(size_t size)
2218cfc8ddcSGeliang Tang {
222cb3bee03SGeliang Tang 	return LZ4_compressBound(size);
223239b7161SGeliang Tang }
224239b7161SGeliang Tang #endif
225239b7161SGeliang Tang 
22658eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
227cb3bee03SGeliang Tang static int zbufsize_842(size_t size)
228239b7161SGeliang Tang {
22955597406SKees Cook 	return size;
230239b7161SGeliang Tang }
231fe1d4758SKees Cook #endif
2328cfc8ddcSGeliang Tang 
2331021bcf4SGeliang Tang #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
2341021bcf4SGeliang Tang static int zbufsize_zstd(size_t size)
2351021bcf4SGeliang Tang {
2361021bcf4SGeliang Tang 	return ZSTD_compressBound(size);
2371021bcf4SGeliang Tang }
2381021bcf4SGeliang Tang #endif
2391021bcf4SGeliang Tang 
240fe1d4758SKees Cook static const struct pstore_zbackend *zbackend __ro_after_init;
241fe1d4758SKees Cook 
242fe1d4758SKees Cook static const struct pstore_zbackend zbackends[] = {
24358eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
244fe1d4758SKees Cook 	{
245cb3bee03SGeliang Tang 		.zbufsize	= zbufsize_deflate,
246cb3bee03SGeliang Tang 		.name		= "deflate",
247fe1d4758SKees Cook 	},
248fe1d4758SKees Cook #endif
24958eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
250fe1d4758SKees Cook 	{
251cb3bee03SGeliang Tang 		.zbufsize	= zbufsize_lzo,
252fe1d4758SKees Cook 		.name		= "lzo",
253fe1d4758SKees Cook 	},
254fe1d4758SKees Cook #endif
25558eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS)
256fe1d4758SKees Cook 	{
257cb3bee03SGeliang Tang 		.zbufsize	= zbufsize_lz4,
258fe1d4758SKees Cook 		.name		= "lz4",
259fe1d4758SKees Cook 	},
260fe1d4758SKees Cook #endif
26158eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
262fe1d4758SKees Cook 	{
263cb3bee03SGeliang Tang 		.zbufsize	= zbufsize_lz4,
264fe1d4758SKees Cook 		.name		= "lz4hc",
265fe1d4758SKees Cook 	},
266fe1d4758SKees Cook #endif
26758eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
268fe1d4758SKees Cook 	{
269cb3bee03SGeliang Tang 		.zbufsize	= zbufsize_842,
270239b7161SGeliang Tang 		.name		= "842",
271fe1d4758SKees Cook 	},
272fe1d4758SKees Cook #endif
2731021bcf4SGeliang Tang #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
2741021bcf4SGeliang Tang 	{
2751021bcf4SGeliang Tang 		.zbufsize	= zbufsize_zstd,
2761021bcf4SGeliang Tang 		.name		= "zstd",
2771021bcf4SGeliang Tang 	},
2781021bcf4SGeliang Tang #endif
279fe1d4758SKees Cook 	{ }
2808cfc8ddcSGeliang Tang };
2818cfc8ddcSGeliang Tang 
2828cfc8ddcSGeliang Tang static int pstore_compress(const void *in, void *out,
283cb3bee03SGeliang Tang 			   unsigned int inlen, unsigned int outlen)
2848cfc8ddcSGeliang Tang {
285cb3bee03SGeliang Tang 	int ret;
286cb3bee03SGeliang Tang 
287cb3bee03SGeliang Tang 	ret = crypto_comp_compress(tfm, in, inlen, out, &outlen);
288cb3bee03SGeliang Tang 	if (ret) {
289cb3bee03SGeliang Tang 		pr_err("crypto_comp_compress failed, ret = %d!\n", ret);
290cb3bee03SGeliang Tang 		return ret;
2918cfc8ddcSGeliang Tang 	}
2928cfc8ddcSGeliang Tang 
293cb3bee03SGeliang Tang 	return outlen;
294cb3bee03SGeliang Tang }
295cb3bee03SGeliang Tang 
2968cfc8ddcSGeliang Tang static void allocate_buf_for_compression(void)
2978cfc8ddcSGeliang Tang {
29895047b05SKees Cook 	struct crypto_comp *ctx;
29995047b05SKees Cook 	int size;
30095047b05SKees Cook 	char *buf;
30195047b05SKees Cook 
30295047b05SKees Cook 	/* Skip if not built-in or compression backend not selected yet. */
303e698aaf3STobias Regnery 	if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !zbackend)
304cb3bee03SGeliang Tang 		return;
305cb3bee03SGeliang Tang 
30695047b05SKees Cook 	/* Skip if no pstore backend yet or compression init already done. */
30795047b05SKees Cook 	if (!psinfo || tfm)
30895047b05SKees Cook 		return;
30995047b05SKees Cook 
310cb3bee03SGeliang Tang 	if (!crypto_has_comp(zbackend->name, 0, 0)) {
31195047b05SKees Cook 		pr_err("Unknown compression: %s\n", zbackend->name);
312cb3bee03SGeliang Tang 		return;
313cb3bee03SGeliang Tang 	}
314cb3bee03SGeliang Tang 
31595047b05SKees Cook 	size = zbackend->zbufsize(psinfo->bufsize);
31695047b05SKees Cook 	if (size <= 0) {
31795047b05SKees Cook 		pr_err("Invalid compression size for %s: %d\n",
31895047b05SKees Cook 		       zbackend->name, size);
319cb3bee03SGeliang Tang 		return;
320cb3bee03SGeliang Tang 	}
321cb3bee03SGeliang Tang 
32295047b05SKees Cook 	buf = kmalloc(size, GFP_KERNEL);
32395047b05SKees Cook 	if (!buf) {
32495047b05SKees Cook 		pr_err("Failed %d byte compression buffer allocation for: %s\n",
32595047b05SKees Cook 		       size, zbackend->name);
326cb3bee03SGeliang Tang 		return;
3278cfc8ddcSGeliang Tang 	}
32895047b05SKees Cook 
32995047b05SKees Cook 	ctx = crypto_alloc_comp(zbackend->name, 0, 0);
33095047b05SKees Cook 	if (IS_ERR_OR_NULL(ctx)) {
33195047b05SKees Cook 		kfree(buf);
33295047b05SKees Cook 		pr_err("crypto_alloc_comp('%s') failed: %ld\n", zbackend->name,
33395047b05SKees Cook 		       PTR_ERR(ctx));
33495047b05SKees Cook 		return;
33595047b05SKees Cook 	}
33695047b05SKees Cook 
33795047b05SKees Cook 	/* A non-NULL big_oops_buf indicates compression is available. */
33895047b05SKees Cook 	tfm = ctx;
33995047b05SKees Cook 	big_oops_buf_sz = size;
34095047b05SKees Cook 	big_oops_buf = buf;
34195047b05SKees Cook 
3420eed84ffSKees Cook 	pr_info("Using crash dump compression: %s\n", zbackend->name);
3438cfc8ddcSGeliang Tang }
3448cfc8ddcSGeliang Tang 
3458cfc8ddcSGeliang Tang static void free_buf_for_compression(void)
3468cfc8ddcSGeliang Tang {
347a9fb94a9SPi-Hsun Shih 	if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && tfm) {
348cb3bee03SGeliang Tang 		crypto_free_comp(tfm);
349a9fb94a9SPi-Hsun Shih 		tfm = NULL;
350a9fb94a9SPi-Hsun Shih 	}
351cb3bee03SGeliang Tang 	kfree(big_oops_buf);
352cb3bee03SGeliang Tang 	big_oops_buf = NULL;
353cb3bee03SGeliang Tang 	big_oops_buf_sz = 0;
354ee1d2674SGeliang Tang }
355ee1d2674SGeliang Tang 
356b0aad7a9SAruna Balakrishnaiah /*
357b0aad7a9SAruna Balakrishnaiah  * Called when compression fails, since the printk buffer
358b0aad7a9SAruna Balakrishnaiah  * would be fetched for compression calling it again when
359b0aad7a9SAruna Balakrishnaiah  * compression fails would have moved the iterator of
360b0aad7a9SAruna Balakrishnaiah  * printk buffer which results in fetching old contents.
361b0aad7a9SAruna Balakrishnaiah  * Copy the recent messages from big_oops_buf to psinfo->buf
362b0aad7a9SAruna Balakrishnaiah  */
363b0aad7a9SAruna Balakrishnaiah static size_t copy_kmsg_to_buffer(int hsize, size_t len)
364b0aad7a9SAruna Balakrishnaiah {
365b0aad7a9SAruna Balakrishnaiah 	size_t total_len;
366b0aad7a9SAruna Balakrishnaiah 	size_t diff;
367b0aad7a9SAruna Balakrishnaiah 
368b0aad7a9SAruna Balakrishnaiah 	total_len = hsize + len;
369b0aad7a9SAruna Balakrishnaiah 
370b0aad7a9SAruna Balakrishnaiah 	if (total_len > psinfo->bufsize) {
371b0aad7a9SAruna Balakrishnaiah 		diff = total_len - psinfo->bufsize + hsize;
372b0aad7a9SAruna Balakrishnaiah 		memcpy(psinfo->buf, big_oops_buf, hsize);
373b0aad7a9SAruna Balakrishnaiah 		memcpy(psinfo->buf + hsize, big_oops_buf + diff,
374b0aad7a9SAruna Balakrishnaiah 					psinfo->bufsize - hsize);
375b0aad7a9SAruna Balakrishnaiah 		total_len = psinfo->bufsize;
376b0aad7a9SAruna Balakrishnaiah 	} else
377b0aad7a9SAruna Balakrishnaiah 		memcpy(psinfo->buf, big_oops_buf, total_len);
378b0aad7a9SAruna Balakrishnaiah 
379b0aad7a9SAruna Balakrishnaiah 	return total_len;
380b0aad7a9SAruna Balakrishnaiah }
381b0aad7a9SAruna Balakrishnaiah 
382e581ca81SKees Cook void pstore_record_init(struct pstore_record *record,
383e581ca81SKees Cook 			struct pstore_info *psinfo)
384e581ca81SKees Cook {
385e581ca81SKees Cook 	memset(record, 0, sizeof(*record));
386e581ca81SKees Cook 
387e581ca81SKees Cook 	record->psi = psinfo;
388c7f3c595SKees Cook 
389c7f3c595SKees Cook 	/* Report zeroed timestamp if called before timekeeping has resumed. */
3907aaa822eSKees Cook 	record->time = ns_to_timespec64(ktime_get_real_fast_ns());
391e581ca81SKees Cook }
392e581ca81SKees Cook 
393ca01d6ddSTony Luck /*
3940eed84ffSKees Cook  * callback from kmsg_dump. Save as much as we can (up to kmsg_bytes) from the
3950eed84ffSKees Cook  * end of the buffer.
396ca01d6ddSTony Luck  */
397ca01d6ddSTony Luck static void pstore_dump(struct kmsg_dumper *dumper,
398e2ae715dSKay Sievers 			enum kmsg_dump_reason reason)
399ca01d6ddSTony Luck {
400e2ae715dSKay Sievers 	unsigned long	total = 0;
401381b872cSSeiji Aguchi 	const char	*why;
402b94fdd07SMatthew Garrett 	unsigned int	part = 1;
403e2ae715dSKay Sievers 	int		ret;
404ca01d6ddSTony Luck 
405381b872cSSeiji Aguchi 	why = get_reason_str(reason);
4069f6af27fSTony Luck 
407ea84b580SKees Cook 	if (down_trylock(&psinfo->buf_lock)) {
408ea84b580SKees Cook 		/* Failed to acquire lock: give up if we cannot wait. */
409ea84b580SKees Cook 		if (pstore_cannot_wait(reason)) {
410ea84b580SKees Cook 			pr_err("dump skipped in %s path: may corrupt error record\n",
411ea84b580SKees Cook 				in_nmi() ? "NMI" : why);
412959217c8SLi Pengcheng 			return;
4139f244e9cSSeiji Aguchi 		}
414ea84b580SKees Cook 		if (down_interruptible(&psinfo->buf_lock)) {
415ea84b580SKees Cook 			pr_err("could not grab semaphore?!\n");
416ea84b580SKees Cook 			return;
41798e44fdaSNamhyung Kim 		}
418ea84b580SKees Cook 	}
419ea84b580SKees Cook 
420ca01d6ddSTony Luck 	oopscount++;
421ca01d6ddSTony Luck 	while (total < kmsg_bytes) {
422e2ae715dSKay Sievers 		char *dst;
42376cc9580SKees Cook 		size_t dst_size;
42476cc9580SKees Cook 		int header_size;
425b0aad7a9SAruna Balakrishnaiah 		int zipped_len = -1;
42676cc9580SKees Cook 		size_t dump_size;
427e581ca81SKees Cook 		struct pstore_record record;
428e581ca81SKees Cook 
429e581ca81SKees Cook 		pstore_record_init(&record, psinfo);
430e581ca81SKees Cook 		record.type = PSTORE_TYPE_DMESG;
431e581ca81SKees Cook 		record.count = oopscount;
432e581ca81SKees Cook 		record.reason = reason;
433e581ca81SKees Cook 		record.part = part;
434e581ca81SKees Cook 		record.buf = psinfo->buf;
435e2ae715dSKay Sievers 
436ea84b580SKees Cook 		if (big_oops_buf) {
437b0aad7a9SAruna Balakrishnaiah 			dst = big_oops_buf;
43876cc9580SKees Cook 			dst_size = big_oops_buf_sz;
439235f6d15SNamhyung Kim 		} else {
440235f6d15SNamhyung Kim 			dst = psinfo->buf;
44176cc9580SKees Cook 			dst_size = psinfo->bufsize;
442235f6d15SNamhyung Kim 		}
443235f6d15SNamhyung Kim 
44476cc9580SKees Cook 		/* Write dump header. */
44576cc9580SKees Cook 		header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why,
44676cc9580SKees Cook 				 oopscount, part);
44776cc9580SKees Cook 		dst_size -= header_size;
448b0aad7a9SAruna Balakrishnaiah 
44976cc9580SKees Cook 		/* Write dump contents. */
45076cc9580SKees Cook 		if (!kmsg_dump_get_buffer(dumper, true, dst + header_size,
45176cc9580SKees Cook 					  dst_size, &dump_size))
452b0aad7a9SAruna Balakrishnaiah 			break;
453b0aad7a9SAruna Balakrishnaiah 
454ea84b580SKees Cook 		if (big_oops_buf) {
455b0aad7a9SAruna Balakrishnaiah 			zipped_len = pstore_compress(dst, psinfo->buf,
45676cc9580SKees Cook 						header_size + dump_size,
45776cc9580SKees Cook 						psinfo->bufsize);
458b0aad7a9SAruna Balakrishnaiah 
459b0aad7a9SAruna Balakrishnaiah 			if (zipped_len > 0) {
46076cc9580SKees Cook 				record.compressed = true;
46176cc9580SKees Cook 				record.size = zipped_len;
462b0aad7a9SAruna Balakrishnaiah 			} else {
46376cc9580SKees Cook 				record.size = copy_kmsg_to_buffer(header_size,
46476cc9580SKees Cook 								  dump_size);
465b0aad7a9SAruna Balakrishnaiah 			}
466b0aad7a9SAruna Balakrishnaiah 		} else {
46776cc9580SKees Cook 			record.size = header_size + dump_size;
468b0aad7a9SAruna Balakrishnaiah 		}
469b0aad7a9SAruna Balakrishnaiah 
47076cc9580SKees Cook 		ret = psinfo->write(&record);
47178c83c82SKees Cook 		if (ret == 0 && reason == KMSG_DUMP_OOPS) {
4726dda9266SLuck, Tony 			pstore_new_entry = 1;
47378c83c82SKees Cook 			pstore_timer_kick();
47478c83c82SKees Cook 		}
475e2ae715dSKay Sievers 
47676cc9580SKees Cook 		total += record.size;
47756280682SMatthew Garrett 		part++;
478ca01d6ddSTony Luck 	}
479ea84b580SKees Cook 
480ea84b580SKees Cook 	up(&psinfo->buf_lock);
481ca01d6ddSTony Luck }
482ca01d6ddSTony Luck 
483ca01d6ddSTony Luck static struct kmsg_dumper pstore_dumper = {
484ca01d6ddSTony Luck 	.dump = pstore_dump,
485ca01d6ddSTony Luck };
486ca01d6ddSTony Luck 
487306e5c2aSGeliang Tang /*
488306e5c2aSGeliang Tang  * Register with kmsg_dump to save last part of console log on panic.
489306e5c2aSGeliang Tang  */
49018730411SGeliang Tang static void pstore_register_kmsg(void)
49118730411SGeliang Tang {
49218730411SGeliang Tang 	kmsg_dump_register(&pstore_dumper);
49318730411SGeliang Tang }
49418730411SGeliang Tang 
495ee1d2674SGeliang Tang static void pstore_unregister_kmsg(void)
496ee1d2674SGeliang Tang {
497ee1d2674SGeliang Tang 	kmsg_dump_unregister(&pstore_dumper);
498ee1d2674SGeliang Tang }
499ee1d2674SGeliang Tang 
500f29e5956SAnton Vorontsov #ifdef CONFIG_PSTORE_CONSOLE
501f29e5956SAnton Vorontsov static void pstore_console_write(struct console *con, const char *s, unsigned c)
502f29e5956SAnton Vorontsov {
503e581ca81SKees Cook 	struct pstore_record record;
504f29e5956SAnton Vorontsov 
5054c6c4d34SYue Hu 	if (!c)
5064c6c4d34SYue Hu 		return;
5074c6c4d34SYue Hu 
508e581ca81SKees Cook 	pstore_record_init(&record, psinfo);
509e581ca81SKees Cook 	record.type = PSTORE_TYPE_CONSOLE;
510e581ca81SKees Cook 
511b10b4711SKees Cook 	record.buf = (char *)s;
512b10b4711SKees Cook 	record.size = c;
5134c9ec219SKees Cook 	psinfo->write(&record);
514f29e5956SAnton Vorontsov }
515f29e5956SAnton Vorontsov 
516f29e5956SAnton Vorontsov static struct console pstore_console = {
517f29e5956SAnton Vorontsov 	.write	= pstore_console_write,
518f29e5956SAnton Vorontsov 	.index	= -1,
519f29e5956SAnton Vorontsov };
520f29e5956SAnton Vorontsov 
521f29e5956SAnton Vorontsov static void pstore_register_console(void)
522f29e5956SAnton Vorontsov {
523*d195c390SKees Cook 	/* Show which backend is going to get console writes. */
524*d195c390SKees Cook 	strscpy(pstore_console.name, psinfo->name,
525*d195c390SKees Cook 		sizeof(pstore_console.name));
526b7753fc7SKees Cook 	/*
527b7753fc7SKees Cook 	 * Always initialize flags here since prior unregister_console()
528b7753fc7SKees Cook 	 * calls may have changed settings (specifically CON_ENABLED).
529b7753fc7SKees Cook 	 */
530b7753fc7SKees Cook 	pstore_console.flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME;
531f29e5956SAnton Vorontsov 	register_console(&pstore_console);
532f29e5956SAnton Vorontsov }
533ee1d2674SGeliang Tang 
534ee1d2674SGeliang Tang static void pstore_unregister_console(void)
535ee1d2674SGeliang Tang {
536ee1d2674SGeliang Tang 	unregister_console(&pstore_console);
537ee1d2674SGeliang Tang }
538f29e5956SAnton Vorontsov #else
539f29e5956SAnton Vorontsov static void pstore_register_console(void) {}
540ee1d2674SGeliang Tang static void pstore_unregister_console(void) {}
541f29e5956SAnton Vorontsov #endif
542f29e5956SAnton Vorontsov 
5434c9ec219SKees Cook static int pstore_write_user_compat(struct pstore_record *record,
544fdd03118SKees Cook 				    const char __user *buf)
5455bf6d1b9SMark Salyzyn {
54630800d99SKees Cook 	int ret = 0;
5475bf6d1b9SMark Salyzyn 
54830800d99SKees Cook 	if (record->buf)
54930800d99SKees Cook 		return -EINVAL;
5505bf6d1b9SMark Salyzyn 
551077090afSGeliang Tang 	record->buf = memdup_user(buf, record->size);
552dfd6fa39SHirofumi Nakagawa 	if (IS_ERR(record->buf)) {
553077090afSGeliang Tang 		ret = PTR_ERR(record->buf);
55430800d99SKees Cook 		goto out;
5555bf6d1b9SMark Salyzyn 	}
55630800d99SKees Cook 
5574c9ec219SKees Cook 	ret = record->psi->write(record);
55830800d99SKees Cook 
55930800d99SKees Cook 	kfree(record->buf);
560077090afSGeliang Tang out:
56130800d99SKees Cook 	record->buf = NULL;
56230800d99SKees Cook 
56330800d99SKees Cook 	return unlikely(ret < 0) ? ret : record->size;
5645bf6d1b9SMark Salyzyn }
5655bf6d1b9SMark Salyzyn 
566ca01d6ddSTony Luck /*
567ca01d6ddSTony Luck  * platform specific persistent storage driver registers with
568ca01d6ddSTony Luck  * us here. If pstore is already mounted, call the platform
569ca01d6ddSTony Luck  * read function right away to populate the file system. If not
570ca01d6ddSTony Luck  * then the pstore mount code will call us later to fill out
571ca01d6ddSTony Luck  * the file system.
572ca01d6ddSTony Luck  */
573ca01d6ddSTony Luck int pstore_register(struct pstore_info *psi)
574ca01d6ddSTony Luck {
5750d7cd09aSKees Cook 	if (backend && strcmp(backend, psi->name)) {
5760d7cd09aSKees Cook 		pr_warn("ignoring unexpected backend '%s'\n", psi->name);
5778e48b1a8SLenny Szubowicz 		return -EPERM;
5780d7cd09aSKees Cook 	}
5798e48b1a8SLenny Szubowicz 
5804c9ec219SKees Cook 	/* Sanity check flags. */
5814c9ec219SKees Cook 	if (!psi->flags) {
5824c9ec219SKees Cook 		pr_warn("backend '%s' must support at least one frontend\n",
5834c9ec219SKees Cook 			psi->name);
5844c9ec219SKees Cook 		return -EINVAL;
5854c9ec219SKees Cook 	}
5864c9ec219SKees Cook 
5874c9ec219SKees Cook 	/* Check for required functions. */
5884c9ec219SKees Cook 	if (!psi->read || !psi->write) {
5894c9ec219SKees Cook 		pr_warn("backend '%s' must implement read() and write()\n",
5904c9ec219SKees Cook 			psi->name);
5914c9ec219SKees Cook 		return -EINVAL;
5924c9ec219SKees Cook 	}
5934c9ec219SKees Cook 
594cab12fd0SKees Cook 	mutex_lock(&psinfo_lock);
595ca01d6ddSTony Luck 	if (psinfo) {
5960d7cd09aSKees Cook 		pr_warn("backend '%s' already loaded: ignoring '%s'\n",
5970d7cd09aSKees Cook 			psinfo->name, psi->name);
598cab12fd0SKees Cook 		mutex_unlock(&psinfo_lock);
599ca01d6ddSTony Luck 		return -EBUSY;
600ca01d6ddSTony Luck 	}
601dee28e72SMatthew Garrett 
6024c9ec219SKees Cook 	if (!psi->write_user)
6034c9ec219SKees Cook 		psi->write_user = pstore_write_user_compat;
604ca01d6ddSTony Luck 	psinfo = psi;
605f6f82851SKees Cook 	mutex_init(&psinfo->read_mutex);
606ea84b580SKees Cook 	sema_init(&psinfo->buf_lock, 1);
607ca01d6ddSTony Luck 
6088880fa32SKees Cook 	if (psi->flags & PSTORE_FLAGS_DMESG)
609b0aad7a9SAruna Balakrishnaiah 		allocate_buf_for_compression();
610b0aad7a9SAruna Balakrishnaiah 
6116dda9266SLuck, Tony 	pstore_get_records(0);
612ca01d6ddSTony Luck 
613c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_DMESG)
61418730411SGeliang Tang 		pstore_register_kmsg();
615c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_CONSOLE)
616f29e5956SAnton Vorontsov 		pstore_register_console();
617c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_FTRACE)
61865f8c95eSAnton Vorontsov 		pstore_register_ftrace();
619c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_PMSG)
6209d5438f4SMark Salyzyn 		pstore_register_pmsg();
621ca01d6ddSTony Luck 
6226330d553SKees Cook 	/* Start watching for new records, if desired. */
62378c83c82SKees Cook 	pstore_timer_kick();
6246dda9266SLuck, Tony 
62542222c2aSWang Long 	/*
62642222c2aSWang Long 	 * Update the module parameter backend, so it is visible
62742222c2aSWang Long 	 * through /sys/module/pstore/parameters/backend
62842222c2aSWang Long 	 */
629563ca40dSKees Cook 	backend = kstrdup(psi->name, GFP_KERNEL);
63042222c2aSWang Long 
631ef748853SFabian Frederick 	pr_info("Registered %s as persistent store backend\n", psi->name);
6328e48b1a8SLenny Szubowicz 
6336248a066SKees Cook 	mutex_unlock(&psinfo_lock);
634ca01d6ddSTony Luck 	return 0;
635ca01d6ddSTony Luck }
636ca01d6ddSTony Luck EXPORT_SYMBOL_GPL(pstore_register);
637ca01d6ddSTony Luck 
638ee1d2674SGeliang Tang void pstore_unregister(struct pstore_info *psi)
639ee1d2674SGeliang Tang {
6406248a066SKees Cook 	/* It's okay to unregister nothing. */
6416248a066SKees Cook 	if (!psi)
6426248a066SKees Cook 		return;
6436248a066SKees Cook 
6446248a066SKees Cook 	mutex_lock(&psinfo_lock);
6456248a066SKees Cook 
6466248a066SKees Cook 	/* Only one backend can be registered at a time. */
6476248a066SKees Cook 	if (WARN_ON(psi != psinfo)) {
6486248a066SKees Cook 		mutex_unlock(&psinfo_lock);
6496248a066SKees Cook 		return;
6506248a066SKees Cook 	}
6516248a066SKees Cook 
65278c83c82SKees Cook 	/* Unregister all callbacks. */
653c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_PMSG)
654ee1d2674SGeliang Tang 		pstore_unregister_pmsg();
655c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_FTRACE)
656ee1d2674SGeliang Tang 		pstore_unregister_ftrace();
657c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_CONSOLE)
658ee1d2674SGeliang Tang 		pstore_unregister_console();
659c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_DMESG)
660ee1d2674SGeliang Tang 		pstore_unregister_kmsg();
661ee1d2674SGeliang Tang 
66278c83c82SKees Cook 	/* Stop timer and make sure all work has finished. */
66378c83c82SKees Cook 	del_timer_sync(&pstore_timer);
66478c83c82SKees Cook 	flush_work(&pstore_work);
66578c83c82SKees Cook 
666609e28bbSKees Cook 	/* Remove all backend records from filesystem tree. */
667609e28bbSKees Cook 	pstore_put_backend_records(psi);
668609e28bbSKees Cook 
669ee1d2674SGeliang Tang 	free_buf_for_compression();
670ee1d2674SGeliang Tang 
671ee1d2674SGeliang Tang 	psinfo = NULL;
672563ca40dSKees Cook 	kfree(backend);
673ee1d2674SGeliang Tang 	backend = NULL;
6746248a066SKees Cook 	mutex_unlock(&psinfo_lock);
675ee1d2674SGeliang Tang }
676ee1d2674SGeliang Tang EXPORT_SYMBOL_GPL(pstore_unregister);
677ee1d2674SGeliang Tang 
678634f8f51SKees Cook static void decompress_record(struct pstore_record *record)
679634f8f51SKees Cook {
680bdabc8e7SKees Cook 	int ret;
681634f8f51SKees Cook 	int unzipped_len;
682bdabc8e7SKees Cook 	char *unzipped, *workspace;
683634f8f51SKees Cook 
6844a16d1cbSAnkit Kumar 	if (!record->compressed)
6854a16d1cbSAnkit Kumar 		return;
6864a16d1cbSAnkit Kumar 
687634f8f51SKees Cook 	/* Only PSTORE_TYPE_DMESG support compression. */
6884a16d1cbSAnkit Kumar 	if (record->type != PSTORE_TYPE_DMESG) {
689634f8f51SKees Cook 		pr_warn("ignored compressed record type %d\n", record->type);
690634f8f51SKees Cook 		return;
691634f8f51SKees Cook 	}
692634f8f51SKees Cook 
693bdabc8e7SKees Cook 	/* Missing compression buffer means compression was not initialized. */
694634f8f51SKees Cook 	if (!big_oops_buf) {
695bdabc8e7SKees Cook 		pr_warn("no decompression method initialized!\n");
696634f8f51SKees Cook 		return;
697634f8f51SKees Cook 	}
698634f8f51SKees Cook 
699bdabc8e7SKees Cook 	/* Allocate enough space to hold max decompression and ECC. */
700bdabc8e7SKees Cook 	unzipped_len = big_oops_buf_sz;
701bdabc8e7SKees Cook 	workspace = kmalloc(unzipped_len + record->ecc_notice_size,
7027e8cc8dcSKees Cook 			    GFP_KERNEL);
703bdabc8e7SKees Cook 	if (!workspace)
704bdabc8e7SKees Cook 		return;
705bdabc8e7SKees Cook 
706bdabc8e7SKees Cook 	/* After decompression "unzipped_len" is almost certainly smaller. */
707bdabc8e7SKees Cook 	ret = crypto_comp_decompress(tfm, record->buf, record->size,
708bdabc8e7SKees Cook 					  workspace, &unzipped_len);
709bdabc8e7SKees Cook 	if (ret) {
710bdabc8e7SKees Cook 		pr_err("crypto_comp_decompress failed, ret = %d!\n", ret);
711bdabc8e7SKees Cook 		kfree(workspace);
7127e8cc8dcSKees Cook 		return;
7137e8cc8dcSKees Cook 	}
7147e8cc8dcSKees Cook 
7157e8cc8dcSKees Cook 	/* Append ECC notice to decompressed buffer. */
716bdabc8e7SKees Cook 	memcpy(workspace + unzipped_len, record->buf + record->size,
717634f8f51SKees Cook 	       record->ecc_notice_size);
7187e8cc8dcSKees Cook 
719bdabc8e7SKees Cook 	/* Copy decompressed contents into an minimum-sized allocation. */
720bdabc8e7SKees Cook 	unzipped = kmemdup(workspace, unzipped_len + record->ecc_notice_size,
721bdabc8e7SKees Cook 			   GFP_KERNEL);
722bdabc8e7SKees Cook 	kfree(workspace);
723bdabc8e7SKees Cook 	if (!unzipped)
724bdabc8e7SKees Cook 		return;
725bdabc8e7SKees Cook 
726bdabc8e7SKees Cook 	/* Swap out compressed contents with decompressed contents. */
727634f8f51SKees Cook 	kfree(record->buf);
728bdabc8e7SKees Cook 	record->buf = unzipped;
729634f8f51SKees Cook 	record->size = unzipped_len;
730634f8f51SKees Cook 	record->compressed = false;
731634f8f51SKees Cook }
732634f8f51SKees Cook 
733ca01d6ddSTony Luck /*
7343a7d2fd1SKees Cook  * Read all the records from one persistent store backend. Create
7356dda9266SLuck, Tony  * files in our filesystem.  Don't warn about -EEXIST errors
7366dda9266SLuck, Tony  * when we are re-scanning the backing store looking to add new
7376dda9266SLuck, Tony  * error records.
738ca01d6ddSTony Luck  */
7393a7d2fd1SKees Cook void pstore_get_backend_records(struct pstore_info *psi,
7403a7d2fd1SKees Cook 				struct dentry *root, int quiet)
741ca01d6ddSTony Luck {
7422a2b0acfSKees Cook 	int failed = 0;
743656de42eSKees Cook 	unsigned int stop_loop = 65536;
744ca01d6ddSTony Luck 
7453a7d2fd1SKees Cook 	if (!psi || !root)
746ca01d6ddSTony Luck 		return;
747ca01d6ddSTony Luck 
748f6f82851SKees Cook 	mutex_lock(&psi->read_mutex);
7492174f6dfSKees Cook 	if (psi->open && psi->open(psi))
75006cf91b4SChen Gong 		goto out;
75106cf91b4SChen Gong 
7521dfff7ddSKees Cook 	/*
7531dfff7ddSKees Cook 	 * Backend callback read() allocates record.buf. decompress_record()
7541dfff7ddSKees Cook 	 * may reallocate record.buf. On success, pstore_mkfile() will keep
7551dfff7ddSKees Cook 	 * the record.buf, so free it only on failure.
7561dfff7ddSKees Cook 	 */
757656de42eSKees Cook 	for (; stop_loop; stop_loop--) {
7582a2b0acfSKees Cook 		struct pstore_record *record;
7592a2b0acfSKees Cook 		int rc;
7602a2b0acfSKees Cook 
7612a2b0acfSKees Cook 		record = kzalloc(sizeof(*record), GFP_KERNEL);
7622a2b0acfSKees Cook 		if (!record) {
7632a2b0acfSKees Cook 			pr_err("out of memory creating record\n");
7642a2b0acfSKees Cook 			break;
7652a2b0acfSKees Cook 		}
766e581ca81SKees Cook 		pstore_record_init(record, psi);
7672a2b0acfSKees Cook 
7682a2b0acfSKees Cook 		record->size = psi->read(record);
7692a2b0acfSKees Cook 
7702a2b0acfSKees Cook 		/* No more records left in backend? */
771f6525b96SDouglas Anderson 		if (record->size <= 0) {
772f6525b96SDouglas Anderson 			kfree(record);
7732a2b0acfSKees Cook 			break;
774f6525b96SDouglas Anderson 		}
7752a2b0acfSKees Cook 
7762a2b0acfSKees Cook 		decompress_record(record);
7773a7d2fd1SKees Cook 		rc = pstore_mkfile(root, record);
7781dfff7ddSKees Cook 		if (rc) {
77983f70f07SKees Cook 			/* pstore_mkfile() did not take record, so free it. */
7802a2b0acfSKees Cook 			kfree(record->buf);
78183f70f07SKees Cook 			kfree(record);
7821dfff7ddSKees Cook 			if (rc != -EEXIST || !quiet)
7831dfff7ddSKees Cook 				failed++;
7841dfff7ddSKees Cook 		}
785ca01d6ddSTony Luck 	}
7862174f6dfSKees Cook 	if (psi->close)
78706cf91b4SChen Gong 		psi->close(psi);
78806cf91b4SChen Gong out:
789f6f82851SKees Cook 	mutex_unlock(&psi->read_mutex);
790ca01d6ddSTony Luck 
791ca01d6ddSTony Luck 	if (failed)
792656de42eSKees Cook 		pr_warn("failed to create %d record(s) from '%s'\n",
793ca01d6ddSTony Luck 			failed, psi->name);
794656de42eSKees Cook 	if (!stop_loop)
795656de42eSKees Cook 		pr_err("looping? Too many records seen from '%s'\n",
796656de42eSKees Cook 			psi->name);
797ca01d6ddSTony Luck }
798ca01d6ddSTony Luck 
7996dda9266SLuck, Tony static void pstore_dowork(struct work_struct *work)
8006dda9266SLuck, Tony {
8016dda9266SLuck, Tony 	pstore_get_records(1);
8026dda9266SLuck, Tony }
8036dda9266SLuck, Tony 
80424ed960aSKees Cook static void pstore_timefunc(struct timer_list *unused)
8056dda9266SLuck, Tony {
8066dda9266SLuck, Tony 	if (pstore_new_entry) {
8076dda9266SLuck, Tony 		pstore_new_entry = 0;
8086dda9266SLuck, Tony 		schedule_work(&pstore_work);
8096dda9266SLuck, Tony 	}
8106dda9266SLuck, Tony 
81178c83c82SKees Cook 	pstore_timer_kick();
8126dda9266SLuck, Tony }
8136dda9266SLuck, Tony 
8148d82cee2SBen Dooks (Codethink) static void __init pstore_choose_compression(void)
815fe1d4758SKees Cook {
816fe1d4758SKees Cook 	const struct pstore_zbackend *step;
817fe1d4758SKees Cook 
818fe1d4758SKees Cook 	if (!compress)
819fe1d4758SKees Cook 		return;
820fe1d4758SKees Cook 
821fe1d4758SKees Cook 	for (step = zbackends; step->name; step++) {
822fe1d4758SKees Cook 		if (!strcmp(compress, step->name)) {
823fe1d4758SKees Cook 			zbackend = step;
824fe1d4758SKees Cook 			return;
825fe1d4758SKees Cook 		}
826fe1d4758SKees Cook 	}
827fe1d4758SKees Cook }
828fe1d4758SKees Cook 
829cb095afdSKees Cook static int __init pstore_init(void)
830cb095afdSKees Cook {
831cb095afdSKees Cook 	int ret;
832cb095afdSKees Cook 
833cb095afdSKees Cook 	pstore_choose_compression();
834cb095afdSKees Cook 
83541603165SJoel Fernandes (Google) 	/*
83641603165SJoel Fernandes (Google) 	 * Check if any pstore backends registered earlier but did not
83741603165SJoel Fernandes (Google) 	 * initialize compression because crypto was not ready. If so,
83841603165SJoel Fernandes (Google) 	 * initialize compression now.
83941603165SJoel Fernandes (Google) 	 */
84041603165SJoel Fernandes (Google) 	allocate_buf_for_compression();
84141603165SJoel Fernandes (Google) 
842cb095afdSKees Cook 	ret = pstore_init_fs();
843cb095afdSKees Cook 	if (ret)
8448a57d6d4Schenqiwu 		free_buf_for_compression();
845cb095afdSKees Cook 
8468a57d6d4Schenqiwu 	return ret;
847cb095afdSKees Cook }
84841603165SJoel Fernandes (Google) late_initcall(pstore_init);
849cb095afdSKees Cook 
850cb095afdSKees Cook static void __exit pstore_exit(void)
851cb095afdSKees Cook {
852cb095afdSKees Cook 	pstore_exit_fs();
853cb095afdSKees Cook }
854cb095afdSKees Cook module_exit(pstore_exit)
855cb095afdSKees Cook 
856fe1d4758SKees Cook module_param(compress, charp, 0444);
857fe1d4758SKees Cook MODULE_PARM_DESC(compress, "Pstore compression to use");
858fe1d4758SKees Cook 
859dee28e72SMatthew Garrett module_param(backend, charp, 0444);
860dee28e72SMatthew Garrett MODULE_PARM_DESC(backend, "Pstore backend to use");
861cb095afdSKees Cook 
862cb095afdSKees Cook MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
863cb095afdSKees Cook MODULE_LICENSE("GPL");
864