xref: /openbmc/linux/fs/pstore/platform.c (revision cab12fd049380b1d0c3ac9f534407a8cc0ac4bba)
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; "
47521f7288SAnton Vorontsov 		 "enabling this option is not 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 /*
72c30b20cdSKees Cook  * psinfo_lock just protects "psinfo" during
73ca01d6ddSTony Luck  * calls to pstore_register()
74ca01d6ddSTony Luck  */
75*cab12fd0SKees Cook static DEFINE_MUTEX(psinfo_lock);
76060287b8SAnton Vorontsov struct pstore_info *psinfo;
77ca01d6ddSTony Luck 
78dee28e72SMatthew Garrett static char *backend;
79fe1d4758SKees Cook static char *compress =
80fe1d4758SKees Cook #ifdef CONFIG_PSTORE_COMPRESS_DEFAULT
81fe1d4758SKees Cook 		CONFIG_PSTORE_COMPRESS_DEFAULT;
82fe1d4758SKees Cook #else
83fe1d4758SKees Cook 		NULL;
84fe1d4758SKees Cook #endif
85dee28e72SMatthew Garrett 
86b0aad7a9SAruna Balakrishnaiah /* Compression parameters */
87cb3bee03SGeliang Tang static struct crypto_comp *tfm;
888cfc8ddcSGeliang Tang 
898cfc8ddcSGeliang Tang struct pstore_zbackend {
90cb3bee03SGeliang Tang 	int (*zbufsize)(size_t size);
918cfc8ddcSGeliang Tang 	const char *name;
928cfc8ddcSGeliang Tang };
93b0aad7a9SAruna Balakrishnaiah 
94b0aad7a9SAruna Balakrishnaiah static char *big_oops_buf;
95b0aad7a9SAruna Balakrishnaiah static size_t big_oops_buf_sz;
96b0aad7a9SAruna Balakrishnaiah 
97366f7e7aSLuck, Tony /* How much of the console log to snapshot */
98349d7438SDavid Howells unsigned long kmsg_bytes = PSTORE_DEFAULT_KMSG_BYTES;
99ca01d6ddSTony Luck 
100366f7e7aSLuck, Tony void pstore_set_kmsg_bytes(int bytes)
101ca01d6ddSTony Luck {
102366f7e7aSLuck, Tony 	kmsg_bytes = bytes;
103ca01d6ddSTony Luck }
104ca01d6ddSTony Luck 
105ca01d6ddSTony Luck /* Tag each group of saved records with a sequence number */
106ca01d6ddSTony Luck static int	oopscount;
107ca01d6ddSTony Luck 
108f0f23e54SJoel Fernandes (Google) const char *pstore_type_to_name(enum pstore_type_id type)
109f0f23e54SJoel Fernandes (Google) {
110f0f23e54SJoel Fernandes (Google) 	BUILD_BUG_ON(ARRAY_SIZE(pstore_type_names) != PSTORE_TYPE_MAX);
111f0f23e54SJoel Fernandes (Google) 
112f0f23e54SJoel Fernandes (Google) 	if (WARN_ON_ONCE(type >= PSTORE_TYPE_MAX))
113f0f23e54SJoel Fernandes (Google) 		return "unknown";
114f0f23e54SJoel Fernandes (Google) 
115f0f23e54SJoel Fernandes (Google) 	return pstore_type_names[type];
116f0f23e54SJoel Fernandes (Google) }
117f0f23e54SJoel Fernandes (Google) EXPORT_SYMBOL_GPL(pstore_type_to_name);
118f0f23e54SJoel Fernandes (Google) 
119f0f23e54SJoel Fernandes (Google) enum pstore_type_id pstore_name_to_type(const char *name)
120f0f23e54SJoel Fernandes (Google) {
121f0f23e54SJoel Fernandes (Google) 	int i;
122f0f23e54SJoel Fernandes (Google) 
123f0f23e54SJoel Fernandes (Google) 	for (i = 0; i < PSTORE_TYPE_MAX; i++) {
124f0f23e54SJoel Fernandes (Google) 		if (!strcmp(pstore_type_names[i], name))
125f0f23e54SJoel Fernandes (Google) 			return i;
126f0f23e54SJoel Fernandes (Google) 	}
127f0f23e54SJoel Fernandes (Google) 
128f0f23e54SJoel Fernandes (Google) 	return PSTORE_TYPE_MAX;
129f0f23e54SJoel Fernandes (Google) }
130f0f23e54SJoel Fernandes (Google) EXPORT_SYMBOL_GPL(pstore_name_to_type);
131f0f23e54SJoel Fernandes (Google) 
132381b872cSSeiji Aguchi static const char *get_reason_str(enum kmsg_dump_reason reason)
133381b872cSSeiji Aguchi {
134381b872cSSeiji Aguchi 	switch (reason) {
135381b872cSSeiji Aguchi 	case KMSG_DUMP_PANIC:
136381b872cSSeiji Aguchi 		return "Panic";
137381b872cSSeiji Aguchi 	case KMSG_DUMP_OOPS:
138381b872cSSeiji Aguchi 		return "Oops";
139381b872cSSeiji Aguchi 	case KMSG_DUMP_EMERG:
140381b872cSSeiji Aguchi 		return "Emergency";
141381b872cSSeiji Aguchi 	case KMSG_DUMP_RESTART:
142381b872cSSeiji Aguchi 		return "Restart";
143381b872cSSeiji Aguchi 	case KMSG_DUMP_HALT:
144381b872cSSeiji Aguchi 		return "Halt";
145381b872cSSeiji Aguchi 	case KMSG_DUMP_POWEROFF:
146381b872cSSeiji Aguchi 		return "Poweroff";
147381b872cSSeiji Aguchi 	default:
148381b872cSSeiji Aguchi 		return "Unknown";
149381b872cSSeiji Aguchi 	}
150381b872cSSeiji Aguchi }
1519f6af27fSTony Luck 
1529f244e9cSSeiji Aguchi /*
153ea84b580SKees Cook  * Should pstore_dump() wait for a concurrent pstore_dump()? If
154ea84b580SKees Cook  * not, the current pstore_dump() will report a failure to dump
155ea84b580SKees Cook  * and return.
1569f244e9cSSeiji Aguchi  */
157ea84b580SKees Cook static bool pstore_cannot_wait(enum kmsg_dump_reason reason)
158ea84b580SKees Cook {
159ea84b580SKees Cook 	/* In NMI path, pstore shouldn't block regardless of reason. */
1609f244e9cSSeiji Aguchi 	if (in_nmi())
1619f244e9cSSeiji Aguchi 		return true;
1629f244e9cSSeiji Aguchi 
1639f244e9cSSeiji Aguchi 	switch (reason) {
1649f244e9cSSeiji Aguchi 	/* In panic case, other cpus are stopped by smp_send_stop(). */
1659f244e9cSSeiji Aguchi 	case KMSG_DUMP_PANIC:
166ea84b580SKees Cook 	/* Emergency restart shouldn't be blocked. */
1679f244e9cSSeiji Aguchi 	case KMSG_DUMP_EMERG:
1689f244e9cSSeiji Aguchi 		return true;
1699f244e9cSSeiji Aguchi 	default:
1709f244e9cSSeiji Aguchi 		return false;
1719f244e9cSSeiji Aguchi 	}
1729f244e9cSSeiji Aguchi }
1739f244e9cSSeiji Aguchi 
17458eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
175cb3bee03SGeliang Tang static int zbufsize_deflate(size_t size)
176b0aad7a9SAruna Balakrishnaiah {
1777de8fe2fSAruna Balakrishnaiah 	size_t cmpr;
178b0aad7a9SAruna Balakrishnaiah 
179cb3bee03SGeliang Tang 	switch (size) {
1807de8fe2fSAruna Balakrishnaiah 	/* buffer range for efivars */
1817de8fe2fSAruna Balakrishnaiah 	case 1000 ... 2000:
1827de8fe2fSAruna Balakrishnaiah 		cmpr = 56;
1837de8fe2fSAruna Balakrishnaiah 		break;
1847de8fe2fSAruna Balakrishnaiah 	case 2001 ... 3000:
1857de8fe2fSAruna Balakrishnaiah 		cmpr = 54;
1867de8fe2fSAruna Balakrishnaiah 		break;
1877de8fe2fSAruna Balakrishnaiah 	case 3001 ... 3999:
1887de8fe2fSAruna Balakrishnaiah 		cmpr = 52;
1897de8fe2fSAruna Balakrishnaiah 		break;
1907de8fe2fSAruna Balakrishnaiah 	/* buffer range for nvram, erst */
1917de8fe2fSAruna Balakrishnaiah 	case 4000 ... 10000:
1927de8fe2fSAruna Balakrishnaiah 		cmpr = 45;
1937de8fe2fSAruna Balakrishnaiah 		break;
1947de8fe2fSAruna Balakrishnaiah 	default:
1957de8fe2fSAruna Balakrishnaiah 		cmpr = 60;
1967de8fe2fSAruna Balakrishnaiah 		break;
1977de8fe2fSAruna Balakrishnaiah 	}
1987de8fe2fSAruna Balakrishnaiah 
199cb3bee03SGeliang Tang 	return (size * 100) / cmpr;
2008cfc8ddcSGeliang Tang }
2018cfc8ddcSGeliang Tang #endif
2028cfc8ddcSGeliang Tang 
20358eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
204cb3bee03SGeliang Tang static int zbufsize_lzo(size_t size)
2058cfc8ddcSGeliang Tang {
206cb3bee03SGeliang Tang 	return lzo1x_worst_compress(size);
2078cfc8ddcSGeliang Tang }
2088cfc8ddcSGeliang Tang #endif
2098cfc8ddcSGeliang Tang 
21058eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
211cb3bee03SGeliang Tang static int zbufsize_lz4(size_t size)
2128cfc8ddcSGeliang Tang {
213cb3bee03SGeliang Tang 	return LZ4_compressBound(size);
214239b7161SGeliang Tang }
215239b7161SGeliang Tang #endif
216239b7161SGeliang Tang 
21758eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
218cb3bee03SGeliang Tang static int zbufsize_842(size_t size)
219239b7161SGeliang Tang {
22055597406SKees Cook 	return size;
221239b7161SGeliang Tang }
222fe1d4758SKees Cook #endif
2238cfc8ddcSGeliang Tang 
2241021bcf4SGeliang Tang #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
2251021bcf4SGeliang Tang static int zbufsize_zstd(size_t size)
2261021bcf4SGeliang Tang {
2271021bcf4SGeliang Tang 	return ZSTD_compressBound(size);
2281021bcf4SGeliang Tang }
2291021bcf4SGeliang Tang #endif
2301021bcf4SGeliang Tang 
231fe1d4758SKees Cook static const struct pstore_zbackend *zbackend __ro_after_init;
232fe1d4758SKees Cook 
233fe1d4758SKees Cook static const struct pstore_zbackend zbackends[] = {
23458eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
235fe1d4758SKees Cook 	{
236cb3bee03SGeliang Tang 		.zbufsize	= zbufsize_deflate,
237cb3bee03SGeliang Tang 		.name		= "deflate",
238fe1d4758SKees Cook 	},
239fe1d4758SKees Cook #endif
24058eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
241fe1d4758SKees Cook 	{
242cb3bee03SGeliang Tang 		.zbufsize	= zbufsize_lzo,
243fe1d4758SKees Cook 		.name		= "lzo",
244fe1d4758SKees Cook 	},
245fe1d4758SKees Cook #endif
24658eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS)
247fe1d4758SKees Cook 	{
248cb3bee03SGeliang Tang 		.zbufsize	= zbufsize_lz4,
249fe1d4758SKees Cook 		.name		= "lz4",
250fe1d4758SKees Cook 	},
251fe1d4758SKees Cook #endif
25258eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
253fe1d4758SKees Cook 	{
254cb3bee03SGeliang Tang 		.zbufsize	= zbufsize_lz4,
255fe1d4758SKees Cook 		.name		= "lz4hc",
256fe1d4758SKees Cook 	},
257fe1d4758SKees Cook #endif
25858eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
259fe1d4758SKees Cook 	{
260cb3bee03SGeliang Tang 		.zbufsize	= zbufsize_842,
261239b7161SGeliang Tang 		.name		= "842",
262fe1d4758SKees Cook 	},
263fe1d4758SKees Cook #endif
2641021bcf4SGeliang Tang #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
2651021bcf4SGeliang Tang 	{
2661021bcf4SGeliang Tang 		.zbufsize	= zbufsize_zstd,
2671021bcf4SGeliang Tang 		.name		= "zstd",
2681021bcf4SGeliang Tang 	},
2691021bcf4SGeliang Tang #endif
270fe1d4758SKees Cook 	{ }
2718cfc8ddcSGeliang Tang };
2728cfc8ddcSGeliang Tang 
2738cfc8ddcSGeliang Tang static int pstore_compress(const void *in, void *out,
274cb3bee03SGeliang Tang 			   unsigned int inlen, unsigned int outlen)
2758cfc8ddcSGeliang Tang {
276cb3bee03SGeliang Tang 	int ret;
277cb3bee03SGeliang Tang 
278cb3bee03SGeliang Tang 	ret = crypto_comp_compress(tfm, in, inlen, out, &outlen);
279cb3bee03SGeliang Tang 	if (ret) {
280cb3bee03SGeliang Tang 		pr_err("crypto_comp_compress failed, ret = %d!\n", ret);
281cb3bee03SGeliang Tang 		return ret;
2828cfc8ddcSGeliang Tang 	}
2838cfc8ddcSGeliang Tang 
284cb3bee03SGeliang Tang 	return outlen;
285cb3bee03SGeliang Tang }
286cb3bee03SGeliang Tang 
2878cfc8ddcSGeliang Tang static void allocate_buf_for_compression(void)
2888cfc8ddcSGeliang Tang {
28995047b05SKees Cook 	struct crypto_comp *ctx;
29095047b05SKees Cook 	int size;
29195047b05SKees Cook 	char *buf;
29295047b05SKees Cook 
29395047b05SKees Cook 	/* Skip if not built-in or compression backend not selected yet. */
294e698aaf3STobias Regnery 	if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !zbackend)
295cb3bee03SGeliang Tang 		return;
296cb3bee03SGeliang Tang 
29795047b05SKees Cook 	/* Skip if no pstore backend yet or compression init already done. */
29895047b05SKees Cook 	if (!psinfo || tfm)
29995047b05SKees Cook 		return;
30095047b05SKees Cook 
301cb3bee03SGeliang Tang 	if (!crypto_has_comp(zbackend->name, 0, 0)) {
30295047b05SKees Cook 		pr_err("Unknown compression: %s\n", zbackend->name);
303cb3bee03SGeliang Tang 		return;
304cb3bee03SGeliang Tang 	}
305cb3bee03SGeliang Tang 
30695047b05SKees Cook 	size = zbackend->zbufsize(psinfo->bufsize);
30795047b05SKees Cook 	if (size <= 0) {
30895047b05SKees Cook 		pr_err("Invalid compression size for %s: %d\n",
30995047b05SKees Cook 		       zbackend->name, size);
310cb3bee03SGeliang Tang 		return;
311cb3bee03SGeliang Tang 	}
312cb3bee03SGeliang Tang 
31395047b05SKees Cook 	buf = kmalloc(size, GFP_KERNEL);
31495047b05SKees Cook 	if (!buf) {
31595047b05SKees Cook 		pr_err("Failed %d byte compression buffer allocation for: %s\n",
31695047b05SKees Cook 		       size, zbackend->name);
317cb3bee03SGeliang Tang 		return;
3188cfc8ddcSGeliang Tang 	}
31995047b05SKees Cook 
32095047b05SKees Cook 	ctx = crypto_alloc_comp(zbackend->name, 0, 0);
32195047b05SKees Cook 	if (IS_ERR_OR_NULL(ctx)) {
32295047b05SKees Cook 		kfree(buf);
32395047b05SKees Cook 		pr_err("crypto_alloc_comp('%s') failed: %ld\n", zbackend->name,
32495047b05SKees Cook 		       PTR_ERR(ctx));
32595047b05SKees Cook 		return;
32695047b05SKees Cook 	}
32795047b05SKees Cook 
32895047b05SKees Cook 	/* A non-NULL big_oops_buf indicates compression is available. */
32995047b05SKees Cook 	tfm = ctx;
33095047b05SKees Cook 	big_oops_buf_sz = size;
33195047b05SKees Cook 	big_oops_buf = buf;
33295047b05SKees Cook 
3330eed84ffSKees Cook 	pr_info("Using crash dump compression: %s\n", zbackend->name);
3348cfc8ddcSGeliang Tang }
3358cfc8ddcSGeliang Tang 
3368cfc8ddcSGeliang Tang static void free_buf_for_compression(void)
3378cfc8ddcSGeliang Tang {
338a9fb94a9SPi-Hsun Shih 	if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && tfm) {
339cb3bee03SGeliang Tang 		crypto_free_comp(tfm);
340a9fb94a9SPi-Hsun Shih 		tfm = NULL;
341a9fb94a9SPi-Hsun Shih 	}
342cb3bee03SGeliang Tang 	kfree(big_oops_buf);
343cb3bee03SGeliang Tang 	big_oops_buf = NULL;
344cb3bee03SGeliang Tang 	big_oops_buf_sz = 0;
345ee1d2674SGeliang Tang }
346ee1d2674SGeliang Tang 
347b0aad7a9SAruna Balakrishnaiah /*
348b0aad7a9SAruna Balakrishnaiah  * Called when compression fails, since the printk buffer
349b0aad7a9SAruna Balakrishnaiah  * would be fetched for compression calling it again when
350b0aad7a9SAruna Balakrishnaiah  * compression fails would have moved the iterator of
351b0aad7a9SAruna Balakrishnaiah  * printk buffer which results in fetching old contents.
352b0aad7a9SAruna Balakrishnaiah  * Copy the recent messages from big_oops_buf to psinfo->buf
353b0aad7a9SAruna Balakrishnaiah  */
354b0aad7a9SAruna Balakrishnaiah static size_t copy_kmsg_to_buffer(int hsize, size_t len)
355b0aad7a9SAruna Balakrishnaiah {
356b0aad7a9SAruna Balakrishnaiah 	size_t total_len;
357b0aad7a9SAruna Balakrishnaiah 	size_t diff;
358b0aad7a9SAruna Balakrishnaiah 
359b0aad7a9SAruna Balakrishnaiah 	total_len = hsize + len;
360b0aad7a9SAruna Balakrishnaiah 
361b0aad7a9SAruna Balakrishnaiah 	if (total_len > psinfo->bufsize) {
362b0aad7a9SAruna Balakrishnaiah 		diff = total_len - psinfo->bufsize + hsize;
363b0aad7a9SAruna Balakrishnaiah 		memcpy(psinfo->buf, big_oops_buf, hsize);
364b0aad7a9SAruna Balakrishnaiah 		memcpy(psinfo->buf + hsize, big_oops_buf + diff,
365b0aad7a9SAruna Balakrishnaiah 					psinfo->bufsize - hsize);
366b0aad7a9SAruna Balakrishnaiah 		total_len = psinfo->bufsize;
367b0aad7a9SAruna Balakrishnaiah 	} else
368b0aad7a9SAruna Balakrishnaiah 		memcpy(psinfo->buf, big_oops_buf, total_len);
369b0aad7a9SAruna Balakrishnaiah 
370b0aad7a9SAruna Balakrishnaiah 	return total_len;
371b0aad7a9SAruna Balakrishnaiah }
372b0aad7a9SAruna Balakrishnaiah 
373e581ca81SKees Cook void pstore_record_init(struct pstore_record *record,
374e581ca81SKees Cook 			struct pstore_info *psinfo)
375e581ca81SKees Cook {
376e581ca81SKees Cook 	memset(record, 0, sizeof(*record));
377e581ca81SKees Cook 
378e581ca81SKees Cook 	record->psi = psinfo;
379c7f3c595SKees Cook 
380c7f3c595SKees Cook 	/* Report zeroed timestamp if called before timekeeping has resumed. */
3817aaa822eSKees Cook 	record->time = ns_to_timespec64(ktime_get_real_fast_ns());
382e581ca81SKees Cook }
383e581ca81SKees Cook 
384ca01d6ddSTony Luck /*
3850eed84ffSKees Cook  * callback from kmsg_dump. Save as much as we can (up to kmsg_bytes) from the
3860eed84ffSKees Cook  * end of the buffer.
387ca01d6ddSTony Luck  */
388ca01d6ddSTony Luck static void pstore_dump(struct kmsg_dumper *dumper,
389e2ae715dSKay Sievers 			enum kmsg_dump_reason reason)
390ca01d6ddSTony Luck {
391e2ae715dSKay Sievers 	unsigned long	total = 0;
392381b872cSSeiji Aguchi 	const char	*why;
393b94fdd07SMatthew Garrett 	unsigned int	part = 1;
394e2ae715dSKay Sievers 	int		ret;
395ca01d6ddSTony Luck 
396381b872cSSeiji Aguchi 	why = get_reason_str(reason);
3979f6af27fSTony Luck 
398ea84b580SKees Cook 	if (down_trylock(&psinfo->buf_lock)) {
399ea84b580SKees Cook 		/* Failed to acquire lock: give up if we cannot wait. */
400ea84b580SKees Cook 		if (pstore_cannot_wait(reason)) {
401ea84b580SKees Cook 			pr_err("dump skipped in %s path: may corrupt error record\n",
402ea84b580SKees Cook 				in_nmi() ? "NMI" : why);
403959217c8SLi Pengcheng 			return;
4049f244e9cSSeiji Aguchi 		}
405ea84b580SKees Cook 		if (down_interruptible(&psinfo->buf_lock)) {
406ea84b580SKees Cook 			pr_err("could not grab semaphore?!\n");
407ea84b580SKees Cook 			return;
40898e44fdaSNamhyung Kim 		}
409ea84b580SKees Cook 	}
410ea84b580SKees Cook 
411ca01d6ddSTony Luck 	oopscount++;
412ca01d6ddSTony Luck 	while (total < kmsg_bytes) {
413e2ae715dSKay Sievers 		char *dst;
41476cc9580SKees Cook 		size_t dst_size;
41576cc9580SKees Cook 		int header_size;
416b0aad7a9SAruna Balakrishnaiah 		int zipped_len = -1;
41776cc9580SKees Cook 		size_t dump_size;
418e581ca81SKees Cook 		struct pstore_record record;
419e581ca81SKees Cook 
420e581ca81SKees Cook 		pstore_record_init(&record, psinfo);
421e581ca81SKees Cook 		record.type = PSTORE_TYPE_DMESG;
422e581ca81SKees Cook 		record.count = oopscount;
423e581ca81SKees Cook 		record.reason = reason;
424e581ca81SKees Cook 		record.part = part;
425e581ca81SKees Cook 		record.buf = psinfo->buf;
426e2ae715dSKay Sievers 
427ea84b580SKees Cook 		if (big_oops_buf) {
428b0aad7a9SAruna Balakrishnaiah 			dst = big_oops_buf;
42976cc9580SKees Cook 			dst_size = big_oops_buf_sz;
430235f6d15SNamhyung Kim 		} else {
431235f6d15SNamhyung Kim 			dst = psinfo->buf;
43276cc9580SKees Cook 			dst_size = psinfo->bufsize;
433235f6d15SNamhyung Kim 		}
434235f6d15SNamhyung Kim 
43576cc9580SKees Cook 		/* Write dump header. */
43676cc9580SKees Cook 		header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why,
43776cc9580SKees Cook 				 oopscount, part);
43876cc9580SKees Cook 		dst_size -= header_size;
439b0aad7a9SAruna Balakrishnaiah 
44076cc9580SKees Cook 		/* Write dump contents. */
44176cc9580SKees Cook 		if (!kmsg_dump_get_buffer(dumper, true, dst + header_size,
44276cc9580SKees Cook 					  dst_size, &dump_size))
443b0aad7a9SAruna Balakrishnaiah 			break;
444b0aad7a9SAruna Balakrishnaiah 
445ea84b580SKees Cook 		if (big_oops_buf) {
446b0aad7a9SAruna Balakrishnaiah 			zipped_len = pstore_compress(dst, psinfo->buf,
44776cc9580SKees Cook 						header_size + dump_size,
44876cc9580SKees Cook 						psinfo->bufsize);
449b0aad7a9SAruna Balakrishnaiah 
450b0aad7a9SAruna Balakrishnaiah 			if (zipped_len > 0) {
45176cc9580SKees Cook 				record.compressed = true;
45276cc9580SKees Cook 				record.size = zipped_len;
453b0aad7a9SAruna Balakrishnaiah 			} else {
45476cc9580SKees Cook 				record.size = copy_kmsg_to_buffer(header_size,
45576cc9580SKees Cook 								  dump_size);
456b0aad7a9SAruna Balakrishnaiah 			}
457b0aad7a9SAruna Balakrishnaiah 		} else {
45876cc9580SKees Cook 			record.size = header_size + dump_size;
459b0aad7a9SAruna Balakrishnaiah 		}
460b0aad7a9SAruna Balakrishnaiah 
46176cc9580SKees Cook 		ret = psinfo->write(&record);
462b238b8faSChen Gong 		if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted())
4636dda9266SLuck, Tony 			pstore_new_entry = 1;
464e2ae715dSKay Sievers 
46576cc9580SKees Cook 		total += record.size;
46656280682SMatthew Garrett 		part++;
467ca01d6ddSTony Luck 	}
468ea84b580SKees Cook 
469ea84b580SKees Cook 	up(&psinfo->buf_lock);
470ca01d6ddSTony Luck }
471ca01d6ddSTony Luck 
472ca01d6ddSTony Luck static struct kmsg_dumper pstore_dumper = {
473ca01d6ddSTony Luck 	.dump = pstore_dump,
474ca01d6ddSTony Luck };
475ca01d6ddSTony Luck 
476306e5c2aSGeliang Tang /*
477306e5c2aSGeliang Tang  * Register with kmsg_dump to save last part of console log on panic.
478306e5c2aSGeliang Tang  */
47918730411SGeliang Tang static void pstore_register_kmsg(void)
48018730411SGeliang Tang {
48118730411SGeliang Tang 	kmsg_dump_register(&pstore_dumper);
48218730411SGeliang Tang }
48318730411SGeliang Tang 
484ee1d2674SGeliang Tang static void pstore_unregister_kmsg(void)
485ee1d2674SGeliang Tang {
486ee1d2674SGeliang Tang 	kmsg_dump_unregister(&pstore_dumper);
487ee1d2674SGeliang Tang }
488ee1d2674SGeliang Tang 
489f29e5956SAnton Vorontsov #ifdef CONFIG_PSTORE_CONSOLE
490f29e5956SAnton Vorontsov static void pstore_console_write(struct console *con, const char *s, unsigned c)
491f29e5956SAnton Vorontsov {
492e581ca81SKees Cook 	struct pstore_record record;
493f29e5956SAnton Vorontsov 
4944c6c4d34SYue Hu 	if (!c)
4954c6c4d34SYue Hu 		return;
4964c6c4d34SYue Hu 
497e581ca81SKees Cook 	pstore_record_init(&record, psinfo);
498e581ca81SKees Cook 	record.type = PSTORE_TYPE_CONSOLE;
499e581ca81SKees Cook 
500b10b4711SKees Cook 	record.buf = (char *)s;
501b10b4711SKees Cook 	record.size = c;
5024c9ec219SKees Cook 	psinfo->write(&record);
503f29e5956SAnton Vorontsov }
504f29e5956SAnton Vorontsov 
505f29e5956SAnton Vorontsov static struct console pstore_console = {
506f29e5956SAnton Vorontsov 	.name	= "pstore",
507f29e5956SAnton Vorontsov 	.write	= pstore_console_write,
508f29e5956SAnton Vorontsov 	.flags	= CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME,
509f29e5956SAnton Vorontsov 	.index	= -1,
510f29e5956SAnton Vorontsov };
511f29e5956SAnton Vorontsov 
512f29e5956SAnton Vorontsov static void pstore_register_console(void)
513f29e5956SAnton Vorontsov {
514f29e5956SAnton Vorontsov 	register_console(&pstore_console);
515f29e5956SAnton Vorontsov }
516ee1d2674SGeliang Tang 
517ee1d2674SGeliang Tang static void pstore_unregister_console(void)
518ee1d2674SGeliang Tang {
519ee1d2674SGeliang Tang 	unregister_console(&pstore_console);
520ee1d2674SGeliang Tang }
521f29e5956SAnton Vorontsov #else
522f29e5956SAnton Vorontsov static void pstore_register_console(void) {}
523ee1d2674SGeliang Tang static void pstore_unregister_console(void) {}
524f29e5956SAnton Vorontsov #endif
525f29e5956SAnton Vorontsov 
5264c9ec219SKees Cook static int pstore_write_user_compat(struct pstore_record *record,
527fdd03118SKees Cook 				    const char __user *buf)
5285bf6d1b9SMark Salyzyn {
52930800d99SKees Cook 	int ret = 0;
5305bf6d1b9SMark Salyzyn 
53130800d99SKees Cook 	if (record->buf)
53230800d99SKees Cook 		return -EINVAL;
5335bf6d1b9SMark Salyzyn 
534077090afSGeliang Tang 	record->buf = memdup_user(buf, record->size);
535dfd6fa39SHirofumi Nakagawa 	if (IS_ERR(record->buf)) {
536077090afSGeliang Tang 		ret = PTR_ERR(record->buf);
53730800d99SKees Cook 		goto out;
5385bf6d1b9SMark Salyzyn 	}
53930800d99SKees Cook 
5404c9ec219SKees Cook 	ret = record->psi->write(record);
54130800d99SKees Cook 
54230800d99SKees Cook 	kfree(record->buf);
543077090afSGeliang Tang out:
54430800d99SKees Cook 	record->buf = NULL;
54530800d99SKees Cook 
54630800d99SKees Cook 	return unlikely(ret < 0) ? ret : record->size;
5475bf6d1b9SMark Salyzyn }
5485bf6d1b9SMark Salyzyn 
549ca01d6ddSTony Luck /*
550ca01d6ddSTony Luck  * platform specific persistent storage driver registers with
551ca01d6ddSTony Luck  * us here. If pstore is already mounted, call the platform
552ca01d6ddSTony Luck  * read function right away to populate the file system. If not
553ca01d6ddSTony Luck  * then the pstore mount code will call us later to fill out
554ca01d6ddSTony Luck  * the file system.
555ca01d6ddSTony Luck  */
556ca01d6ddSTony Luck int pstore_register(struct pstore_info *psi)
557ca01d6ddSTony Luck {
5580d7cd09aSKees Cook 	if (backend && strcmp(backend, psi->name)) {
5590d7cd09aSKees Cook 		pr_warn("ignoring unexpected backend '%s'\n", psi->name);
5608e48b1a8SLenny Szubowicz 		return -EPERM;
5610d7cd09aSKees Cook 	}
5628e48b1a8SLenny Szubowicz 
5634c9ec219SKees Cook 	/* Sanity check flags. */
5644c9ec219SKees Cook 	if (!psi->flags) {
5654c9ec219SKees Cook 		pr_warn("backend '%s' must support at least one frontend\n",
5664c9ec219SKees Cook 			psi->name);
5674c9ec219SKees Cook 		return -EINVAL;
5684c9ec219SKees Cook 	}
5694c9ec219SKees Cook 
5704c9ec219SKees Cook 	/* Check for required functions. */
5714c9ec219SKees Cook 	if (!psi->read || !psi->write) {
5724c9ec219SKees Cook 		pr_warn("backend '%s' must implement read() and write()\n",
5734c9ec219SKees Cook 			psi->name);
5744c9ec219SKees Cook 		return -EINVAL;
5754c9ec219SKees Cook 	}
5764c9ec219SKees Cook 
577*cab12fd0SKees Cook 	mutex_lock(&psinfo_lock);
578ca01d6ddSTony Luck 	if (psinfo) {
5790d7cd09aSKees Cook 		pr_warn("backend '%s' already loaded: ignoring '%s'\n",
5800d7cd09aSKees Cook 			psinfo->name, psi->name);
581*cab12fd0SKees Cook 		mutex_unlock(&psinfo_lock);
582ca01d6ddSTony Luck 		return -EBUSY;
583ca01d6ddSTony Luck 	}
584dee28e72SMatthew Garrett 
5854c9ec219SKees Cook 	if (!psi->write_user)
5864c9ec219SKees Cook 		psi->write_user = pstore_write_user_compat;
587ca01d6ddSTony Luck 	psinfo = psi;
588f6f82851SKees Cook 	mutex_init(&psinfo->read_mutex);
589ea84b580SKees Cook 	sema_init(&psinfo->buf_lock, 1);
590*cab12fd0SKees Cook 	mutex_unlock(&psinfo_lock);
591ca01d6ddSTony Luck 
592ca01d6ddSTony Luck 
5938880fa32SKees Cook 	if (psi->flags & PSTORE_FLAGS_DMESG)
594b0aad7a9SAruna Balakrishnaiah 		allocate_buf_for_compression();
595b0aad7a9SAruna Balakrishnaiah 
596ca01d6ddSTony Luck 	if (pstore_is_mounted())
5976dda9266SLuck, Tony 		pstore_get_records(0);
598ca01d6ddSTony Luck 
599c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_DMESG)
60018730411SGeliang Tang 		pstore_register_kmsg();
601c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_CONSOLE)
602f29e5956SAnton Vorontsov 		pstore_register_console();
603c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_FTRACE)
60465f8c95eSAnton Vorontsov 		pstore_register_ftrace();
605c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_PMSG)
6069d5438f4SMark Salyzyn 		pstore_register_pmsg();
607ca01d6ddSTony Luck 
6086330d553SKees Cook 	/* Start watching for new records, if desired. */
609a3f5f075SAnton Vorontsov 	if (pstore_update_ms >= 0) {
610a3f5f075SAnton Vorontsov 		pstore_timer.expires = jiffies +
611a3f5f075SAnton Vorontsov 			msecs_to_jiffies(pstore_update_ms);
6126dda9266SLuck, Tony 		add_timer(&pstore_timer);
613a3f5f075SAnton Vorontsov 	}
6146dda9266SLuck, Tony 
61542222c2aSWang Long 	/*
61642222c2aSWang Long 	 * Update the module parameter backend, so it is visible
61742222c2aSWang Long 	 * through /sys/module/pstore/parameters/backend
61842222c2aSWang Long 	 */
61942222c2aSWang Long 	backend = psi->name;
62042222c2aSWang Long 
621ef748853SFabian Frederick 	pr_info("Registered %s as persistent store backend\n", psi->name);
6228e48b1a8SLenny Szubowicz 
623ca01d6ddSTony Luck 	return 0;
624ca01d6ddSTony Luck }
625ca01d6ddSTony Luck EXPORT_SYMBOL_GPL(pstore_register);
626ca01d6ddSTony Luck 
627ee1d2674SGeliang Tang void pstore_unregister(struct pstore_info *psi)
628ee1d2674SGeliang Tang {
6296330d553SKees Cook 	/* Stop timer and make sure all work has finished. */
6306330d553SKees Cook 	pstore_update_ms = -1;
6316330d553SKees Cook 	del_timer_sync(&pstore_timer);
6326330d553SKees Cook 	flush_work(&pstore_work);
6336330d553SKees Cook 
634c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_PMSG)
635ee1d2674SGeliang Tang 		pstore_unregister_pmsg();
636c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_FTRACE)
637ee1d2674SGeliang Tang 		pstore_unregister_ftrace();
638c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_CONSOLE)
639ee1d2674SGeliang Tang 		pstore_unregister_console();
640c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_DMESG)
641ee1d2674SGeliang Tang 		pstore_unregister_kmsg();
642ee1d2674SGeliang Tang 
643ee1d2674SGeliang Tang 	free_buf_for_compression();
644ee1d2674SGeliang Tang 
645ee1d2674SGeliang Tang 	psinfo = NULL;
646ee1d2674SGeliang Tang 	backend = NULL;
647ee1d2674SGeliang Tang }
648ee1d2674SGeliang Tang EXPORT_SYMBOL_GPL(pstore_unregister);
649ee1d2674SGeliang Tang 
650634f8f51SKees Cook static void decompress_record(struct pstore_record *record)
651634f8f51SKees Cook {
652bdabc8e7SKees Cook 	int ret;
653634f8f51SKees Cook 	int unzipped_len;
654bdabc8e7SKees Cook 	char *unzipped, *workspace;
655634f8f51SKees Cook 
6564a16d1cbSAnkit Kumar 	if (!record->compressed)
6574a16d1cbSAnkit Kumar 		return;
6584a16d1cbSAnkit Kumar 
659634f8f51SKees Cook 	/* Only PSTORE_TYPE_DMESG support compression. */
6604a16d1cbSAnkit Kumar 	if (record->type != PSTORE_TYPE_DMESG) {
661634f8f51SKees Cook 		pr_warn("ignored compressed record type %d\n", record->type);
662634f8f51SKees Cook 		return;
663634f8f51SKees Cook 	}
664634f8f51SKees Cook 
665bdabc8e7SKees Cook 	/* Missing compression buffer means compression was not initialized. */
666634f8f51SKees Cook 	if (!big_oops_buf) {
667bdabc8e7SKees Cook 		pr_warn("no decompression method initialized!\n");
668634f8f51SKees Cook 		return;
669634f8f51SKees Cook 	}
670634f8f51SKees Cook 
671bdabc8e7SKees Cook 	/* Allocate enough space to hold max decompression and ECC. */
672bdabc8e7SKees Cook 	unzipped_len = big_oops_buf_sz;
673bdabc8e7SKees Cook 	workspace = kmalloc(unzipped_len + record->ecc_notice_size,
6747e8cc8dcSKees Cook 			    GFP_KERNEL);
675bdabc8e7SKees Cook 	if (!workspace)
676bdabc8e7SKees Cook 		return;
677bdabc8e7SKees Cook 
678bdabc8e7SKees Cook 	/* After decompression "unzipped_len" is almost certainly smaller. */
679bdabc8e7SKees Cook 	ret = crypto_comp_decompress(tfm, record->buf, record->size,
680bdabc8e7SKees Cook 					  workspace, &unzipped_len);
681bdabc8e7SKees Cook 	if (ret) {
682bdabc8e7SKees Cook 		pr_err("crypto_comp_decompress failed, ret = %d!\n", ret);
683bdabc8e7SKees Cook 		kfree(workspace);
6847e8cc8dcSKees Cook 		return;
6857e8cc8dcSKees Cook 	}
6867e8cc8dcSKees Cook 
6877e8cc8dcSKees Cook 	/* Append ECC notice to decompressed buffer. */
688bdabc8e7SKees Cook 	memcpy(workspace + unzipped_len, record->buf + record->size,
689634f8f51SKees Cook 	       record->ecc_notice_size);
6907e8cc8dcSKees Cook 
691bdabc8e7SKees Cook 	/* Copy decompressed contents into an minimum-sized allocation. */
692bdabc8e7SKees Cook 	unzipped = kmemdup(workspace, unzipped_len + record->ecc_notice_size,
693bdabc8e7SKees Cook 			   GFP_KERNEL);
694bdabc8e7SKees Cook 	kfree(workspace);
695bdabc8e7SKees Cook 	if (!unzipped)
696bdabc8e7SKees Cook 		return;
697bdabc8e7SKees Cook 
698bdabc8e7SKees Cook 	/* Swap out compressed contents with decompressed contents. */
699634f8f51SKees Cook 	kfree(record->buf);
700bdabc8e7SKees Cook 	record->buf = unzipped;
701634f8f51SKees Cook 	record->size = unzipped_len;
702634f8f51SKees Cook 	record->compressed = false;
703634f8f51SKees Cook }
704634f8f51SKees Cook 
705ca01d6ddSTony Luck /*
7063a7d2fd1SKees Cook  * Read all the records from one persistent store backend. Create
7076dda9266SLuck, Tony  * files in our filesystem.  Don't warn about -EEXIST errors
7086dda9266SLuck, Tony  * when we are re-scanning the backing store looking to add new
7096dda9266SLuck, Tony  * error records.
710ca01d6ddSTony Luck  */
7113a7d2fd1SKees Cook void pstore_get_backend_records(struct pstore_info *psi,
7123a7d2fd1SKees Cook 				struct dentry *root, int quiet)
713ca01d6ddSTony Luck {
7142a2b0acfSKees Cook 	int failed = 0;
715656de42eSKees Cook 	unsigned int stop_loop = 65536;
716ca01d6ddSTony Luck 
7173a7d2fd1SKees Cook 	if (!psi || !root)
718ca01d6ddSTony Luck 		return;
719ca01d6ddSTony Luck 
720f6f82851SKees Cook 	mutex_lock(&psi->read_mutex);
7212174f6dfSKees Cook 	if (psi->open && psi->open(psi))
72206cf91b4SChen Gong 		goto out;
72306cf91b4SChen Gong 
7241dfff7ddSKees Cook 	/*
7251dfff7ddSKees Cook 	 * Backend callback read() allocates record.buf. decompress_record()
7261dfff7ddSKees Cook 	 * may reallocate record.buf. On success, pstore_mkfile() will keep
7271dfff7ddSKees Cook 	 * the record.buf, so free it only on failure.
7281dfff7ddSKees Cook 	 */
729656de42eSKees Cook 	for (; stop_loop; stop_loop--) {
7302a2b0acfSKees Cook 		struct pstore_record *record;
7312a2b0acfSKees Cook 		int rc;
7322a2b0acfSKees Cook 
7332a2b0acfSKees Cook 		record = kzalloc(sizeof(*record), GFP_KERNEL);
7342a2b0acfSKees Cook 		if (!record) {
7352a2b0acfSKees Cook 			pr_err("out of memory creating record\n");
7362a2b0acfSKees Cook 			break;
7372a2b0acfSKees Cook 		}
738e581ca81SKees Cook 		pstore_record_init(record, psi);
7392a2b0acfSKees Cook 
7402a2b0acfSKees Cook 		record->size = psi->read(record);
7412a2b0acfSKees Cook 
7422a2b0acfSKees Cook 		/* No more records left in backend? */
743f6525b96SDouglas Anderson 		if (record->size <= 0) {
744f6525b96SDouglas Anderson 			kfree(record);
7452a2b0acfSKees Cook 			break;
746f6525b96SDouglas Anderson 		}
7472a2b0acfSKees Cook 
7482a2b0acfSKees Cook 		decompress_record(record);
7493a7d2fd1SKees Cook 		rc = pstore_mkfile(root, record);
7501dfff7ddSKees Cook 		if (rc) {
75183f70f07SKees Cook 			/* pstore_mkfile() did not take record, so free it. */
7522a2b0acfSKees Cook 			kfree(record->buf);
75383f70f07SKees Cook 			kfree(record);
7541dfff7ddSKees Cook 			if (rc != -EEXIST || !quiet)
7551dfff7ddSKees Cook 				failed++;
7561dfff7ddSKees Cook 		}
757ca01d6ddSTony Luck 	}
7582174f6dfSKees Cook 	if (psi->close)
75906cf91b4SChen Gong 		psi->close(psi);
76006cf91b4SChen Gong out:
761f6f82851SKees Cook 	mutex_unlock(&psi->read_mutex);
762ca01d6ddSTony Luck 
763ca01d6ddSTony Luck 	if (failed)
764656de42eSKees Cook 		pr_warn("failed to create %d record(s) from '%s'\n",
765ca01d6ddSTony Luck 			failed, psi->name);
766656de42eSKees Cook 	if (!stop_loop)
767656de42eSKees Cook 		pr_err("looping? Too many records seen from '%s'\n",
768656de42eSKees Cook 			psi->name);
769ca01d6ddSTony Luck }
770ca01d6ddSTony Luck 
7716dda9266SLuck, Tony static void pstore_dowork(struct work_struct *work)
7726dda9266SLuck, Tony {
7736dda9266SLuck, Tony 	pstore_get_records(1);
7746dda9266SLuck, Tony }
7756dda9266SLuck, Tony 
77624ed960aSKees Cook static void pstore_timefunc(struct timer_list *unused)
7776dda9266SLuck, Tony {
7786dda9266SLuck, Tony 	if (pstore_new_entry) {
7796dda9266SLuck, Tony 		pstore_new_entry = 0;
7806dda9266SLuck, Tony 		schedule_work(&pstore_work);
7816dda9266SLuck, Tony 	}
7826dda9266SLuck, Tony 
7836330d553SKees Cook 	if (pstore_update_ms >= 0)
7846330d553SKees Cook 		mod_timer(&pstore_timer,
7856330d553SKees Cook 			  jiffies + msecs_to_jiffies(pstore_update_ms));
7866dda9266SLuck, Tony }
7876dda9266SLuck, Tony 
7888d82cee2SBen Dooks (Codethink) static void __init pstore_choose_compression(void)
789fe1d4758SKees Cook {
790fe1d4758SKees Cook 	const struct pstore_zbackend *step;
791fe1d4758SKees Cook 
792fe1d4758SKees Cook 	if (!compress)
793fe1d4758SKees Cook 		return;
794fe1d4758SKees Cook 
795fe1d4758SKees Cook 	for (step = zbackends; step->name; step++) {
796fe1d4758SKees Cook 		if (!strcmp(compress, step->name)) {
797fe1d4758SKees Cook 			zbackend = step;
798fe1d4758SKees Cook 			return;
799fe1d4758SKees Cook 		}
800fe1d4758SKees Cook 	}
801fe1d4758SKees Cook }
802fe1d4758SKees Cook 
803cb095afdSKees Cook static int __init pstore_init(void)
804cb095afdSKees Cook {
805cb095afdSKees Cook 	int ret;
806cb095afdSKees Cook 
807cb095afdSKees Cook 	pstore_choose_compression();
808cb095afdSKees Cook 
80941603165SJoel Fernandes (Google) 	/*
81041603165SJoel Fernandes (Google) 	 * Check if any pstore backends registered earlier but did not
81141603165SJoel Fernandes (Google) 	 * initialize compression because crypto was not ready. If so,
81241603165SJoel Fernandes (Google) 	 * initialize compression now.
81341603165SJoel Fernandes (Google) 	 */
81441603165SJoel Fernandes (Google) 	allocate_buf_for_compression();
81541603165SJoel Fernandes (Google) 
816cb095afdSKees Cook 	ret = pstore_init_fs();
817cb095afdSKees Cook 	if (ret)
8188a57d6d4Schenqiwu 		free_buf_for_compression();
819cb095afdSKees Cook 
8208a57d6d4Schenqiwu 	return ret;
821cb095afdSKees Cook }
82241603165SJoel Fernandes (Google) late_initcall(pstore_init);
823cb095afdSKees Cook 
824cb095afdSKees Cook static void __exit pstore_exit(void)
825cb095afdSKees Cook {
826cb095afdSKees Cook 	pstore_exit_fs();
827cb095afdSKees Cook }
828cb095afdSKees Cook module_exit(pstore_exit)
829cb095afdSKees Cook 
830fe1d4758SKees Cook module_param(compress, charp, 0444);
831fe1d4758SKees Cook MODULE_PARM_DESC(compress, "Pstore compression to use");
832fe1d4758SKees Cook 
833dee28e72SMatthew Garrett module_param(backend, charp, 0444);
834dee28e72SMatthew Garrett MODULE_PARM_DESC(backend, "Pstore backend to use");
835cb095afdSKees Cook 
836cb095afdSKees Cook MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
837cb095afdSKees Cook MODULE_LICENSE("GPL");
838