xref: /openbmc/linux/fs/pstore/platform.c (revision d973f7d83dc7360597373536b34e80eea306d15f)
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;
80*d973f7d8SKees Cook module_param(backend, charp, 0444);
81*d973f7d8SKees Cook MODULE_PARM_DESC(backend, "specific backend to use");
82*d973f7d8SKees 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
89*d973f7d8SKees Cook module_param(compress, charp, 0444);
90*d973f7d8SKees 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) 
138381b872cSSeiji Aguchi static const char *get_reason_str(enum kmsg_dump_reason reason)
139381b872cSSeiji Aguchi {
140381b872cSSeiji Aguchi 	switch (reason) {
141381b872cSSeiji Aguchi 	case KMSG_DUMP_PANIC:
142381b872cSSeiji Aguchi 		return "Panic";
143381b872cSSeiji Aguchi 	case KMSG_DUMP_OOPS:
144381b872cSSeiji Aguchi 		return "Oops";
145381b872cSSeiji Aguchi 	case KMSG_DUMP_EMERG:
146381b872cSSeiji Aguchi 		return "Emergency";
147381b872cSSeiji Aguchi 	case KMSG_DUMP_RESTART:
148381b872cSSeiji Aguchi 		return "Restart";
149381b872cSSeiji Aguchi 	case KMSG_DUMP_HALT:
150381b872cSSeiji Aguchi 		return "Halt";
151381b872cSSeiji Aguchi 	case KMSG_DUMP_POWEROFF:
152381b872cSSeiji Aguchi 		return "Poweroff";
153381b872cSSeiji Aguchi 	default:
154381b872cSSeiji Aguchi 		return "Unknown";
155381b872cSSeiji Aguchi 	}
156381b872cSSeiji Aguchi }
1579f6af27fSTony Luck 
15878c83c82SKees Cook static void pstore_timer_kick(void)
15978c83c82SKees Cook {
16078c83c82SKees Cook 	if (pstore_update_ms < 0)
16178c83c82SKees Cook 		return;
16278c83c82SKees Cook 
16378c83c82SKees Cook 	mod_timer(&pstore_timer, jiffies + msecs_to_jiffies(pstore_update_ms));
16478c83c82SKees Cook }
16578c83c82SKees Cook 
1669f244e9cSSeiji Aguchi /*
167ea84b580SKees Cook  * Should pstore_dump() wait for a concurrent pstore_dump()? If
168ea84b580SKees Cook  * not, the current pstore_dump() will report a failure to dump
169ea84b580SKees Cook  * and return.
1709f244e9cSSeiji Aguchi  */
171ea84b580SKees Cook static bool pstore_cannot_wait(enum kmsg_dump_reason reason)
172ea84b580SKees Cook {
173ea84b580SKees Cook 	/* In NMI path, pstore shouldn't block regardless of reason. */
1749f244e9cSSeiji Aguchi 	if (in_nmi())
1759f244e9cSSeiji Aguchi 		return true;
1769f244e9cSSeiji Aguchi 
1779f244e9cSSeiji Aguchi 	switch (reason) {
1789f244e9cSSeiji Aguchi 	/* In panic case, other cpus are stopped by smp_send_stop(). */
1799f244e9cSSeiji Aguchi 	case KMSG_DUMP_PANIC:
180ea84b580SKees Cook 	/* Emergency restart shouldn't be blocked. */
1819f244e9cSSeiji Aguchi 	case KMSG_DUMP_EMERG:
1829f244e9cSSeiji Aguchi 		return true;
1839f244e9cSSeiji Aguchi 	default:
1849f244e9cSSeiji Aguchi 		return false;
1859f244e9cSSeiji Aguchi 	}
1869f244e9cSSeiji Aguchi }
1879f244e9cSSeiji Aguchi 
18858eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
189cb3bee03SGeliang Tang static int zbufsize_deflate(size_t size)
190b0aad7a9SAruna Balakrishnaiah {
1917de8fe2fSAruna Balakrishnaiah 	size_t cmpr;
192b0aad7a9SAruna Balakrishnaiah 
193cb3bee03SGeliang Tang 	switch (size) {
1947de8fe2fSAruna Balakrishnaiah 	/* buffer range for efivars */
1957de8fe2fSAruna Balakrishnaiah 	case 1000 ... 2000:
1967de8fe2fSAruna Balakrishnaiah 		cmpr = 56;
1977de8fe2fSAruna Balakrishnaiah 		break;
1987de8fe2fSAruna Balakrishnaiah 	case 2001 ... 3000:
1997de8fe2fSAruna Balakrishnaiah 		cmpr = 54;
2007de8fe2fSAruna Balakrishnaiah 		break;
2017de8fe2fSAruna Balakrishnaiah 	case 3001 ... 3999:
2027de8fe2fSAruna Balakrishnaiah 		cmpr = 52;
2037de8fe2fSAruna Balakrishnaiah 		break;
2047de8fe2fSAruna Balakrishnaiah 	/* buffer range for nvram, erst */
2057de8fe2fSAruna Balakrishnaiah 	case 4000 ... 10000:
2067de8fe2fSAruna Balakrishnaiah 		cmpr = 45;
2077de8fe2fSAruna Balakrishnaiah 		break;
2087de8fe2fSAruna Balakrishnaiah 	default:
2097de8fe2fSAruna Balakrishnaiah 		cmpr = 60;
2107de8fe2fSAruna Balakrishnaiah 		break;
2117de8fe2fSAruna Balakrishnaiah 	}
2127de8fe2fSAruna Balakrishnaiah 
213cb3bee03SGeliang Tang 	return (size * 100) / cmpr;
2148cfc8ddcSGeliang Tang }
2158cfc8ddcSGeliang Tang #endif
2168cfc8ddcSGeliang Tang 
21758eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
218cb3bee03SGeliang Tang static int zbufsize_lzo(size_t size)
2198cfc8ddcSGeliang Tang {
220cb3bee03SGeliang Tang 	return lzo1x_worst_compress(size);
2218cfc8ddcSGeliang Tang }
2228cfc8ddcSGeliang Tang #endif
2238cfc8ddcSGeliang Tang 
22458eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
225cb3bee03SGeliang Tang static int zbufsize_lz4(size_t size)
2268cfc8ddcSGeliang Tang {
227cb3bee03SGeliang Tang 	return LZ4_compressBound(size);
228239b7161SGeliang Tang }
229239b7161SGeliang Tang #endif
230239b7161SGeliang Tang 
23158eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
232cb3bee03SGeliang Tang static int zbufsize_842(size_t size)
233239b7161SGeliang Tang {
23455597406SKees Cook 	return size;
235239b7161SGeliang Tang }
236fe1d4758SKees Cook #endif
2378cfc8ddcSGeliang Tang 
2381021bcf4SGeliang Tang #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
2391021bcf4SGeliang Tang static int zbufsize_zstd(size_t size)
2401021bcf4SGeliang Tang {
2411021bcf4SGeliang Tang 	return ZSTD_compressBound(size);
2421021bcf4SGeliang Tang }
2431021bcf4SGeliang Tang #endif
2441021bcf4SGeliang Tang 
245fe1d4758SKees Cook static const struct pstore_zbackend *zbackend __ro_after_init;
246fe1d4758SKees Cook 
247fe1d4758SKees Cook static const struct pstore_zbackend zbackends[] = {
24858eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
249fe1d4758SKees Cook 	{
250cb3bee03SGeliang Tang 		.zbufsize	= zbufsize_deflate,
251cb3bee03SGeliang Tang 		.name		= "deflate",
252fe1d4758SKees Cook 	},
253fe1d4758SKees Cook #endif
25458eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
255fe1d4758SKees Cook 	{
256cb3bee03SGeliang Tang 		.zbufsize	= zbufsize_lzo,
257fe1d4758SKees Cook 		.name		= "lzo",
258fe1d4758SKees Cook 	},
259fe1d4758SKees Cook #endif
26058eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS)
261fe1d4758SKees Cook 	{
262cb3bee03SGeliang Tang 		.zbufsize	= zbufsize_lz4,
263fe1d4758SKees Cook 		.name		= "lz4",
264fe1d4758SKees Cook 	},
265fe1d4758SKees Cook #endif
26658eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
267fe1d4758SKees Cook 	{
268cb3bee03SGeliang Tang 		.zbufsize	= zbufsize_lz4,
269fe1d4758SKees Cook 		.name		= "lz4hc",
270fe1d4758SKees Cook 	},
271fe1d4758SKees Cook #endif
27258eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
273fe1d4758SKees Cook 	{
274cb3bee03SGeliang Tang 		.zbufsize	= zbufsize_842,
275239b7161SGeliang Tang 		.name		= "842",
276fe1d4758SKees Cook 	},
277fe1d4758SKees Cook #endif
2781021bcf4SGeliang Tang #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
2791021bcf4SGeliang Tang 	{
2801021bcf4SGeliang Tang 		.zbufsize	= zbufsize_zstd,
2811021bcf4SGeliang Tang 		.name		= "zstd",
2821021bcf4SGeliang Tang 	},
2831021bcf4SGeliang Tang #endif
284fe1d4758SKees Cook 	{ }
2858cfc8ddcSGeliang Tang };
2868cfc8ddcSGeliang Tang 
2878cfc8ddcSGeliang Tang static int pstore_compress(const void *in, void *out,
288cb3bee03SGeliang Tang 			   unsigned int inlen, unsigned int outlen)
2898cfc8ddcSGeliang Tang {
290cb3bee03SGeliang Tang 	int ret;
291cb3bee03SGeliang Tang 
292cb3bee03SGeliang Tang 	ret = crypto_comp_compress(tfm, in, inlen, out, &outlen);
293cb3bee03SGeliang Tang 	if (ret) {
294cb3bee03SGeliang Tang 		pr_err("crypto_comp_compress failed, ret = %d!\n", ret);
295cb3bee03SGeliang Tang 		return ret;
2968cfc8ddcSGeliang Tang 	}
2978cfc8ddcSGeliang Tang 
298cb3bee03SGeliang Tang 	return outlen;
299cb3bee03SGeliang Tang }
300cb3bee03SGeliang Tang 
3018cfc8ddcSGeliang Tang static void allocate_buf_for_compression(void)
3028cfc8ddcSGeliang Tang {
30395047b05SKees Cook 	struct crypto_comp *ctx;
30495047b05SKees Cook 	int size;
30595047b05SKees Cook 	char *buf;
30695047b05SKees Cook 
30795047b05SKees Cook 	/* Skip if not built-in or compression backend not selected yet. */
308e698aaf3STobias Regnery 	if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !zbackend)
309cb3bee03SGeliang Tang 		return;
310cb3bee03SGeliang Tang 
31195047b05SKees Cook 	/* Skip if no pstore backend yet or compression init already done. */
31295047b05SKees Cook 	if (!psinfo || tfm)
31395047b05SKees Cook 		return;
31495047b05SKees Cook 
315cb3bee03SGeliang Tang 	if (!crypto_has_comp(zbackend->name, 0, 0)) {
31695047b05SKees Cook 		pr_err("Unknown compression: %s\n", zbackend->name);
317cb3bee03SGeliang Tang 		return;
318cb3bee03SGeliang Tang 	}
319cb3bee03SGeliang Tang 
32095047b05SKees Cook 	size = zbackend->zbufsize(psinfo->bufsize);
32195047b05SKees Cook 	if (size <= 0) {
32295047b05SKees Cook 		pr_err("Invalid compression size for %s: %d\n",
32395047b05SKees Cook 		       zbackend->name, size);
324cb3bee03SGeliang Tang 		return;
325cb3bee03SGeliang Tang 	}
326cb3bee03SGeliang Tang 
32795047b05SKees Cook 	buf = kmalloc(size, GFP_KERNEL);
32895047b05SKees Cook 	if (!buf) {
32995047b05SKees Cook 		pr_err("Failed %d byte compression buffer allocation for: %s\n",
33095047b05SKees Cook 		       size, zbackend->name);
331cb3bee03SGeliang Tang 		return;
3328cfc8ddcSGeliang Tang 	}
33395047b05SKees Cook 
33495047b05SKees Cook 	ctx = crypto_alloc_comp(zbackend->name, 0, 0);
33595047b05SKees Cook 	if (IS_ERR_OR_NULL(ctx)) {
33695047b05SKees Cook 		kfree(buf);
33795047b05SKees Cook 		pr_err("crypto_alloc_comp('%s') failed: %ld\n", zbackend->name,
33895047b05SKees Cook 		       PTR_ERR(ctx));
33995047b05SKees Cook 		return;
34095047b05SKees Cook 	}
34195047b05SKees Cook 
34295047b05SKees Cook 	/* A non-NULL big_oops_buf indicates compression is available. */
34395047b05SKees Cook 	tfm = ctx;
34495047b05SKees Cook 	big_oops_buf_sz = size;
34595047b05SKees Cook 	big_oops_buf = buf;
34695047b05SKees Cook 
3470eed84ffSKees Cook 	pr_info("Using crash dump compression: %s\n", zbackend->name);
3488cfc8ddcSGeliang Tang }
3498cfc8ddcSGeliang Tang 
3508cfc8ddcSGeliang Tang static void free_buf_for_compression(void)
3518cfc8ddcSGeliang Tang {
352a9fb94a9SPi-Hsun Shih 	if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && tfm) {
353cb3bee03SGeliang Tang 		crypto_free_comp(tfm);
354a9fb94a9SPi-Hsun Shih 		tfm = NULL;
355a9fb94a9SPi-Hsun Shih 	}
356cb3bee03SGeliang Tang 	kfree(big_oops_buf);
357cb3bee03SGeliang Tang 	big_oops_buf = NULL;
358cb3bee03SGeliang Tang 	big_oops_buf_sz = 0;
359ee1d2674SGeliang Tang }
360ee1d2674SGeliang Tang 
361b0aad7a9SAruna Balakrishnaiah /*
362b0aad7a9SAruna Balakrishnaiah  * Called when compression fails, since the printk buffer
363b0aad7a9SAruna Balakrishnaiah  * would be fetched for compression calling it again when
364b0aad7a9SAruna Balakrishnaiah  * compression fails would have moved the iterator of
365b0aad7a9SAruna Balakrishnaiah  * printk buffer which results in fetching old contents.
366b0aad7a9SAruna Balakrishnaiah  * Copy the recent messages from big_oops_buf to psinfo->buf
367b0aad7a9SAruna Balakrishnaiah  */
368b0aad7a9SAruna Balakrishnaiah static size_t copy_kmsg_to_buffer(int hsize, size_t len)
369b0aad7a9SAruna Balakrishnaiah {
370b0aad7a9SAruna Balakrishnaiah 	size_t total_len;
371b0aad7a9SAruna Balakrishnaiah 	size_t diff;
372b0aad7a9SAruna Balakrishnaiah 
373b0aad7a9SAruna Balakrishnaiah 	total_len = hsize + len;
374b0aad7a9SAruna Balakrishnaiah 
375b0aad7a9SAruna Balakrishnaiah 	if (total_len > psinfo->bufsize) {
376b0aad7a9SAruna Balakrishnaiah 		diff = total_len - psinfo->bufsize + hsize;
377b0aad7a9SAruna Balakrishnaiah 		memcpy(psinfo->buf, big_oops_buf, hsize);
378b0aad7a9SAruna Balakrishnaiah 		memcpy(psinfo->buf + hsize, big_oops_buf + diff,
379b0aad7a9SAruna Balakrishnaiah 					psinfo->bufsize - hsize);
380b0aad7a9SAruna Balakrishnaiah 		total_len = psinfo->bufsize;
381b0aad7a9SAruna Balakrishnaiah 	} else
382b0aad7a9SAruna Balakrishnaiah 		memcpy(psinfo->buf, big_oops_buf, total_len);
383b0aad7a9SAruna Balakrishnaiah 
384b0aad7a9SAruna Balakrishnaiah 	return total_len;
385b0aad7a9SAruna Balakrishnaiah }
386b0aad7a9SAruna Balakrishnaiah 
387e581ca81SKees Cook void pstore_record_init(struct pstore_record *record,
388e581ca81SKees Cook 			struct pstore_info *psinfo)
389e581ca81SKees Cook {
390e581ca81SKees Cook 	memset(record, 0, sizeof(*record));
391e581ca81SKees Cook 
392e581ca81SKees Cook 	record->psi = psinfo;
393c7f3c595SKees Cook 
394c7f3c595SKees Cook 	/* Report zeroed timestamp if called before timekeeping has resumed. */
3957aaa822eSKees Cook 	record->time = ns_to_timespec64(ktime_get_real_fast_ns());
396e581ca81SKees Cook }
397e581ca81SKees Cook 
398ca01d6ddSTony Luck /*
3990eed84ffSKees Cook  * callback from kmsg_dump. Save as much as we can (up to kmsg_bytes) from the
4000eed84ffSKees Cook  * end of the buffer.
401ca01d6ddSTony Luck  */
402ca01d6ddSTony Luck static void pstore_dump(struct kmsg_dumper *dumper,
403e2ae715dSKay Sievers 			enum kmsg_dump_reason reason)
404ca01d6ddSTony Luck {
405e2ae715dSKay Sievers 	unsigned long	total = 0;
406381b872cSSeiji Aguchi 	const char	*why;
407b94fdd07SMatthew Garrett 	unsigned int	part = 1;
408e2ae715dSKay Sievers 	int		ret;
409ca01d6ddSTony Luck 
410381b872cSSeiji Aguchi 	why = get_reason_str(reason);
4119f6af27fSTony Luck 
412ea84b580SKees Cook 	if (down_trylock(&psinfo->buf_lock)) {
413ea84b580SKees Cook 		/* Failed to acquire lock: give up if we cannot wait. */
414ea84b580SKees Cook 		if (pstore_cannot_wait(reason)) {
415ea84b580SKees Cook 			pr_err("dump skipped in %s path: may corrupt error record\n",
416ea84b580SKees Cook 				in_nmi() ? "NMI" : why);
417959217c8SLi Pengcheng 			return;
4189f244e9cSSeiji Aguchi 		}
419ea84b580SKees Cook 		if (down_interruptible(&psinfo->buf_lock)) {
420ea84b580SKees Cook 			pr_err("could not grab semaphore?!\n");
421ea84b580SKees Cook 			return;
42298e44fdaSNamhyung Kim 		}
423ea84b580SKees Cook 	}
424ea84b580SKees Cook 
425ca01d6ddSTony Luck 	oopscount++;
426ca01d6ddSTony Luck 	while (total < kmsg_bytes) {
427e2ae715dSKay Sievers 		char *dst;
42876cc9580SKees Cook 		size_t dst_size;
42976cc9580SKees Cook 		int header_size;
430b0aad7a9SAruna Balakrishnaiah 		int zipped_len = -1;
43176cc9580SKees Cook 		size_t dump_size;
432e581ca81SKees Cook 		struct pstore_record record;
433e581ca81SKees Cook 
434e581ca81SKees Cook 		pstore_record_init(&record, psinfo);
435e581ca81SKees Cook 		record.type = PSTORE_TYPE_DMESG;
436e581ca81SKees Cook 		record.count = oopscount;
437e581ca81SKees Cook 		record.reason = reason;
438e581ca81SKees Cook 		record.part = part;
439e581ca81SKees Cook 		record.buf = psinfo->buf;
440e2ae715dSKay Sievers 
441ea84b580SKees Cook 		if (big_oops_buf) {
442b0aad7a9SAruna Balakrishnaiah 			dst = big_oops_buf;
44376cc9580SKees Cook 			dst_size = big_oops_buf_sz;
444235f6d15SNamhyung Kim 		} else {
445235f6d15SNamhyung Kim 			dst = psinfo->buf;
44676cc9580SKees Cook 			dst_size = psinfo->bufsize;
447235f6d15SNamhyung Kim 		}
448235f6d15SNamhyung Kim 
44976cc9580SKees Cook 		/* Write dump header. */
45076cc9580SKees Cook 		header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why,
45176cc9580SKees Cook 				 oopscount, part);
45276cc9580SKees Cook 		dst_size -= header_size;
453b0aad7a9SAruna Balakrishnaiah 
45476cc9580SKees Cook 		/* Write dump contents. */
45576cc9580SKees Cook 		if (!kmsg_dump_get_buffer(dumper, true, dst + header_size,
45676cc9580SKees Cook 					  dst_size, &dump_size))
457b0aad7a9SAruna Balakrishnaiah 			break;
458b0aad7a9SAruna Balakrishnaiah 
459ea84b580SKees Cook 		if (big_oops_buf) {
460b0aad7a9SAruna Balakrishnaiah 			zipped_len = pstore_compress(dst, psinfo->buf,
46176cc9580SKees Cook 						header_size + dump_size,
46276cc9580SKees Cook 						psinfo->bufsize);
463b0aad7a9SAruna Balakrishnaiah 
464b0aad7a9SAruna Balakrishnaiah 			if (zipped_len > 0) {
46576cc9580SKees Cook 				record.compressed = true;
46676cc9580SKees Cook 				record.size = zipped_len;
467b0aad7a9SAruna Balakrishnaiah 			} else {
46876cc9580SKees Cook 				record.size = copy_kmsg_to_buffer(header_size,
46976cc9580SKees Cook 								  dump_size);
470b0aad7a9SAruna Balakrishnaiah 			}
471b0aad7a9SAruna Balakrishnaiah 		} else {
47276cc9580SKees Cook 			record.size = header_size + dump_size;
473b0aad7a9SAruna Balakrishnaiah 		}
474b0aad7a9SAruna Balakrishnaiah 
47576cc9580SKees Cook 		ret = psinfo->write(&record);
47678c83c82SKees Cook 		if (ret == 0 && reason == KMSG_DUMP_OOPS) {
4776dda9266SLuck, Tony 			pstore_new_entry = 1;
47878c83c82SKees Cook 			pstore_timer_kick();
47978c83c82SKees Cook 		}
480e2ae715dSKay Sievers 
48176cc9580SKees Cook 		total += record.size;
48256280682SMatthew Garrett 		part++;
483ca01d6ddSTony Luck 	}
484ea84b580SKees Cook 
485ea84b580SKees Cook 	up(&psinfo->buf_lock);
486ca01d6ddSTony Luck }
487ca01d6ddSTony Luck 
488ca01d6ddSTony Luck static struct kmsg_dumper pstore_dumper = {
489ca01d6ddSTony Luck 	.dump = pstore_dump,
490ca01d6ddSTony Luck };
491ca01d6ddSTony Luck 
492306e5c2aSGeliang Tang /*
493306e5c2aSGeliang Tang  * Register with kmsg_dump to save last part of console log on panic.
494306e5c2aSGeliang Tang  */
49518730411SGeliang Tang static void pstore_register_kmsg(void)
49618730411SGeliang Tang {
49718730411SGeliang Tang 	kmsg_dump_register(&pstore_dumper);
49818730411SGeliang Tang }
49918730411SGeliang Tang 
500ee1d2674SGeliang Tang static void pstore_unregister_kmsg(void)
501ee1d2674SGeliang Tang {
502ee1d2674SGeliang Tang 	kmsg_dump_unregister(&pstore_dumper);
503ee1d2674SGeliang Tang }
504ee1d2674SGeliang Tang 
505f29e5956SAnton Vorontsov #ifdef CONFIG_PSTORE_CONSOLE
506f29e5956SAnton Vorontsov static void pstore_console_write(struct console *con, const char *s, unsigned c)
507f29e5956SAnton Vorontsov {
508e581ca81SKees Cook 	struct pstore_record record;
509f29e5956SAnton Vorontsov 
5104c6c4d34SYue Hu 	if (!c)
5114c6c4d34SYue Hu 		return;
5124c6c4d34SYue Hu 
513e581ca81SKees Cook 	pstore_record_init(&record, psinfo);
514e581ca81SKees Cook 	record.type = PSTORE_TYPE_CONSOLE;
515e581ca81SKees Cook 
516b10b4711SKees Cook 	record.buf = (char *)s;
517b10b4711SKees Cook 	record.size = c;
5184c9ec219SKees Cook 	psinfo->write(&record);
519f29e5956SAnton Vorontsov }
520f29e5956SAnton Vorontsov 
521f29e5956SAnton Vorontsov static struct console pstore_console = {
522f29e5956SAnton Vorontsov 	.write	= pstore_console_write,
523f29e5956SAnton Vorontsov 	.index	= -1,
524f29e5956SAnton Vorontsov };
525f29e5956SAnton Vorontsov 
526f29e5956SAnton Vorontsov static void pstore_register_console(void)
527f29e5956SAnton Vorontsov {
528d195c390SKees Cook 	/* Show which backend is going to get console writes. */
529d195c390SKees Cook 	strscpy(pstore_console.name, psinfo->name,
530d195c390SKees Cook 		sizeof(pstore_console.name));
531b7753fc7SKees Cook 	/*
532b7753fc7SKees Cook 	 * Always initialize flags here since prior unregister_console()
533b7753fc7SKees Cook 	 * calls may have changed settings (specifically CON_ENABLED).
534b7753fc7SKees Cook 	 */
535b7753fc7SKees Cook 	pstore_console.flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME;
536f29e5956SAnton Vorontsov 	register_console(&pstore_console);
537f29e5956SAnton Vorontsov }
538ee1d2674SGeliang Tang 
539ee1d2674SGeliang Tang static void pstore_unregister_console(void)
540ee1d2674SGeliang Tang {
541ee1d2674SGeliang Tang 	unregister_console(&pstore_console);
542ee1d2674SGeliang Tang }
543f29e5956SAnton Vorontsov #else
544f29e5956SAnton Vorontsov static void pstore_register_console(void) {}
545ee1d2674SGeliang Tang static void pstore_unregister_console(void) {}
546f29e5956SAnton Vorontsov #endif
547f29e5956SAnton Vorontsov 
5484c9ec219SKees Cook static int pstore_write_user_compat(struct pstore_record *record,
549fdd03118SKees Cook 				    const char __user *buf)
5505bf6d1b9SMark Salyzyn {
55130800d99SKees Cook 	int ret = 0;
5525bf6d1b9SMark Salyzyn 
55330800d99SKees Cook 	if (record->buf)
55430800d99SKees Cook 		return -EINVAL;
5555bf6d1b9SMark Salyzyn 
556077090afSGeliang Tang 	record->buf = memdup_user(buf, record->size);
557dfd6fa39SHirofumi Nakagawa 	if (IS_ERR(record->buf)) {
558077090afSGeliang Tang 		ret = PTR_ERR(record->buf);
55930800d99SKees Cook 		goto out;
5605bf6d1b9SMark Salyzyn 	}
56130800d99SKees Cook 
5624c9ec219SKees Cook 	ret = record->psi->write(record);
56330800d99SKees Cook 
56430800d99SKees Cook 	kfree(record->buf);
565077090afSGeliang Tang out:
56630800d99SKees Cook 	record->buf = NULL;
56730800d99SKees Cook 
56830800d99SKees Cook 	return unlikely(ret < 0) ? ret : record->size;
5695bf6d1b9SMark Salyzyn }
5705bf6d1b9SMark Salyzyn 
571ca01d6ddSTony Luck /*
572ca01d6ddSTony Luck  * platform specific persistent storage driver registers with
573ca01d6ddSTony Luck  * us here. If pstore is already mounted, call the platform
574ca01d6ddSTony Luck  * read function right away to populate the file system. If not
575ca01d6ddSTony Luck  * then the pstore mount code will call us later to fill out
576ca01d6ddSTony Luck  * the file system.
577ca01d6ddSTony Luck  */
578ca01d6ddSTony Luck int pstore_register(struct pstore_info *psi)
579ca01d6ddSTony Luck {
5800d7cd09aSKees Cook 	if (backend && strcmp(backend, psi->name)) {
5810d7cd09aSKees Cook 		pr_warn("ignoring unexpected backend '%s'\n", psi->name);
5828e48b1a8SLenny Szubowicz 		return -EPERM;
5830d7cd09aSKees Cook 	}
5848e48b1a8SLenny Szubowicz 
5854c9ec219SKees Cook 	/* Sanity check flags. */
5864c9ec219SKees Cook 	if (!psi->flags) {
5874c9ec219SKees Cook 		pr_warn("backend '%s' must support at least one frontend\n",
5884c9ec219SKees Cook 			psi->name);
5894c9ec219SKees Cook 		return -EINVAL;
5904c9ec219SKees Cook 	}
5914c9ec219SKees Cook 
5924c9ec219SKees Cook 	/* Check for required functions. */
5934c9ec219SKees Cook 	if (!psi->read || !psi->write) {
5944c9ec219SKees Cook 		pr_warn("backend '%s' must implement read() and write()\n",
5954c9ec219SKees Cook 			psi->name);
5964c9ec219SKees Cook 		return -EINVAL;
5974c9ec219SKees Cook 	}
5984c9ec219SKees Cook 
599cab12fd0SKees Cook 	mutex_lock(&psinfo_lock);
600ca01d6ddSTony Luck 	if (psinfo) {
6010d7cd09aSKees Cook 		pr_warn("backend '%s' already loaded: ignoring '%s'\n",
6020d7cd09aSKees Cook 			psinfo->name, psi->name);
603cab12fd0SKees Cook 		mutex_unlock(&psinfo_lock);
604ca01d6ddSTony Luck 		return -EBUSY;
605ca01d6ddSTony Luck 	}
606dee28e72SMatthew Garrett 
6074c9ec219SKees Cook 	if (!psi->write_user)
6084c9ec219SKees Cook 		psi->write_user = pstore_write_user_compat;
609ca01d6ddSTony Luck 	psinfo = psi;
610f6f82851SKees Cook 	mutex_init(&psinfo->read_mutex);
611ea84b580SKees Cook 	sema_init(&psinfo->buf_lock, 1);
612ca01d6ddSTony Luck 
6138880fa32SKees Cook 	if (psi->flags & PSTORE_FLAGS_DMESG)
614b0aad7a9SAruna Balakrishnaiah 		allocate_buf_for_compression();
615b0aad7a9SAruna Balakrishnaiah 
6166dda9266SLuck, Tony 	pstore_get_records(0);
617ca01d6ddSTony Luck 
618c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_DMESG)
61918730411SGeliang Tang 		pstore_register_kmsg();
620c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_CONSOLE)
621f29e5956SAnton Vorontsov 		pstore_register_console();
622c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_FTRACE)
62365f8c95eSAnton Vorontsov 		pstore_register_ftrace();
624c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_PMSG)
6259d5438f4SMark Salyzyn 		pstore_register_pmsg();
626ca01d6ddSTony Luck 
6276330d553SKees Cook 	/* Start watching for new records, if desired. */
62878c83c82SKees Cook 	pstore_timer_kick();
6296dda9266SLuck, Tony 
63042222c2aSWang Long 	/*
63142222c2aSWang Long 	 * Update the module parameter backend, so it is visible
63242222c2aSWang Long 	 * through /sys/module/pstore/parameters/backend
63342222c2aSWang Long 	 */
634563ca40dSKees Cook 	backend = kstrdup(psi->name, GFP_KERNEL);
63542222c2aSWang Long 
636ef748853SFabian Frederick 	pr_info("Registered %s as persistent store backend\n", psi->name);
6378e48b1a8SLenny Szubowicz 
6386248a066SKees Cook 	mutex_unlock(&psinfo_lock);
639ca01d6ddSTony Luck 	return 0;
640ca01d6ddSTony Luck }
641ca01d6ddSTony Luck EXPORT_SYMBOL_GPL(pstore_register);
642ca01d6ddSTony Luck 
643ee1d2674SGeliang Tang void pstore_unregister(struct pstore_info *psi)
644ee1d2674SGeliang Tang {
6456248a066SKees Cook 	/* It's okay to unregister nothing. */
6466248a066SKees Cook 	if (!psi)
6476248a066SKees Cook 		return;
6486248a066SKees Cook 
6496248a066SKees Cook 	mutex_lock(&psinfo_lock);
6506248a066SKees Cook 
6516248a066SKees Cook 	/* Only one backend can be registered at a time. */
6526248a066SKees Cook 	if (WARN_ON(psi != psinfo)) {
6536248a066SKees Cook 		mutex_unlock(&psinfo_lock);
6546248a066SKees Cook 		return;
6556248a066SKees Cook 	}
6566248a066SKees Cook 
65778c83c82SKees Cook 	/* Unregister all callbacks. */
658c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_PMSG)
659ee1d2674SGeliang Tang 		pstore_unregister_pmsg();
660c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_FTRACE)
661ee1d2674SGeliang Tang 		pstore_unregister_ftrace();
662c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_CONSOLE)
663ee1d2674SGeliang Tang 		pstore_unregister_console();
664c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_DMESG)
665ee1d2674SGeliang Tang 		pstore_unregister_kmsg();
666ee1d2674SGeliang Tang 
66778c83c82SKees Cook 	/* Stop timer and make sure all work has finished. */
66878c83c82SKees Cook 	del_timer_sync(&pstore_timer);
66978c83c82SKees Cook 	flush_work(&pstore_work);
67078c83c82SKees Cook 
671609e28bbSKees Cook 	/* Remove all backend records from filesystem tree. */
672609e28bbSKees Cook 	pstore_put_backend_records(psi);
673609e28bbSKees Cook 
674ee1d2674SGeliang Tang 	free_buf_for_compression();
675ee1d2674SGeliang Tang 
676ee1d2674SGeliang Tang 	psinfo = NULL;
677563ca40dSKees Cook 	kfree(backend);
678ee1d2674SGeliang Tang 	backend = NULL;
6796248a066SKees Cook 	mutex_unlock(&psinfo_lock);
680ee1d2674SGeliang Tang }
681ee1d2674SGeliang Tang EXPORT_SYMBOL_GPL(pstore_unregister);
682ee1d2674SGeliang Tang 
683634f8f51SKees Cook static void decompress_record(struct pstore_record *record)
684634f8f51SKees Cook {
685bdabc8e7SKees Cook 	int ret;
686634f8f51SKees Cook 	int unzipped_len;
687bdabc8e7SKees Cook 	char *unzipped, *workspace;
688634f8f51SKees Cook 
6894a16d1cbSAnkit Kumar 	if (!record->compressed)
6904a16d1cbSAnkit Kumar 		return;
6914a16d1cbSAnkit Kumar 
692634f8f51SKees Cook 	/* Only PSTORE_TYPE_DMESG support compression. */
6934a16d1cbSAnkit Kumar 	if (record->type != PSTORE_TYPE_DMESG) {
694634f8f51SKees Cook 		pr_warn("ignored compressed record type %d\n", record->type);
695634f8f51SKees Cook 		return;
696634f8f51SKees Cook 	}
697634f8f51SKees Cook 
698bdabc8e7SKees Cook 	/* Missing compression buffer means compression was not initialized. */
699634f8f51SKees Cook 	if (!big_oops_buf) {
700bdabc8e7SKees Cook 		pr_warn("no decompression method initialized!\n");
701634f8f51SKees Cook 		return;
702634f8f51SKees Cook 	}
703634f8f51SKees Cook 
704bdabc8e7SKees Cook 	/* Allocate enough space to hold max decompression and ECC. */
705bdabc8e7SKees Cook 	unzipped_len = big_oops_buf_sz;
706bdabc8e7SKees Cook 	workspace = kmalloc(unzipped_len + record->ecc_notice_size,
7077e8cc8dcSKees Cook 			    GFP_KERNEL);
708bdabc8e7SKees Cook 	if (!workspace)
709bdabc8e7SKees Cook 		return;
710bdabc8e7SKees Cook 
711bdabc8e7SKees Cook 	/* After decompression "unzipped_len" is almost certainly smaller. */
712bdabc8e7SKees Cook 	ret = crypto_comp_decompress(tfm, record->buf, record->size,
713bdabc8e7SKees Cook 					  workspace, &unzipped_len);
714bdabc8e7SKees Cook 	if (ret) {
715bdabc8e7SKees Cook 		pr_err("crypto_comp_decompress failed, ret = %d!\n", ret);
716bdabc8e7SKees Cook 		kfree(workspace);
7177e8cc8dcSKees Cook 		return;
7187e8cc8dcSKees Cook 	}
7197e8cc8dcSKees Cook 
7207e8cc8dcSKees Cook 	/* Append ECC notice to decompressed buffer. */
721bdabc8e7SKees Cook 	memcpy(workspace + unzipped_len, record->buf + record->size,
722634f8f51SKees Cook 	       record->ecc_notice_size);
7237e8cc8dcSKees Cook 
724bdabc8e7SKees Cook 	/* Copy decompressed contents into an minimum-sized allocation. */
725bdabc8e7SKees Cook 	unzipped = kmemdup(workspace, unzipped_len + record->ecc_notice_size,
726bdabc8e7SKees Cook 			   GFP_KERNEL);
727bdabc8e7SKees Cook 	kfree(workspace);
728bdabc8e7SKees Cook 	if (!unzipped)
729bdabc8e7SKees Cook 		return;
730bdabc8e7SKees Cook 
731bdabc8e7SKees Cook 	/* Swap out compressed contents with decompressed contents. */
732634f8f51SKees Cook 	kfree(record->buf);
733bdabc8e7SKees Cook 	record->buf = unzipped;
734634f8f51SKees Cook 	record->size = unzipped_len;
735634f8f51SKees Cook 	record->compressed = false;
736634f8f51SKees Cook }
737634f8f51SKees Cook 
738ca01d6ddSTony Luck /*
7393a7d2fd1SKees Cook  * Read all the records from one persistent store backend. Create
7406dda9266SLuck, Tony  * files in our filesystem.  Don't warn about -EEXIST errors
7416dda9266SLuck, Tony  * when we are re-scanning the backing store looking to add new
7426dda9266SLuck, Tony  * error records.
743ca01d6ddSTony Luck  */
7443a7d2fd1SKees Cook void pstore_get_backend_records(struct pstore_info *psi,
7453a7d2fd1SKees Cook 				struct dentry *root, int quiet)
746ca01d6ddSTony Luck {
7472a2b0acfSKees Cook 	int failed = 0;
748656de42eSKees Cook 	unsigned int stop_loop = 65536;
749ca01d6ddSTony Luck 
7503a7d2fd1SKees Cook 	if (!psi || !root)
751ca01d6ddSTony Luck 		return;
752ca01d6ddSTony Luck 
753f6f82851SKees Cook 	mutex_lock(&psi->read_mutex);
7542174f6dfSKees Cook 	if (psi->open && psi->open(psi))
75506cf91b4SChen Gong 		goto out;
75606cf91b4SChen Gong 
7571dfff7ddSKees Cook 	/*
7581dfff7ddSKees Cook 	 * Backend callback read() allocates record.buf. decompress_record()
7591dfff7ddSKees Cook 	 * may reallocate record.buf. On success, pstore_mkfile() will keep
7601dfff7ddSKees Cook 	 * the record.buf, so free it only on failure.
7611dfff7ddSKees Cook 	 */
762656de42eSKees Cook 	for (; stop_loop; stop_loop--) {
7632a2b0acfSKees Cook 		struct pstore_record *record;
7642a2b0acfSKees Cook 		int rc;
7652a2b0acfSKees Cook 
7662a2b0acfSKees Cook 		record = kzalloc(sizeof(*record), GFP_KERNEL);
7672a2b0acfSKees Cook 		if (!record) {
7682a2b0acfSKees Cook 			pr_err("out of memory creating record\n");
7692a2b0acfSKees Cook 			break;
7702a2b0acfSKees Cook 		}
771e581ca81SKees Cook 		pstore_record_init(record, psi);
7722a2b0acfSKees Cook 
7732a2b0acfSKees Cook 		record->size = psi->read(record);
7742a2b0acfSKees Cook 
7752a2b0acfSKees Cook 		/* No more records left in backend? */
776f6525b96SDouglas Anderson 		if (record->size <= 0) {
777f6525b96SDouglas Anderson 			kfree(record);
7782a2b0acfSKees Cook 			break;
779f6525b96SDouglas Anderson 		}
7802a2b0acfSKees Cook 
7812a2b0acfSKees Cook 		decompress_record(record);
7823a7d2fd1SKees Cook 		rc = pstore_mkfile(root, record);
7831dfff7ddSKees Cook 		if (rc) {
78483f70f07SKees Cook 			/* pstore_mkfile() did not take record, so free it. */
7852a2b0acfSKees Cook 			kfree(record->buf);
78683f70f07SKees Cook 			kfree(record);
7871dfff7ddSKees Cook 			if (rc != -EEXIST || !quiet)
7881dfff7ddSKees Cook 				failed++;
7891dfff7ddSKees Cook 		}
790ca01d6ddSTony Luck 	}
7912174f6dfSKees Cook 	if (psi->close)
79206cf91b4SChen Gong 		psi->close(psi);
79306cf91b4SChen Gong out:
794f6f82851SKees Cook 	mutex_unlock(&psi->read_mutex);
795ca01d6ddSTony Luck 
796ca01d6ddSTony Luck 	if (failed)
797656de42eSKees Cook 		pr_warn("failed to create %d record(s) from '%s'\n",
798ca01d6ddSTony Luck 			failed, psi->name);
799656de42eSKees Cook 	if (!stop_loop)
800656de42eSKees Cook 		pr_err("looping? Too many records seen from '%s'\n",
801656de42eSKees Cook 			psi->name);
802ca01d6ddSTony Luck }
803ca01d6ddSTony Luck 
8046dda9266SLuck, Tony static void pstore_dowork(struct work_struct *work)
8056dda9266SLuck, Tony {
8066dda9266SLuck, Tony 	pstore_get_records(1);
8076dda9266SLuck, Tony }
8086dda9266SLuck, Tony 
80924ed960aSKees Cook static void pstore_timefunc(struct timer_list *unused)
8106dda9266SLuck, Tony {
8116dda9266SLuck, Tony 	if (pstore_new_entry) {
8126dda9266SLuck, Tony 		pstore_new_entry = 0;
8136dda9266SLuck, Tony 		schedule_work(&pstore_work);
8146dda9266SLuck, Tony 	}
8156dda9266SLuck, Tony 
81678c83c82SKees Cook 	pstore_timer_kick();
8176dda9266SLuck, Tony }
8186dda9266SLuck, Tony 
8198d82cee2SBen Dooks (Codethink) static void __init pstore_choose_compression(void)
820fe1d4758SKees Cook {
821fe1d4758SKees Cook 	const struct pstore_zbackend *step;
822fe1d4758SKees Cook 
823fe1d4758SKees Cook 	if (!compress)
824fe1d4758SKees Cook 		return;
825fe1d4758SKees Cook 
826fe1d4758SKees Cook 	for (step = zbackends; step->name; step++) {
827fe1d4758SKees Cook 		if (!strcmp(compress, step->name)) {
828fe1d4758SKees Cook 			zbackend = step;
829fe1d4758SKees Cook 			return;
830fe1d4758SKees Cook 		}
831fe1d4758SKees Cook 	}
832fe1d4758SKees Cook }
833fe1d4758SKees Cook 
834cb095afdSKees Cook static int __init pstore_init(void)
835cb095afdSKees Cook {
836cb095afdSKees Cook 	int ret;
837cb095afdSKees Cook 
838cb095afdSKees Cook 	pstore_choose_compression();
839cb095afdSKees Cook 
84041603165SJoel Fernandes (Google) 	/*
84141603165SJoel Fernandes (Google) 	 * Check if any pstore backends registered earlier but did not
84241603165SJoel Fernandes (Google) 	 * initialize compression because crypto was not ready. If so,
84341603165SJoel Fernandes (Google) 	 * initialize compression now.
84441603165SJoel Fernandes (Google) 	 */
84541603165SJoel Fernandes (Google) 	allocate_buf_for_compression();
84641603165SJoel Fernandes (Google) 
847cb095afdSKees Cook 	ret = pstore_init_fs();
848cb095afdSKees Cook 	if (ret)
8498a57d6d4Schenqiwu 		free_buf_for_compression();
850cb095afdSKees Cook 
8518a57d6d4Schenqiwu 	return ret;
852cb095afdSKees Cook }
85341603165SJoel Fernandes (Google) late_initcall(pstore_init);
854cb095afdSKees Cook 
855cb095afdSKees Cook static void __exit pstore_exit(void)
856cb095afdSKees Cook {
857cb095afdSKees Cook 	pstore_exit_fs();
858cb095afdSKees Cook }
859cb095afdSKees Cook module_exit(pstore_exit)
860cb095afdSKees Cook 
861cb095afdSKees Cook MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
862cb095afdSKees Cook MODULE_LICENSE("GPL");
863