xref: /openbmc/linux/fs/pstore/platform.c (revision 95047b0519c17a28e09df5f38750f5354e3db4c4)
1ca01d6ddSTony Luck /*
2ca01d6ddSTony Luck  * Persistent Storage - platform driver interface parts.
3ca01d6ddSTony Luck  *
4f29e5956SAnton Vorontsov  * Copyright (C) 2007-2008 Google, Inc.
5ca01d6ddSTony Luck  * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com>
6ca01d6ddSTony Luck  *
7ca01d6ddSTony Luck  *  This program is free software; you can redistribute it and/or modify
8ca01d6ddSTony Luck  *  it under the terms of the GNU General Public License version 2 as
9ca01d6ddSTony Luck  *  published by the Free Software Foundation.
10ca01d6ddSTony Luck  *
11ca01d6ddSTony Luck  *  This program is distributed in the hope that it will be useful,
12ca01d6ddSTony Luck  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13ca01d6ddSTony Luck  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14ca01d6ddSTony Luck  *  GNU General Public License for more details.
15ca01d6ddSTony Luck  *
16ca01d6ddSTony Luck  *  You should have received a copy of the GNU General Public License
17ca01d6ddSTony Luck  *  along with this program; if not, write to the Free Software
18ca01d6ddSTony Luck  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19ca01d6ddSTony Luck  */
20ca01d6ddSTony Luck 
21ef748853SFabian Frederick #define pr_fmt(fmt) "pstore: " fmt
22ef748853SFabian Frederick 
23ca01d6ddSTony Luck #include <linux/atomic.h>
24ca01d6ddSTony Luck #include <linux/types.h>
25ca01d6ddSTony Luck #include <linux/errno.h>
26ca01d6ddSTony Luck #include <linux/init.h>
27ca01d6ddSTony Luck #include <linux/kmsg_dump.h>
28f29e5956SAnton Vorontsov #include <linux/console.h>
29ca01d6ddSTony Luck #include <linux/module.h>
30ca01d6ddSTony Luck #include <linux/pstore.h>
3158eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
328cfc8ddcSGeliang Tang #include <linux/lzo.h>
338cfc8ddcSGeliang Tang #endif
3458eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
358cfc8ddcSGeliang Tang #include <linux/lz4.h>
368cfc8ddcSGeliang Tang #endif
371021bcf4SGeliang Tang #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
381021bcf4SGeliang Tang #include <linux/zstd.h>
391021bcf4SGeliang Tang #endif
40cb3bee03SGeliang Tang #include <linux/crypto.h>
41ca01d6ddSTony Luck #include <linux/string.h>
426dda9266SLuck, Tony #include <linux/timer.h>
43ca01d6ddSTony Luck #include <linux/slab.h>
44ca01d6ddSTony Luck #include <linux/uaccess.h>
45a3f5f075SAnton Vorontsov #include <linux/jiffies.h>
466dda9266SLuck, Tony #include <linux/workqueue.h>
47ca01d6ddSTony Luck 
48ca01d6ddSTony Luck #include "internal.h"
49ca01d6ddSTony Luck 
50ca01d6ddSTony Luck /*
516dda9266SLuck, Tony  * We defer making "oops" entries appear in pstore - see
526dda9266SLuck, Tony  * whether the system is actually still running well enough
536dda9266SLuck, Tony  * to let someone see the entry
546dda9266SLuck, Tony  */
55521f7288SAnton Vorontsov static int pstore_update_ms = -1;
56a3f5f075SAnton Vorontsov module_param_named(update_ms, pstore_update_ms, int, 0600);
57a3f5f075SAnton Vorontsov MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content "
58521f7288SAnton Vorontsov 		 "(default is -1, which means runtime updates are disabled; "
59521f7288SAnton Vorontsov 		 "enabling this option is not safe, it may lead to further "
60521f7288SAnton Vorontsov 		 "corruption on Oopses)");
616dda9266SLuck, Tony 
626dda9266SLuck, Tony static int pstore_new_entry;
636dda9266SLuck, Tony 
6424ed960aSKees Cook static void pstore_timefunc(struct timer_list *);
651d27e3e2SKees Cook static DEFINE_TIMER(pstore_timer, pstore_timefunc);
666dda9266SLuck, Tony 
676dda9266SLuck, Tony static void pstore_dowork(struct work_struct *);
686dda9266SLuck, Tony static DECLARE_WORK(pstore_work, pstore_dowork);
696dda9266SLuck, Tony 
706dda9266SLuck, Tony /*
71ca01d6ddSTony Luck  * pstore_lock just protects "psinfo" during
72ca01d6ddSTony Luck  * calls to pstore_register()
73ca01d6ddSTony Luck  */
74ca01d6ddSTony Luck static DEFINE_SPINLOCK(pstore_lock);
75060287b8SAnton Vorontsov struct pstore_info *psinfo;
76ca01d6ddSTony Luck 
77dee28e72SMatthew Garrett static char *backend;
78fe1d4758SKees Cook static char *compress =
79fe1d4758SKees Cook #ifdef CONFIG_PSTORE_COMPRESS_DEFAULT
80fe1d4758SKees Cook 		CONFIG_PSTORE_COMPRESS_DEFAULT;
81fe1d4758SKees Cook #else
82fe1d4758SKees Cook 		NULL;
83fe1d4758SKees Cook #endif
84dee28e72SMatthew Garrett 
85b0aad7a9SAruna Balakrishnaiah /* Compression parameters */
86cb3bee03SGeliang Tang static struct crypto_comp *tfm;
878cfc8ddcSGeliang Tang 
888cfc8ddcSGeliang Tang struct pstore_zbackend {
89cb3bee03SGeliang Tang 	int (*zbufsize)(size_t size);
908cfc8ddcSGeliang Tang 	const char *name;
918cfc8ddcSGeliang Tang };
92b0aad7a9SAruna Balakrishnaiah 
93b0aad7a9SAruna Balakrishnaiah static char *big_oops_buf;
94b0aad7a9SAruna Balakrishnaiah static size_t big_oops_buf_sz;
95b0aad7a9SAruna Balakrishnaiah 
96366f7e7aSLuck, Tony /* How much of the console log to snapshot */
97349d7438SDavid Howells unsigned long kmsg_bytes = PSTORE_DEFAULT_KMSG_BYTES;
98ca01d6ddSTony Luck 
99366f7e7aSLuck, Tony void pstore_set_kmsg_bytes(int bytes)
100ca01d6ddSTony Luck {
101366f7e7aSLuck, Tony 	kmsg_bytes = bytes;
102ca01d6ddSTony Luck }
103ca01d6ddSTony Luck 
104ca01d6ddSTony Luck /* Tag each group of saved records with a sequence number */
105ca01d6ddSTony Luck static int	oopscount;
106ca01d6ddSTony Luck 
107381b872cSSeiji Aguchi static const char *get_reason_str(enum kmsg_dump_reason reason)
108381b872cSSeiji Aguchi {
109381b872cSSeiji Aguchi 	switch (reason) {
110381b872cSSeiji Aguchi 	case KMSG_DUMP_PANIC:
111381b872cSSeiji Aguchi 		return "Panic";
112381b872cSSeiji Aguchi 	case KMSG_DUMP_OOPS:
113381b872cSSeiji Aguchi 		return "Oops";
114381b872cSSeiji Aguchi 	case KMSG_DUMP_EMERG:
115381b872cSSeiji Aguchi 		return "Emergency";
116381b872cSSeiji Aguchi 	case KMSG_DUMP_RESTART:
117381b872cSSeiji Aguchi 		return "Restart";
118381b872cSSeiji Aguchi 	case KMSG_DUMP_HALT:
119381b872cSSeiji Aguchi 		return "Halt";
120381b872cSSeiji Aguchi 	case KMSG_DUMP_POWEROFF:
121381b872cSSeiji Aguchi 		return "Poweroff";
122381b872cSSeiji Aguchi 	default:
123381b872cSSeiji Aguchi 		return "Unknown";
124381b872cSSeiji Aguchi 	}
125381b872cSSeiji Aguchi }
1269f6af27fSTony Luck 
1279f244e9cSSeiji Aguchi bool pstore_cannot_block_path(enum kmsg_dump_reason reason)
1289f244e9cSSeiji Aguchi {
1299f244e9cSSeiji Aguchi 	/*
1309f244e9cSSeiji Aguchi 	 * In case of NMI path, pstore shouldn't be blocked
1319f244e9cSSeiji Aguchi 	 * regardless of reason.
1329f244e9cSSeiji Aguchi 	 */
1339f244e9cSSeiji Aguchi 	if (in_nmi())
1349f244e9cSSeiji Aguchi 		return true;
1359f244e9cSSeiji Aguchi 
1369f244e9cSSeiji Aguchi 	switch (reason) {
1379f244e9cSSeiji Aguchi 	/* In panic case, other cpus are stopped by smp_send_stop(). */
1389f244e9cSSeiji Aguchi 	case KMSG_DUMP_PANIC:
1399f244e9cSSeiji Aguchi 	/* Emergency restart shouldn't be blocked by spin lock. */
1409f244e9cSSeiji Aguchi 	case KMSG_DUMP_EMERG:
1419f244e9cSSeiji Aguchi 		return true;
1429f244e9cSSeiji Aguchi 	default:
1439f244e9cSSeiji Aguchi 		return false;
1449f244e9cSSeiji Aguchi 	}
1459f244e9cSSeiji Aguchi }
1469f244e9cSSeiji Aguchi EXPORT_SYMBOL_GPL(pstore_cannot_block_path);
1479f244e9cSSeiji Aguchi 
14858eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
149cb3bee03SGeliang Tang static int zbufsize_deflate(size_t size)
150b0aad7a9SAruna Balakrishnaiah {
1517de8fe2fSAruna Balakrishnaiah 	size_t cmpr;
152b0aad7a9SAruna Balakrishnaiah 
153cb3bee03SGeliang Tang 	switch (size) {
1547de8fe2fSAruna Balakrishnaiah 	/* buffer range for efivars */
1557de8fe2fSAruna Balakrishnaiah 	case 1000 ... 2000:
1567de8fe2fSAruna Balakrishnaiah 		cmpr = 56;
1577de8fe2fSAruna Balakrishnaiah 		break;
1587de8fe2fSAruna Balakrishnaiah 	case 2001 ... 3000:
1597de8fe2fSAruna Balakrishnaiah 		cmpr = 54;
1607de8fe2fSAruna Balakrishnaiah 		break;
1617de8fe2fSAruna Balakrishnaiah 	case 3001 ... 3999:
1627de8fe2fSAruna Balakrishnaiah 		cmpr = 52;
1637de8fe2fSAruna Balakrishnaiah 		break;
1647de8fe2fSAruna Balakrishnaiah 	/* buffer range for nvram, erst */
1657de8fe2fSAruna Balakrishnaiah 	case 4000 ... 10000:
1667de8fe2fSAruna Balakrishnaiah 		cmpr = 45;
1677de8fe2fSAruna Balakrishnaiah 		break;
1687de8fe2fSAruna Balakrishnaiah 	default:
1697de8fe2fSAruna Balakrishnaiah 		cmpr = 60;
1707de8fe2fSAruna Balakrishnaiah 		break;
1717de8fe2fSAruna Balakrishnaiah 	}
1727de8fe2fSAruna Balakrishnaiah 
173cb3bee03SGeliang Tang 	return (size * 100) / cmpr;
1748cfc8ddcSGeliang Tang }
1758cfc8ddcSGeliang Tang #endif
1768cfc8ddcSGeliang Tang 
17758eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
178cb3bee03SGeliang Tang static int zbufsize_lzo(size_t size)
1798cfc8ddcSGeliang Tang {
180cb3bee03SGeliang Tang 	return lzo1x_worst_compress(size);
1818cfc8ddcSGeliang Tang }
1828cfc8ddcSGeliang Tang #endif
1838cfc8ddcSGeliang Tang 
18458eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
185cb3bee03SGeliang Tang static int zbufsize_lz4(size_t size)
1868cfc8ddcSGeliang Tang {
187cb3bee03SGeliang Tang 	return LZ4_compressBound(size);
188239b7161SGeliang Tang }
189239b7161SGeliang Tang #endif
190239b7161SGeliang Tang 
19158eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
192cb3bee03SGeliang Tang static int zbufsize_842(size_t size)
193239b7161SGeliang Tang {
19455597406SKees Cook 	return size;
195239b7161SGeliang Tang }
196fe1d4758SKees Cook #endif
1978cfc8ddcSGeliang Tang 
1981021bcf4SGeliang Tang #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
1991021bcf4SGeliang Tang static int zbufsize_zstd(size_t size)
2001021bcf4SGeliang Tang {
2011021bcf4SGeliang Tang 	return ZSTD_compressBound(size);
2021021bcf4SGeliang Tang }
2031021bcf4SGeliang Tang #endif
2041021bcf4SGeliang Tang 
205fe1d4758SKees Cook static const struct pstore_zbackend *zbackend __ro_after_init;
206fe1d4758SKees Cook 
207fe1d4758SKees Cook static const struct pstore_zbackend zbackends[] = {
20858eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
209fe1d4758SKees Cook 	{
210cb3bee03SGeliang Tang 		.zbufsize	= zbufsize_deflate,
211cb3bee03SGeliang Tang 		.name		= "deflate",
212fe1d4758SKees Cook 	},
213fe1d4758SKees Cook #endif
21458eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
215fe1d4758SKees Cook 	{
216cb3bee03SGeliang Tang 		.zbufsize	= zbufsize_lzo,
217fe1d4758SKees Cook 		.name		= "lzo",
218fe1d4758SKees Cook 	},
219fe1d4758SKees Cook #endif
22058eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS)
221fe1d4758SKees Cook 	{
222cb3bee03SGeliang Tang 		.zbufsize	= zbufsize_lz4,
223fe1d4758SKees Cook 		.name		= "lz4",
224fe1d4758SKees Cook 	},
225fe1d4758SKees Cook #endif
22658eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
227fe1d4758SKees Cook 	{
228cb3bee03SGeliang Tang 		.zbufsize	= zbufsize_lz4,
229fe1d4758SKees Cook 		.name		= "lz4hc",
230fe1d4758SKees Cook 	},
231fe1d4758SKees Cook #endif
23258eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
233fe1d4758SKees Cook 	{
234cb3bee03SGeliang Tang 		.zbufsize	= zbufsize_842,
235239b7161SGeliang Tang 		.name		= "842",
236fe1d4758SKees Cook 	},
237fe1d4758SKees Cook #endif
2381021bcf4SGeliang Tang #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
2391021bcf4SGeliang Tang 	{
2401021bcf4SGeliang Tang 		.zbufsize	= zbufsize_zstd,
2411021bcf4SGeliang Tang 		.name		= "zstd",
2421021bcf4SGeliang Tang 	},
2431021bcf4SGeliang Tang #endif
244fe1d4758SKees Cook 	{ }
2458cfc8ddcSGeliang Tang };
2468cfc8ddcSGeliang Tang 
2478cfc8ddcSGeliang Tang static int pstore_compress(const void *in, void *out,
248cb3bee03SGeliang Tang 			   unsigned int inlen, unsigned int outlen)
2498cfc8ddcSGeliang Tang {
250cb3bee03SGeliang Tang 	int ret;
251cb3bee03SGeliang Tang 
252cb3bee03SGeliang Tang 	ret = crypto_comp_compress(tfm, in, inlen, out, &outlen);
253cb3bee03SGeliang Tang 	if (ret) {
254cb3bee03SGeliang Tang 		pr_err("crypto_comp_compress failed, ret = %d!\n", ret);
255cb3bee03SGeliang Tang 		return ret;
2568cfc8ddcSGeliang Tang 	}
2578cfc8ddcSGeliang Tang 
258cb3bee03SGeliang Tang 	return outlen;
259cb3bee03SGeliang Tang }
260cb3bee03SGeliang Tang 
261cb3bee03SGeliang Tang static int pstore_decompress(void *in, void *out,
262cb3bee03SGeliang Tang 			     unsigned int inlen, unsigned int outlen)
2638cfc8ddcSGeliang Tang {
264cb3bee03SGeliang Tang 	int ret;
265cb3bee03SGeliang Tang 
266cb3bee03SGeliang Tang 	ret = crypto_comp_decompress(tfm, in, inlen, out, &outlen);
267cb3bee03SGeliang Tang 	if (ret) {
268cb3bee03SGeliang Tang 		pr_err("crypto_comp_decompress failed, ret = %d!\n", ret);
269cb3bee03SGeliang Tang 		return ret;
270cb3bee03SGeliang Tang 	}
271cb3bee03SGeliang Tang 
272cb3bee03SGeliang Tang 	return outlen;
2738cfc8ddcSGeliang Tang }
2748cfc8ddcSGeliang Tang 
2758cfc8ddcSGeliang Tang static void allocate_buf_for_compression(void)
2768cfc8ddcSGeliang Tang {
277*95047b05SKees Cook 	struct crypto_comp *ctx;
278*95047b05SKees Cook 	int size;
279*95047b05SKees Cook 	char *buf;
280*95047b05SKees Cook 
281*95047b05SKees Cook 	/* Skip if not built-in or compression backend not selected yet. */
282e698aaf3STobias Regnery 	if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !zbackend)
283cb3bee03SGeliang Tang 		return;
284cb3bee03SGeliang Tang 
285*95047b05SKees Cook 	/* Skip if no pstore backend yet or compression init already done. */
286*95047b05SKees Cook 	if (!psinfo || tfm)
287*95047b05SKees Cook 		return;
288*95047b05SKees Cook 
289cb3bee03SGeliang Tang 	if (!crypto_has_comp(zbackend->name, 0, 0)) {
290*95047b05SKees Cook 		pr_err("Unknown compression: %s\n", zbackend->name);
291cb3bee03SGeliang Tang 		return;
292cb3bee03SGeliang Tang 	}
293cb3bee03SGeliang Tang 
294*95047b05SKees Cook 	size = zbackend->zbufsize(psinfo->bufsize);
295*95047b05SKees Cook 	if (size <= 0) {
296*95047b05SKees Cook 		pr_err("Invalid compression size for %s: %d\n",
297*95047b05SKees Cook 		       zbackend->name, size);
298cb3bee03SGeliang Tang 		return;
299cb3bee03SGeliang Tang 	}
300cb3bee03SGeliang Tang 
301*95047b05SKees Cook 	buf = kmalloc(size, GFP_KERNEL);
302*95047b05SKees Cook 	if (!buf) {
303*95047b05SKees Cook 		pr_err("Failed %d byte compression buffer allocation for: %s\n",
304*95047b05SKees Cook 		       size, zbackend->name);
305cb3bee03SGeliang Tang 		return;
3068cfc8ddcSGeliang Tang 	}
307*95047b05SKees Cook 
308*95047b05SKees Cook 	ctx = crypto_alloc_comp(zbackend->name, 0, 0);
309*95047b05SKees Cook 	if (IS_ERR_OR_NULL(ctx)) {
310*95047b05SKees Cook 		kfree(buf);
311*95047b05SKees Cook 		pr_err("crypto_alloc_comp('%s') failed: %ld\n", zbackend->name,
312*95047b05SKees Cook 		       PTR_ERR(ctx));
313*95047b05SKees Cook 		return;
314*95047b05SKees Cook 	}
315*95047b05SKees Cook 
316*95047b05SKees Cook 	/* A non-NULL big_oops_buf indicates compression is available. */
317*95047b05SKees Cook 	tfm = ctx;
318*95047b05SKees Cook 	big_oops_buf_sz = size;
319*95047b05SKees Cook 	big_oops_buf = buf;
320*95047b05SKees Cook 
321*95047b05SKees Cook 	pr_info("Using compression: %s\n", zbackend->name);
3228cfc8ddcSGeliang Tang }
3238cfc8ddcSGeliang Tang 
3248cfc8ddcSGeliang Tang static void free_buf_for_compression(void)
3258cfc8ddcSGeliang Tang {
326*95047b05SKees Cook 	if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && tfm)
327cb3bee03SGeliang Tang 		crypto_free_comp(tfm);
328cb3bee03SGeliang Tang 	kfree(big_oops_buf);
329cb3bee03SGeliang Tang 	big_oops_buf = NULL;
330cb3bee03SGeliang Tang 	big_oops_buf_sz = 0;
331ee1d2674SGeliang Tang }
332ee1d2674SGeliang Tang 
333b0aad7a9SAruna Balakrishnaiah /*
334b0aad7a9SAruna Balakrishnaiah  * Called when compression fails, since the printk buffer
335b0aad7a9SAruna Balakrishnaiah  * would be fetched for compression calling it again when
336b0aad7a9SAruna Balakrishnaiah  * compression fails would have moved the iterator of
337b0aad7a9SAruna Balakrishnaiah  * printk buffer which results in fetching old contents.
338b0aad7a9SAruna Balakrishnaiah  * Copy the recent messages from big_oops_buf to psinfo->buf
339b0aad7a9SAruna Balakrishnaiah  */
340b0aad7a9SAruna Balakrishnaiah static size_t copy_kmsg_to_buffer(int hsize, size_t len)
341b0aad7a9SAruna Balakrishnaiah {
342b0aad7a9SAruna Balakrishnaiah 	size_t total_len;
343b0aad7a9SAruna Balakrishnaiah 	size_t diff;
344b0aad7a9SAruna Balakrishnaiah 
345b0aad7a9SAruna Balakrishnaiah 	total_len = hsize + len;
346b0aad7a9SAruna Balakrishnaiah 
347b0aad7a9SAruna Balakrishnaiah 	if (total_len > psinfo->bufsize) {
348b0aad7a9SAruna Balakrishnaiah 		diff = total_len - psinfo->bufsize + hsize;
349b0aad7a9SAruna Balakrishnaiah 		memcpy(psinfo->buf, big_oops_buf, hsize);
350b0aad7a9SAruna Balakrishnaiah 		memcpy(psinfo->buf + hsize, big_oops_buf + diff,
351b0aad7a9SAruna Balakrishnaiah 					psinfo->bufsize - hsize);
352b0aad7a9SAruna Balakrishnaiah 		total_len = psinfo->bufsize;
353b0aad7a9SAruna Balakrishnaiah 	} else
354b0aad7a9SAruna Balakrishnaiah 		memcpy(psinfo->buf, big_oops_buf, total_len);
355b0aad7a9SAruna Balakrishnaiah 
356b0aad7a9SAruna Balakrishnaiah 	return total_len;
357b0aad7a9SAruna Balakrishnaiah }
358b0aad7a9SAruna Balakrishnaiah 
359e581ca81SKees Cook void pstore_record_init(struct pstore_record *record,
360e581ca81SKees Cook 			struct pstore_info *psinfo)
361e581ca81SKees Cook {
362e581ca81SKees Cook 	memset(record, 0, sizeof(*record));
363e581ca81SKees Cook 
364e581ca81SKees Cook 	record->psi = psinfo;
365c7f3c595SKees Cook 
366c7f3c595SKees Cook 	/* Report zeroed timestamp if called before timekeeping has resumed. */
3677aaa822eSKees Cook 	record->time = ns_to_timespec64(ktime_get_real_fast_ns());
368e581ca81SKees Cook }
369e581ca81SKees Cook 
370ca01d6ddSTony Luck /*
371ca01d6ddSTony Luck  * callback from kmsg_dump. (s2,l2) has the most recently
372ca01d6ddSTony Luck  * written bytes, older bytes are in (s1,l1). Save as much
373ca01d6ddSTony Luck  * as we can from the end of the buffer.
374ca01d6ddSTony Luck  */
375ca01d6ddSTony Luck static void pstore_dump(struct kmsg_dumper *dumper,
376e2ae715dSKay Sievers 			enum kmsg_dump_reason reason)
377ca01d6ddSTony Luck {
378e2ae715dSKay Sievers 	unsigned long	total = 0;
379381b872cSSeiji Aguchi 	const char	*why;
380b94fdd07SMatthew Garrett 	unsigned int	part = 1;
381abd4d558SDon Zickus 	unsigned long	flags = 0;
38298e44fdaSNamhyung Kim 	int		is_locked;
383e2ae715dSKay Sievers 	int		ret;
384ca01d6ddSTony Luck 
385381b872cSSeiji Aguchi 	why = get_reason_str(reason);
3869f6af27fSTony Luck 
3879f244e9cSSeiji Aguchi 	if (pstore_cannot_block_path(reason)) {
3889f244e9cSSeiji Aguchi 		is_locked = spin_trylock_irqsave(&psinfo->buf_lock, flags);
3899f244e9cSSeiji Aguchi 		if (!is_locked) {
3909f244e9cSSeiji Aguchi 			pr_err("pstore dump routine blocked in %s path, may corrupt error record\n"
3919f244e9cSSeiji Aguchi 				       , in_nmi() ? "NMI" : why);
392959217c8SLi Pengcheng 			return;
3939f244e9cSSeiji Aguchi 		}
39498e44fdaSNamhyung Kim 	} else {
395abd4d558SDon Zickus 		spin_lock_irqsave(&psinfo->buf_lock, flags);
39698e44fdaSNamhyung Kim 		is_locked = 1;
39798e44fdaSNamhyung Kim 	}
398ca01d6ddSTony Luck 	oopscount++;
399ca01d6ddSTony Luck 	while (total < kmsg_bytes) {
400e2ae715dSKay Sievers 		char *dst;
40176cc9580SKees Cook 		size_t dst_size;
40276cc9580SKees Cook 		int header_size;
403b0aad7a9SAruna Balakrishnaiah 		int zipped_len = -1;
40476cc9580SKees Cook 		size_t dump_size;
405e581ca81SKees Cook 		struct pstore_record record;
406e581ca81SKees Cook 
407e581ca81SKees Cook 		pstore_record_init(&record, psinfo);
408e581ca81SKees Cook 		record.type = PSTORE_TYPE_DMESG;
409e581ca81SKees Cook 		record.count = oopscount;
410e581ca81SKees Cook 		record.reason = reason;
411e581ca81SKees Cook 		record.part = part;
412e581ca81SKees Cook 		record.buf = psinfo->buf;
413e2ae715dSKay Sievers 
414f0e2efcfSKonstantin Khlebnikov 		if (big_oops_buf && is_locked) {
415b0aad7a9SAruna Balakrishnaiah 			dst = big_oops_buf;
41676cc9580SKees Cook 			dst_size = big_oops_buf_sz;
417235f6d15SNamhyung Kim 		} else {
418235f6d15SNamhyung Kim 			dst = psinfo->buf;
41976cc9580SKees Cook 			dst_size = psinfo->bufsize;
420235f6d15SNamhyung Kim 		}
421235f6d15SNamhyung Kim 
42276cc9580SKees Cook 		/* Write dump header. */
42376cc9580SKees Cook 		header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why,
42476cc9580SKees Cook 				 oopscount, part);
42576cc9580SKees Cook 		dst_size -= header_size;
426b0aad7a9SAruna Balakrishnaiah 
42776cc9580SKees Cook 		/* Write dump contents. */
42876cc9580SKees Cook 		if (!kmsg_dump_get_buffer(dumper, true, dst + header_size,
42976cc9580SKees Cook 					  dst_size, &dump_size))
430b0aad7a9SAruna Balakrishnaiah 			break;
431b0aad7a9SAruna Balakrishnaiah 
432235f6d15SNamhyung Kim 		if (big_oops_buf && is_locked) {
433b0aad7a9SAruna Balakrishnaiah 			zipped_len = pstore_compress(dst, psinfo->buf,
43476cc9580SKees Cook 						header_size + dump_size,
43576cc9580SKees Cook 						psinfo->bufsize);
436b0aad7a9SAruna Balakrishnaiah 
437b0aad7a9SAruna Balakrishnaiah 			if (zipped_len > 0) {
43876cc9580SKees Cook 				record.compressed = true;
43976cc9580SKees Cook 				record.size = zipped_len;
440b0aad7a9SAruna Balakrishnaiah 			} else {
44176cc9580SKees Cook 				record.size = copy_kmsg_to_buffer(header_size,
44276cc9580SKees Cook 								  dump_size);
443b0aad7a9SAruna Balakrishnaiah 			}
444b0aad7a9SAruna Balakrishnaiah 		} else {
44576cc9580SKees Cook 			record.size = header_size + dump_size;
446b0aad7a9SAruna Balakrishnaiah 		}
447b0aad7a9SAruna Balakrishnaiah 
44876cc9580SKees Cook 		ret = psinfo->write(&record);
449b238b8faSChen Gong 		if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted())
4506dda9266SLuck, Tony 			pstore_new_entry = 1;
451e2ae715dSKay Sievers 
45276cc9580SKees Cook 		total += record.size;
45356280682SMatthew Garrett 		part++;
454ca01d6ddSTony Luck 	}
455abd4d558SDon Zickus 	if (is_locked)
4569f244e9cSSeiji Aguchi 		spin_unlock_irqrestore(&psinfo->buf_lock, flags);
457ca01d6ddSTony Luck }
458ca01d6ddSTony Luck 
459ca01d6ddSTony Luck static struct kmsg_dumper pstore_dumper = {
460ca01d6ddSTony Luck 	.dump = pstore_dump,
461ca01d6ddSTony Luck };
462ca01d6ddSTony Luck 
463306e5c2aSGeliang Tang /*
464306e5c2aSGeliang Tang  * Register with kmsg_dump to save last part of console log on panic.
465306e5c2aSGeliang Tang  */
46618730411SGeliang Tang static void pstore_register_kmsg(void)
46718730411SGeliang Tang {
46818730411SGeliang Tang 	kmsg_dump_register(&pstore_dumper);
46918730411SGeliang Tang }
47018730411SGeliang Tang 
471ee1d2674SGeliang Tang static void pstore_unregister_kmsg(void)
472ee1d2674SGeliang Tang {
473ee1d2674SGeliang Tang 	kmsg_dump_unregister(&pstore_dumper);
474ee1d2674SGeliang Tang }
475ee1d2674SGeliang Tang 
476f29e5956SAnton Vorontsov #ifdef CONFIG_PSTORE_CONSOLE
477f29e5956SAnton Vorontsov static void pstore_console_write(struct console *con, const char *s, unsigned c)
478f29e5956SAnton Vorontsov {
479f29e5956SAnton Vorontsov 	const char *e = s + c;
480f29e5956SAnton Vorontsov 
481f29e5956SAnton Vorontsov 	while (s < e) {
482e581ca81SKees Cook 		struct pstore_record record;
483f29e5956SAnton Vorontsov 		unsigned long flags;
484f29e5956SAnton Vorontsov 
485e581ca81SKees Cook 		pstore_record_init(&record, psinfo);
486e581ca81SKees Cook 		record.type = PSTORE_TYPE_CONSOLE;
487e581ca81SKees Cook 
488f29e5956SAnton Vorontsov 		if (c > psinfo->bufsize)
489f29e5956SAnton Vorontsov 			c = psinfo->bufsize;
49080c9d03cSChuansheng Liu 
49180c9d03cSChuansheng Liu 		if (oops_in_progress) {
49280c9d03cSChuansheng Liu 			if (!spin_trylock_irqsave(&psinfo->buf_lock, flags))
49380c9d03cSChuansheng Liu 				break;
49480c9d03cSChuansheng Liu 		} else {
495f29e5956SAnton Vorontsov 			spin_lock_irqsave(&psinfo->buf_lock, flags);
49680c9d03cSChuansheng Liu 		}
497b10b4711SKees Cook 		record.buf = (char *)s;
498b10b4711SKees Cook 		record.size = c;
4994c9ec219SKees Cook 		psinfo->write(&record);
500f29e5956SAnton Vorontsov 		spin_unlock_irqrestore(&psinfo->buf_lock, flags);
501f29e5956SAnton Vorontsov 		s += c;
502f29e5956SAnton Vorontsov 		c = e - s;
503f29e5956SAnton Vorontsov 	}
504f29e5956SAnton Vorontsov }
505f29e5956SAnton Vorontsov 
506f29e5956SAnton Vorontsov static struct console pstore_console = {
507f29e5956SAnton Vorontsov 	.name	= "pstore",
508f29e5956SAnton Vorontsov 	.write	= pstore_console_write,
509f29e5956SAnton Vorontsov 	.flags	= CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME,
510f29e5956SAnton Vorontsov 	.index	= -1,
511f29e5956SAnton Vorontsov };
512f29e5956SAnton Vorontsov 
513f29e5956SAnton Vorontsov static void pstore_register_console(void)
514f29e5956SAnton Vorontsov {
515f29e5956SAnton Vorontsov 	register_console(&pstore_console);
516f29e5956SAnton Vorontsov }
517ee1d2674SGeliang Tang 
518ee1d2674SGeliang Tang static void pstore_unregister_console(void)
519ee1d2674SGeliang Tang {
520ee1d2674SGeliang Tang 	unregister_console(&pstore_console);
521ee1d2674SGeliang Tang }
522f29e5956SAnton Vorontsov #else
523f29e5956SAnton Vorontsov static void pstore_register_console(void) {}
524ee1d2674SGeliang Tang static void pstore_unregister_console(void) {}
525f29e5956SAnton Vorontsov #endif
526f29e5956SAnton Vorontsov 
5274c9ec219SKees Cook static int pstore_write_user_compat(struct pstore_record *record,
528fdd03118SKees Cook 				    const char __user *buf)
5295bf6d1b9SMark Salyzyn {
53030800d99SKees Cook 	int ret = 0;
5315bf6d1b9SMark Salyzyn 
53230800d99SKees Cook 	if (record->buf)
53330800d99SKees Cook 		return -EINVAL;
5345bf6d1b9SMark Salyzyn 
535077090afSGeliang Tang 	record->buf = memdup_user(buf, record->size);
536dfd6fa39SHirofumi Nakagawa 	if (IS_ERR(record->buf)) {
537077090afSGeliang Tang 		ret = PTR_ERR(record->buf);
53830800d99SKees Cook 		goto out;
5395bf6d1b9SMark Salyzyn 	}
54030800d99SKees Cook 
5414c9ec219SKees Cook 	ret = record->psi->write(record);
54230800d99SKees Cook 
54330800d99SKees Cook 	kfree(record->buf);
544077090afSGeliang Tang out:
54530800d99SKees Cook 	record->buf = NULL;
54630800d99SKees Cook 
54730800d99SKees Cook 	return unlikely(ret < 0) ? ret : record->size;
5485bf6d1b9SMark Salyzyn }
5495bf6d1b9SMark Salyzyn 
550ca01d6ddSTony Luck /*
551ca01d6ddSTony Luck  * platform specific persistent storage driver registers with
552ca01d6ddSTony Luck  * us here. If pstore is already mounted, call the platform
553ca01d6ddSTony Luck  * read function right away to populate the file system. If not
554ca01d6ddSTony Luck  * then the pstore mount code will call us later to fill out
555ca01d6ddSTony Luck  * the file system.
556ca01d6ddSTony Luck  */
557ca01d6ddSTony Luck int pstore_register(struct pstore_info *psi)
558ca01d6ddSTony Luck {
559ca01d6ddSTony Luck 	struct module *owner = psi->owner;
560ca01d6ddSTony Luck 
5610d7cd09aSKees Cook 	if (backend && strcmp(backend, psi->name)) {
5620d7cd09aSKees Cook 		pr_warn("ignoring unexpected backend '%s'\n", psi->name);
5638e48b1a8SLenny Szubowicz 		return -EPERM;
5640d7cd09aSKees Cook 	}
5658e48b1a8SLenny Szubowicz 
5664c9ec219SKees Cook 	/* Sanity check flags. */
5674c9ec219SKees Cook 	if (!psi->flags) {
5684c9ec219SKees Cook 		pr_warn("backend '%s' must support at least one frontend\n",
5694c9ec219SKees Cook 			psi->name);
5704c9ec219SKees Cook 		return -EINVAL;
5714c9ec219SKees Cook 	}
5724c9ec219SKees Cook 
5734c9ec219SKees Cook 	/* Check for required functions. */
5744c9ec219SKees Cook 	if (!psi->read || !psi->write) {
5754c9ec219SKees Cook 		pr_warn("backend '%s' must implement read() and write()\n",
5764c9ec219SKees Cook 			psi->name);
5774c9ec219SKees Cook 		return -EINVAL;
5784c9ec219SKees Cook 	}
5794c9ec219SKees Cook 
580ca01d6ddSTony Luck 	spin_lock(&pstore_lock);
581ca01d6ddSTony Luck 	if (psinfo) {
5820d7cd09aSKees Cook 		pr_warn("backend '%s' already loaded: ignoring '%s'\n",
5830d7cd09aSKees Cook 			psinfo->name, psi->name);
584ca01d6ddSTony Luck 		spin_unlock(&pstore_lock);
585ca01d6ddSTony Luck 		return -EBUSY;
586ca01d6ddSTony Luck 	}
587dee28e72SMatthew Garrett 
5884c9ec219SKees Cook 	if (!psi->write_user)
5894c9ec219SKees Cook 		psi->write_user = pstore_write_user_compat;
590ca01d6ddSTony Luck 	psinfo = psi;
591f6f82851SKees Cook 	mutex_init(&psinfo->read_mutex);
592ca01d6ddSTony Luck 	spin_unlock(&pstore_lock);
593ca01d6ddSTony Luck 
594ca01d6ddSTony Luck 	if (owner && !try_module_get(owner)) {
595ca01d6ddSTony Luck 		psinfo = NULL;
596ca01d6ddSTony Luck 		return -EINVAL;
597ca01d6ddSTony Luck 	}
598ca01d6ddSTony Luck 
599b0aad7a9SAruna Balakrishnaiah 	allocate_buf_for_compression();
600b0aad7a9SAruna Balakrishnaiah 
601ca01d6ddSTony Luck 	if (pstore_is_mounted())
6026dda9266SLuck, Tony 		pstore_get_records(0);
603ca01d6ddSTony Luck 
604c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_DMESG)
60518730411SGeliang Tang 		pstore_register_kmsg();
606c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_CONSOLE)
607f29e5956SAnton Vorontsov 		pstore_register_console();
608c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_FTRACE)
60965f8c95eSAnton Vorontsov 		pstore_register_ftrace();
610c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_PMSG)
6119d5438f4SMark Salyzyn 		pstore_register_pmsg();
612ca01d6ddSTony Luck 
6136330d553SKees Cook 	/* Start watching for new records, if desired. */
614a3f5f075SAnton Vorontsov 	if (pstore_update_ms >= 0) {
615a3f5f075SAnton Vorontsov 		pstore_timer.expires = jiffies +
616a3f5f075SAnton Vorontsov 			msecs_to_jiffies(pstore_update_ms);
6176dda9266SLuck, Tony 		add_timer(&pstore_timer);
618a3f5f075SAnton Vorontsov 	}
6196dda9266SLuck, Tony 
62042222c2aSWang Long 	/*
62142222c2aSWang Long 	 * Update the module parameter backend, so it is visible
62242222c2aSWang Long 	 * through /sys/module/pstore/parameters/backend
62342222c2aSWang Long 	 */
62442222c2aSWang Long 	backend = psi->name;
62542222c2aSWang Long 
626ef748853SFabian Frederick 	pr_info("Registered %s as persistent store backend\n", psi->name);
6278e48b1a8SLenny Szubowicz 
6281344dd86SKees Cook 	module_put(owner);
6291344dd86SKees Cook 
630ca01d6ddSTony Luck 	return 0;
631ca01d6ddSTony Luck }
632ca01d6ddSTony Luck EXPORT_SYMBOL_GPL(pstore_register);
633ca01d6ddSTony Luck 
634ee1d2674SGeliang Tang void pstore_unregister(struct pstore_info *psi)
635ee1d2674SGeliang Tang {
6366330d553SKees Cook 	/* Stop timer and make sure all work has finished. */
6376330d553SKees Cook 	pstore_update_ms = -1;
6386330d553SKees Cook 	del_timer_sync(&pstore_timer);
6396330d553SKees Cook 	flush_work(&pstore_work);
6406330d553SKees Cook 
641c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_PMSG)
642ee1d2674SGeliang Tang 		pstore_unregister_pmsg();
643c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_FTRACE)
644ee1d2674SGeliang Tang 		pstore_unregister_ftrace();
645c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_CONSOLE)
646ee1d2674SGeliang Tang 		pstore_unregister_console();
647c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_DMESG)
648ee1d2674SGeliang Tang 		pstore_unregister_kmsg();
649ee1d2674SGeliang Tang 
650ee1d2674SGeliang Tang 	free_buf_for_compression();
651ee1d2674SGeliang Tang 
652ee1d2674SGeliang Tang 	psinfo = NULL;
653ee1d2674SGeliang Tang 	backend = NULL;
654ee1d2674SGeliang Tang }
655ee1d2674SGeliang Tang EXPORT_SYMBOL_GPL(pstore_unregister);
656ee1d2674SGeliang Tang 
657634f8f51SKees Cook static void decompress_record(struct pstore_record *record)
658634f8f51SKees Cook {
659634f8f51SKees Cook 	int unzipped_len;
6607e8cc8dcSKees Cook 	char *decompressed;
661634f8f51SKees Cook 
6624a16d1cbSAnkit Kumar 	if (!record->compressed)
6634a16d1cbSAnkit Kumar 		return;
6644a16d1cbSAnkit Kumar 
665634f8f51SKees Cook 	/* Only PSTORE_TYPE_DMESG support compression. */
6664a16d1cbSAnkit Kumar 	if (record->type != PSTORE_TYPE_DMESG) {
667634f8f51SKees Cook 		pr_warn("ignored compressed record type %d\n", record->type);
668634f8f51SKees Cook 		return;
669634f8f51SKees Cook 	}
670634f8f51SKees Cook 
671634f8f51SKees Cook 	/* No compression method has created the common buffer. */
672634f8f51SKees Cook 	if (!big_oops_buf) {
673634f8f51SKees Cook 		pr_warn("no decompression buffer allocated\n");
674634f8f51SKees Cook 		return;
675634f8f51SKees Cook 	}
676634f8f51SKees Cook 
677634f8f51SKees Cook 	unzipped_len = pstore_decompress(record->buf, big_oops_buf,
678634f8f51SKees Cook 					 record->size, big_oops_buf_sz);
6797e8cc8dcSKees Cook 	if (unzipped_len <= 0) {
6807e8cc8dcSKees Cook 		pr_err("decompression failed: %d\n", unzipped_len);
6817e8cc8dcSKees Cook 		return;
6827e8cc8dcSKees Cook 	}
6837e8cc8dcSKees Cook 
6847e8cc8dcSKees Cook 	/* Build new buffer for decompressed contents. */
6857e8cc8dcSKees Cook 	decompressed = kmalloc(unzipped_len + record->ecc_notice_size,
6867e8cc8dcSKees Cook 			       GFP_KERNEL);
6877e8cc8dcSKees Cook 	if (!decompressed) {
6887e8cc8dcSKees Cook 		pr_err("decompression ran out of memory\n");
6897e8cc8dcSKees Cook 		return;
6907e8cc8dcSKees Cook 	}
6917e8cc8dcSKees Cook 	memcpy(decompressed, big_oops_buf, unzipped_len);
6927e8cc8dcSKees Cook 
6937e8cc8dcSKees Cook 	/* Append ECC notice to decompressed buffer. */
6947e8cc8dcSKees Cook 	memcpy(decompressed + unzipped_len, record->buf + record->size,
695634f8f51SKees Cook 	       record->ecc_notice_size);
6967e8cc8dcSKees Cook 
6977e8cc8dcSKees Cook 	/* Swap out compresed contents with decompressed contents. */
698634f8f51SKees Cook 	kfree(record->buf);
6997e8cc8dcSKees Cook 	record->buf = decompressed;
700634f8f51SKees Cook 	record->size = unzipped_len;
701634f8f51SKees Cook 	record->compressed = false;
702634f8f51SKees Cook }
703634f8f51SKees Cook 
704ca01d6ddSTony Luck /*
7053a7d2fd1SKees Cook  * Read all the records from one persistent store backend. Create
7066dda9266SLuck, Tony  * files in our filesystem.  Don't warn about -EEXIST errors
7076dda9266SLuck, Tony  * when we are re-scanning the backing store looking to add new
7086dda9266SLuck, Tony  * error records.
709ca01d6ddSTony Luck  */
7103a7d2fd1SKees Cook void pstore_get_backend_records(struct pstore_info *psi,
7113a7d2fd1SKees Cook 				struct dentry *root, int quiet)
712ca01d6ddSTony Luck {
7132a2b0acfSKees Cook 	int failed = 0;
714656de42eSKees Cook 	unsigned int stop_loop = 65536;
715ca01d6ddSTony Luck 
7163a7d2fd1SKees Cook 	if (!psi || !root)
717ca01d6ddSTony Luck 		return;
718ca01d6ddSTony Luck 
719f6f82851SKees Cook 	mutex_lock(&psi->read_mutex);
7202174f6dfSKees Cook 	if (psi->open && psi->open(psi))
72106cf91b4SChen Gong 		goto out;
72206cf91b4SChen Gong 
7231dfff7ddSKees Cook 	/*
7241dfff7ddSKees Cook 	 * Backend callback read() allocates record.buf. decompress_record()
7251dfff7ddSKees Cook 	 * may reallocate record.buf. On success, pstore_mkfile() will keep
7261dfff7ddSKees Cook 	 * the record.buf, so free it only on failure.
7271dfff7ddSKees Cook 	 */
728656de42eSKees Cook 	for (; stop_loop; stop_loop--) {
7292a2b0acfSKees Cook 		struct pstore_record *record;
7302a2b0acfSKees Cook 		int rc;
7312a2b0acfSKees Cook 
7322a2b0acfSKees Cook 		record = kzalloc(sizeof(*record), GFP_KERNEL);
7332a2b0acfSKees Cook 		if (!record) {
7342a2b0acfSKees Cook 			pr_err("out of memory creating record\n");
7352a2b0acfSKees Cook 			break;
7362a2b0acfSKees Cook 		}
737e581ca81SKees Cook 		pstore_record_init(record, psi);
7382a2b0acfSKees Cook 
7392a2b0acfSKees Cook 		record->size = psi->read(record);
7402a2b0acfSKees Cook 
7412a2b0acfSKees Cook 		/* No more records left in backend? */
742f6525b96SDouglas Anderson 		if (record->size <= 0) {
743f6525b96SDouglas Anderson 			kfree(record);
7442a2b0acfSKees Cook 			break;
745f6525b96SDouglas Anderson 		}
7462a2b0acfSKees Cook 
7472a2b0acfSKees Cook 		decompress_record(record);
7483a7d2fd1SKees Cook 		rc = pstore_mkfile(root, record);
7491dfff7ddSKees Cook 		if (rc) {
75083f70f07SKees Cook 			/* pstore_mkfile() did not take record, so free it. */
7512a2b0acfSKees Cook 			kfree(record->buf);
75283f70f07SKees Cook 			kfree(record);
7531dfff7ddSKees Cook 			if (rc != -EEXIST || !quiet)
7541dfff7ddSKees Cook 				failed++;
7551dfff7ddSKees Cook 		}
756ca01d6ddSTony Luck 	}
7572174f6dfSKees Cook 	if (psi->close)
75806cf91b4SChen Gong 		psi->close(psi);
75906cf91b4SChen Gong out:
760f6f82851SKees Cook 	mutex_unlock(&psi->read_mutex);
761ca01d6ddSTony Luck 
762ca01d6ddSTony Luck 	if (failed)
763656de42eSKees Cook 		pr_warn("failed to create %d record(s) from '%s'\n",
764ca01d6ddSTony Luck 			failed, psi->name);
765656de42eSKees Cook 	if (!stop_loop)
766656de42eSKees Cook 		pr_err("looping? Too many records seen from '%s'\n",
767656de42eSKees Cook 			psi->name);
768ca01d6ddSTony Luck }
769ca01d6ddSTony Luck 
7706dda9266SLuck, Tony static void pstore_dowork(struct work_struct *work)
7716dda9266SLuck, Tony {
7726dda9266SLuck, Tony 	pstore_get_records(1);
7736dda9266SLuck, Tony }
7746dda9266SLuck, Tony 
77524ed960aSKees Cook static void pstore_timefunc(struct timer_list *unused)
7766dda9266SLuck, Tony {
7776dda9266SLuck, Tony 	if (pstore_new_entry) {
7786dda9266SLuck, Tony 		pstore_new_entry = 0;
7796dda9266SLuck, Tony 		schedule_work(&pstore_work);
7806dda9266SLuck, Tony 	}
7816dda9266SLuck, Tony 
7826330d553SKees Cook 	if (pstore_update_ms >= 0)
7836330d553SKees Cook 		mod_timer(&pstore_timer,
7846330d553SKees Cook 			  jiffies + msecs_to_jiffies(pstore_update_ms));
7856dda9266SLuck, Tony }
7866dda9266SLuck, Tony 
787fe1d4758SKees Cook void __init pstore_choose_compression(void)
788fe1d4758SKees Cook {
789fe1d4758SKees Cook 	const struct pstore_zbackend *step;
790fe1d4758SKees Cook 
791fe1d4758SKees Cook 	if (!compress)
792fe1d4758SKees Cook 		return;
793fe1d4758SKees Cook 
794fe1d4758SKees Cook 	for (step = zbackends; step->name; step++) {
795fe1d4758SKees Cook 		if (!strcmp(compress, step->name)) {
796fe1d4758SKees Cook 			zbackend = step;
797fe1d4758SKees Cook 			return;
798fe1d4758SKees Cook 		}
799fe1d4758SKees Cook 	}
800fe1d4758SKees Cook }
801fe1d4758SKees Cook 
802cb095afdSKees Cook static int __init pstore_init(void)
803cb095afdSKees Cook {
804cb095afdSKees Cook 	int ret;
805cb095afdSKees Cook 
806cb095afdSKees Cook 	pstore_choose_compression();
807cb095afdSKees Cook 
80841603165SJoel Fernandes (Google) 	/*
80941603165SJoel Fernandes (Google) 	 * Check if any pstore backends registered earlier but did not
81041603165SJoel Fernandes (Google) 	 * initialize compression because crypto was not ready. If so,
81141603165SJoel Fernandes (Google) 	 * initialize compression now.
81241603165SJoel Fernandes (Google) 	 */
81341603165SJoel Fernandes (Google) 	allocate_buf_for_compression();
81441603165SJoel Fernandes (Google) 
815cb095afdSKees Cook 	ret = pstore_init_fs();
816cb095afdSKees Cook 	if (ret)
817cb095afdSKees Cook 		return ret;
818cb095afdSKees Cook 
819cb095afdSKees Cook 	return 0;
820cb095afdSKees Cook }
82141603165SJoel Fernandes (Google) late_initcall(pstore_init);
822cb095afdSKees Cook 
823cb095afdSKees Cook static void __exit pstore_exit(void)
824cb095afdSKees Cook {
825cb095afdSKees Cook 	pstore_exit_fs();
826cb095afdSKees Cook }
827cb095afdSKees Cook module_exit(pstore_exit)
828cb095afdSKees Cook 
829fe1d4758SKees Cook module_param(compress, charp, 0444);
830fe1d4758SKees Cook MODULE_PARM_DESC(compress, "Pstore compression to use");
831fe1d4758SKees Cook 
832dee28e72SMatthew Garrett module_param(backend, charp, 0444);
833dee28e72SMatthew Garrett MODULE_PARM_DESC(backend, "Pstore backend to use");
834cb095afdSKees Cook 
835cb095afdSKees Cook MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
836cb095afdSKees Cook MODULE_LICENSE("GPL");
837