xref: /openbmc/linux/fs/pstore/platform.c (revision fe1d475888eecf1319458ee916e642e3e5e41c28)
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>
318cfc8ddcSGeliang Tang #ifdef CONFIG_PSTORE_ZLIB_COMPRESS
32b0aad7a9SAruna Balakrishnaiah #include <linux/zlib.h>
338cfc8ddcSGeliang Tang #endif
348cfc8ddcSGeliang Tang #ifdef CONFIG_PSTORE_LZO_COMPRESS
358cfc8ddcSGeliang Tang #include <linux/lzo.h>
368cfc8ddcSGeliang Tang #endif
37239b7161SGeliang Tang #if defined(CONFIG_PSTORE_LZ4_COMPRESS) || defined(CONFIG_PSTORE_LZ4HC_COMPRESS)
388cfc8ddcSGeliang Tang #include <linux/lz4.h>
398cfc8ddcSGeliang Tang #endif
40239b7161SGeliang Tang #ifdef CONFIG_PSTORE_842_COMPRESS
41239b7161SGeliang Tang #include <linux/sw842.h>
42239b7161SGeliang Tang #endif
43ca01d6ddSTony Luck #include <linux/string.h>
446dda9266SLuck, Tony #include <linux/timer.h>
45ca01d6ddSTony Luck #include <linux/slab.h>
46ca01d6ddSTony Luck #include <linux/uaccess.h>
47a3f5f075SAnton Vorontsov #include <linux/jiffies.h>
486dda9266SLuck, Tony #include <linux/workqueue.h>
49ca01d6ddSTony Luck 
50ca01d6ddSTony Luck #include "internal.h"
51ca01d6ddSTony Luck 
52ca01d6ddSTony Luck /*
536dda9266SLuck, Tony  * We defer making "oops" entries appear in pstore - see
546dda9266SLuck, Tony  * whether the system is actually still running well enough
556dda9266SLuck, Tony  * to let someone see the entry
566dda9266SLuck, Tony  */
57521f7288SAnton Vorontsov static int pstore_update_ms = -1;
58a3f5f075SAnton Vorontsov module_param_named(update_ms, pstore_update_ms, int, 0600);
59a3f5f075SAnton Vorontsov MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content "
60521f7288SAnton Vorontsov 		 "(default is -1, which means runtime updates are disabled; "
61521f7288SAnton Vorontsov 		 "enabling this option is not safe, it may lead to further "
62521f7288SAnton Vorontsov 		 "corruption on Oopses)");
636dda9266SLuck, Tony 
646dda9266SLuck, Tony static int pstore_new_entry;
656dda9266SLuck, Tony 
6624ed960aSKees Cook static void pstore_timefunc(struct timer_list *);
671d27e3e2SKees Cook static DEFINE_TIMER(pstore_timer, pstore_timefunc);
686dda9266SLuck, Tony 
696dda9266SLuck, Tony static void pstore_dowork(struct work_struct *);
706dda9266SLuck, Tony static DECLARE_WORK(pstore_work, pstore_dowork);
716dda9266SLuck, Tony 
726dda9266SLuck, Tony /*
73ca01d6ddSTony Luck  * pstore_lock just protects "psinfo" during
74ca01d6ddSTony Luck  * calls to pstore_register()
75ca01d6ddSTony Luck  */
76ca01d6ddSTony Luck static DEFINE_SPINLOCK(pstore_lock);
77060287b8SAnton Vorontsov struct pstore_info *psinfo;
78ca01d6ddSTony Luck 
79dee28e72SMatthew Garrett static char *backend;
80*fe1d4758SKees Cook static char *compress =
81*fe1d4758SKees Cook #ifdef CONFIG_PSTORE_COMPRESS_DEFAULT
82*fe1d4758SKees Cook 		CONFIG_PSTORE_COMPRESS_DEFAULT;
83*fe1d4758SKees Cook #else
84*fe1d4758SKees Cook 		NULL;
85*fe1d4758SKees Cook #endif
86dee28e72SMatthew Garrett 
87b0aad7a9SAruna Balakrishnaiah /* Compression parameters */
888cfc8ddcSGeliang Tang #ifdef CONFIG_PSTORE_ZLIB_COMPRESS
89b0aad7a9SAruna Balakrishnaiah #define COMPR_LEVEL 6
90b0aad7a9SAruna Balakrishnaiah #define WINDOW_BITS 12
91b0aad7a9SAruna Balakrishnaiah #define MEM_LEVEL 4
92b0aad7a9SAruna Balakrishnaiah static struct z_stream_s stream;
93*fe1d4758SKees Cook #endif
94*fe1d4758SKees Cook #if defined(CONFIG_PSTORE_LZO_COMPRESS)   || \
95*fe1d4758SKees Cook     defined(CONFIG_PSTORE_LZ4_COMPRESS)   || \
96*fe1d4758SKees Cook     defined(CONFIG_PSTORE_LZ4HC_COMPRESS) || \
97*fe1d4758SKees Cook     defined(CONFIG_PSTORE_842_COMPRESS)
988cfc8ddcSGeliang Tang static unsigned char *workspace;
998cfc8ddcSGeliang Tang #endif
1008cfc8ddcSGeliang Tang 
1018cfc8ddcSGeliang Tang struct pstore_zbackend {
1028cfc8ddcSGeliang Tang 	int (*compress)(const void *in, void *out, size_t inlen, size_t outlen);
1038cfc8ddcSGeliang Tang 	int (*decompress)(void *in, void *out, size_t inlen, size_t outlen);
1048cfc8ddcSGeliang Tang 	void (*allocate)(void);
1058cfc8ddcSGeliang Tang 	void (*free)(void);
1068cfc8ddcSGeliang Tang 
1078cfc8ddcSGeliang Tang 	const char *name;
1088cfc8ddcSGeliang Tang };
109b0aad7a9SAruna Balakrishnaiah 
110b0aad7a9SAruna Balakrishnaiah static char *big_oops_buf;
111b0aad7a9SAruna Balakrishnaiah static size_t big_oops_buf_sz;
112b0aad7a9SAruna Balakrishnaiah 
113366f7e7aSLuck, Tony /* How much of the console log to snapshot */
114349d7438SDavid Howells unsigned long kmsg_bytes = PSTORE_DEFAULT_KMSG_BYTES;
115ca01d6ddSTony Luck 
116366f7e7aSLuck, Tony void pstore_set_kmsg_bytes(int bytes)
117ca01d6ddSTony Luck {
118366f7e7aSLuck, Tony 	kmsg_bytes = bytes;
119ca01d6ddSTony Luck }
120ca01d6ddSTony Luck 
121ca01d6ddSTony Luck /* Tag each group of saved records with a sequence number */
122ca01d6ddSTony Luck static int	oopscount;
123ca01d6ddSTony Luck 
124381b872cSSeiji Aguchi static const char *get_reason_str(enum kmsg_dump_reason reason)
125381b872cSSeiji Aguchi {
126381b872cSSeiji Aguchi 	switch (reason) {
127381b872cSSeiji Aguchi 	case KMSG_DUMP_PANIC:
128381b872cSSeiji Aguchi 		return "Panic";
129381b872cSSeiji Aguchi 	case KMSG_DUMP_OOPS:
130381b872cSSeiji Aguchi 		return "Oops";
131381b872cSSeiji Aguchi 	case KMSG_DUMP_EMERG:
132381b872cSSeiji Aguchi 		return "Emergency";
133381b872cSSeiji Aguchi 	case KMSG_DUMP_RESTART:
134381b872cSSeiji Aguchi 		return "Restart";
135381b872cSSeiji Aguchi 	case KMSG_DUMP_HALT:
136381b872cSSeiji Aguchi 		return "Halt";
137381b872cSSeiji Aguchi 	case KMSG_DUMP_POWEROFF:
138381b872cSSeiji Aguchi 		return "Poweroff";
139381b872cSSeiji Aguchi 	default:
140381b872cSSeiji Aguchi 		return "Unknown";
141381b872cSSeiji Aguchi 	}
142381b872cSSeiji Aguchi }
1439f6af27fSTony Luck 
1449f244e9cSSeiji Aguchi bool pstore_cannot_block_path(enum kmsg_dump_reason reason)
1459f244e9cSSeiji Aguchi {
1469f244e9cSSeiji Aguchi 	/*
1479f244e9cSSeiji Aguchi 	 * In case of NMI path, pstore shouldn't be blocked
1489f244e9cSSeiji Aguchi 	 * regardless of reason.
1499f244e9cSSeiji Aguchi 	 */
1509f244e9cSSeiji Aguchi 	if (in_nmi())
1519f244e9cSSeiji Aguchi 		return true;
1529f244e9cSSeiji Aguchi 
1539f244e9cSSeiji Aguchi 	switch (reason) {
1549f244e9cSSeiji Aguchi 	/* In panic case, other cpus are stopped by smp_send_stop(). */
1559f244e9cSSeiji Aguchi 	case KMSG_DUMP_PANIC:
1569f244e9cSSeiji Aguchi 	/* Emergency restart shouldn't be blocked by spin lock. */
1579f244e9cSSeiji Aguchi 	case KMSG_DUMP_EMERG:
1589f244e9cSSeiji Aguchi 		return true;
1599f244e9cSSeiji Aguchi 	default:
1609f244e9cSSeiji Aguchi 		return false;
1619f244e9cSSeiji Aguchi 	}
1629f244e9cSSeiji Aguchi }
1639f244e9cSSeiji Aguchi EXPORT_SYMBOL_GPL(pstore_cannot_block_path);
1649f244e9cSSeiji Aguchi 
1658cfc8ddcSGeliang Tang #ifdef CONFIG_PSTORE_ZLIB_COMPRESS
166b0aad7a9SAruna Balakrishnaiah /* Derived from logfs_compress() */
1678cfc8ddcSGeliang Tang static int compress_zlib(const void *in, void *out, size_t inlen, size_t outlen)
168b0aad7a9SAruna Balakrishnaiah {
169b0aad7a9SAruna Balakrishnaiah 	int err, ret;
170b0aad7a9SAruna Balakrishnaiah 
171b0aad7a9SAruna Balakrishnaiah 	ret = -EIO;
172b0aad7a9SAruna Balakrishnaiah 	err = zlib_deflateInit2(&stream, COMPR_LEVEL, Z_DEFLATED, WINDOW_BITS,
173b0aad7a9SAruna Balakrishnaiah 						MEM_LEVEL, Z_DEFAULT_STRATEGY);
174b0aad7a9SAruna Balakrishnaiah 	if (err != Z_OK)
175b0aad7a9SAruna Balakrishnaiah 		goto error;
176b0aad7a9SAruna Balakrishnaiah 
177b0aad7a9SAruna Balakrishnaiah 	stream.next_in = in;
178b0aad7a9SAruna Balakrishnaiah 	stream.avail_in = inlen;
179b0aad7a9SAruna Balakrishnaiah 	stream.total_in = 0;
180b0aad7a9SAruna Balakrishnaiah 	stream.next_out = out;
181b0aad7a9SAruna Balakrishnaiah 	stream.avail_out = outlen;
182b0aad7a9SAruna Balakrishnaiah 	stream.total_out = 0;
183b0aad7a9SAruna Balakrishnaiah 
184b0aad7a9SAruna Balakrishnaiah 	err = zlib_deflate(&stream, Z_FINISH);
185b0aad7a9SAruna Balakrishnaiah 	if (err != Z_STREAM_END)
186b0aad7a9SAruna Balakrishnaiah 		goto error;
187b0aad7a9SAruna Balakrishnaiah 
188b0aad7a9SAruna Balakrishnaiah 	err = zlib_deflateEnd(&stream);
189b0aad7a9SAruna Balakrishnaiah 	if (err != Z_OK)
190b0aad7a9SAruna Balakrishnaiah 		goto error;
191b0aad7a9SAruna Balakrishnaiah 
192b0aad7a9SAruna Balakrishnaiah 	if (stream.total_out >= stream.total_in)
193b0aad7a9SAruna Balakrishnaiah 		goto error;
194b0aad7a9SAruna Balakrishnaiah 
195b0aad7a9SAruna Balakrishnaiah 	ret = stream.total_out;
196b0aad7a9SAruna Balakrishnaiah error:
197b0aad7a9SAruna Balakrishnaiah 	return ret;
198b0aad7a9SAruna Balakrishnaiah }
199b0aad7a9SAruna Balakrishnaiah 
200adb42f5eSAruna Balakrishnaiah /* Derived from logfs_uncompress */
2018cfc8ddcSGeliang Tang static int decompress_zlib(void *in, void *out, size_t inlen, size_t outlen)
202adb42f5eSAruna Balakrishnaiah {
203adb42f5eSAruna Balakrishnaiah 	int err, ret;
204adb42f5eSAruna Balakrishnaiah 
205adb42f5eSAruna Balakrishnaiah 	ret = -EIO;
206b61edf8eSAruna Balakrishnaiah 	err = zlib_inflateInit2(&stream, WINDOW_BITS);
207adb42f5eSAruna Balakrishnaiah 	if (err != Z_OK)
208adb42f5eSAruna Balakrishnaiah 		goto error;
209adb42f5eSAruna Balakrishnaiah 
210adb42f5eSAruna Balakrishnaiah 	stream.next_in = in;
211adb42f5eSAruna Balakrishnaiah 	stream.avail_in = inlen;
212adb42f5eSAruna Balakrishnaiah 	stream.total_in = 0;
213adb42f5eSAruna Balakrishnaiah 	stream.next_out = out;
214adb42f5eSAruna Balakrishnaiah 	stream.avail_out = outlen;
215adb42f5eSAruna Balakrishnaiah 	stream.total_out = 0;
216adb42f5eSAruna Balakrishnaiah 
217adb42f5eSAruna Balakrishnaiah 	err = zlib_inflate(&stream, Z_FINISH);
218adb42f5eSAruna Balakrishnaiah 	if (err != Z_STREAM_END)
219adb42f5eSAruna Balakrishnaiah 		goto error;
220adb42f5eSAruna Balakrishnaiah 
221adb42f5eSAruna Balakrishnaiah 	err = zlib_inflateEnd(&stream);
222adb42f5eSAruna Balakrishnaiah 	if (err != Z_OK)
223adb42f5eSAruna Balakrishnaiah 		goto error;
224adb42f5eSAruna Balakrishnaiah 
225adb42f5eSAruna Balakrishnaiah 	ret = stream.total_out;
226adb42f5eSAruna Balakrishnaiah error:
227adb42f5eSAruna Balakrishnaiah 	return ret;
228adb42f5eSAruna Balakrishnaiah }
229adb42f5eSAruna Balakrishnaiah 
2308cfc8ddcSGeliang Tang static void allocate_zlib(void)
231b0aad7a9SAruna Balakrishnaiah {
232b0aad7a9SAruna Balakrishnaiah 	size_t size;
2337de8fe2fSAruna Balakrishnaiah 	size_t cmpr;
234b0aad7a9SAruna Balakrishnaiah 
2357de8fe2fSAruna Balakrishnaiah 	switch (psinfo->bufsize) {
2367de8fe2fSAruna Balakrishnaiah 	/* buffer range for efivars */
2377de8fe2fSAruna Balakrishnaiah 	case 1000 ... 2000:
2387de8fe2fSAruna Balakrishnaiah 		cmpr = 56;
2397de8fe2fSAruna Balakrishnaiah 		break;
2407de8fe2fSAruna Balakrishnaiah 	case 2001 ... 3000:
2417de8fe2fSAruna Balakrishnaiah 		cmpr = 54;
2427de8fe2fSAruna Balakrishnaiah 		break;
2437de8fe2fSAruna Balakrishnaiah 	case 3001 ... 3999:
2447de8fe2fSAruna Balakrishnaiah 		cmpr = 52;
2457de8fe2fSAruna Balakrishnaiah 		break;
2467de8fe2fSAruna Balakrishnaiah 	/* buffer range for nvram, erst */
2477de8fe2fSAruna Balakrishnaiah 	case 4000 ... 10000:
2487de8fe2fSAruna Balakrishnaiah 		cmpr = 45;
2497de8fe2fSAruna Balakrishnaiah 		break;
2507de8fe2fSAruna Balakrishnaiah 	default:
2517de8fe2fSAruna Balakrishnaiah 		cmpr = 60;
2527de8fe2fSAruna Balakrishnaiah 		break;
2537de8fe2fSAruna Balakrishnaiah 	}
2547de8fe2fSAruna Balakrishnaiah 
2557de8fe2fSAruna Balakrishnaiah 	big_oops_buf_sz = (psinfo->bufsize * 100) / cmpr;
256b0aad7a9SAruna Balakrishnaiah 	big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
257b0aad7a9SAruna Balakrishnaiah 	if (big_oops_buf) {
258b0aad7a9SAruna Balakrishnaiah 		size = max(zlib_deflate_workspacesize(WINDOW_BITS, MEM_LEVEL),
259b0aad7a9SAruna Balakrishnaiah 			zlib_inflate_workspacesize());
260b0aad7a9SAruna Balakrishnaiah 		stream.workspace = kmalloc(size, GFP_KERNEL);
261b0aad7a9SAruna Balakrishnaiah 		if (!stream.workspace) {
262ef748853SFabian Frederick 			pr_err("No memory for compression workspace; skipping compression\n");
263b0aad7a9SAruna Balakrishnaiah 			kfree(big_oops_buf);
264b0aad7a9SAruna Balakrishnaiah 			big_oops_buf = NULL;
265b0aad7a9SAruna Balakrishnaiah 		}
266b0aad7a9SAruna Balakrishnaiah 	} else {
267ef748853SFabian Frederick 		pr_err("No memory for uncompressed data; skipping compression\n");
268b0aad7a9SAruna Balakrishnaiah 		stream.workspace = NULL;
269b0aad7a9SAruna Balakrishnaiah 	}
270b0aad7a9SAruna Balakrishnaiah 
271b0aad7a9SAruna Balakrishnaiah }
272b0aad7a9SAruna Balakrishnaiah 
2738cfc8ddcSGeliang Tang static void free_zlib(void)
274ee1d2674SGeliang Tang {
275ee1d2674SGeliang Tang 	kfree(stream.workspace);
276ee1d2674SGeliang Tang 	stream.workspace = NULL;
277ee1d2674SGeliang Tang 	kfree(big_oops_buf);
278ee1d2674SGeliang Tang 	big_oops_buf = NULL;
2798cfc8ddcSGeliang Tang 	big_oops_buf_sz = 0;
2808cfc8ddcSGeliang Tang }
2818cfc8ddcSGeliang Tang #endif
2828cfc8ddcSGeliang Tang 
2838cfc8ddcSGeliang Tang #ifdef CONFIG_PSTORE_LZO_COMPRESS
2848cfc8ddcSGeliang Tang static int compress_lzo(const void *in, void *out, size_t inlen, size_t outlen)
2858cfc8ddcSGeliang Tang {
2868cfc8ddcSGeliang Tang 	int ret;
2878cfc8ddcSGeliang Tang 
2888cfc8ddcSGeliang Tang 	ret = lzo1x_1_compress(in, inlen, out, &outlen, workspace);
2898cfc8ddcSGeliang Tang 	if (ret != LZO_E_OK) {
2908cfc8ddcSGeliang Tang 		pr_err("lzo_compress error, ret = %d!\n", ret);
2918cfc8ddcSGeliang Tang 		return -EIO;
2928cfc8ddcSGeliang Tang 	}
2938cfc8ddcSGeliang Tang 
2948cfc8ddcSGeliang Tang 	return outlen;
2958cfc8ddcSGeliang Tang }
2968cfc8ddcSGeliang Tang 
2978cfc8ddcSGeliang Tang static int decompress_lzo(void *in, void *out, size_t inlen, size_t outlen)
2988cfc8ddcSGeliang Tang {
2998cfc8ddcSGeliang Tang 	int ret;
3008cfc8ddcSGeliang Tang 
3018cfc8ddcSGeliang Tang 	ret = lzo1x_decompress_safe(in, inlen, out, &outlen);
3028cfc8ddcSGeliang Tang 	if (ret != LZO_E_OK) {
3038cfc8ddcSGeliang Tang 		pr_err("lzo_decompress error, ret = %d!\n", ret);
3048cfc8ddcSGeliang Tang 		return -EIO;
3058cfc8ddcSGeliang Tang 	}
3068cfc8ddcSGeliang Tang 
3078cfc8ddcSGeliang Tang 	return outlen;
3088cfc8ddcSGeliang Tang }
3098cfc8ddcSGeliang Tang 
3108cfc8ddcSGeliang Tang static void allocate_lzo(void)
3118cfc8ddcSGeliang Tang {
3128cfc8ddcSGeliang Tang 	big_oops_buf_sz = lzo1x_worst_compress(psinfo->bufsize);
3138cfc8ddcSGeliang Tang 	big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
3148cfc8ddcSGeliang Tang 	if (big_oops_buf) {
3158cfc8ddcSGeliang Tang 		workspace = kmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
3168cfc8ddcSGeliang Tang 		if (!workspace) {
3178cfc8ddcSGeliang Tang 			pr_err("No memory for compression workspace; skipping compression\n");
3188cfc8ddcSGeliang Tang 			kfree(big_oops_buf);
3198cfc8ddcSGeliang Tang 			big_oops_buf = NULL;
3208cfc8ddcSGeliang Tang 		}
3218cfc8ddcSGeliang Tang 	} else {
3228cfc8ddcSGeliang Tang 		pr_err("No memory for uncompressed data; skipping compression\n");
3238cfc8ddcSGeliang Tang 		workspace = NULL;
3248cfc8ddcSGeliang Tang 	}
3258cfc8ddcSGeliang Tang }
3268cfc8ddcSGeliang Tang 
3278cfc8ddcSGeliang Tang static void free_lzo(void)
3288cfc8ddcSGeliang Tang {
3298cfc8ddcSGeliang Tang 	kfree(workspace);
3308cfc8ddcSGeliang Tang 	kfree(big_oops_buf);
3318cfc8ddcSGeliang Tang 	big_oops_buf = NULL;
3328cfc8ddcSGeliang Tang 	big_oops_buf_sz = 0;
3338cfc8ddcSGeliang Tang }
3348cfc8ddcSGeliang Tang #endif
3358cfc8ddcSGeliang Tang 
336239b7161SGeliang Tang #if defined(CONFIG_PSTORE_LZ4_COMPRESS) || defined(CONFIG_PSTORE_LZ4HC_COMPRESS)
3378cfc8ddcSGeliang Tang static int decompress_lz4(void *in, void *out, size_t inlen, size_t outlen)
3388cfc8ddcSGeliang Tang {
3398cfc8ddcSGeliang Tang 	int ret;
3408cfc8ddcSGeliang Tang 
341d21b5ff1SSven Schmidt 	ret = LZ4_decompress_safe(in, out, inlen, outlen);
342d21b5ff1SSven Schmidt 	if (ret < 0) {
343d21b5ff1SSven Schmidt 		/*
344d21b5ff1SSven Schmidt 		 * LZ4_decompress_safe will return an error code
345d21b5ff1SSven Schmidt 		 * (< 0) if decompression failed
346d21b5ff1SSven Schmidt 		 */
347d21b5ff1SSven Schmidt 		pr_err("LZ4_decompress_safe error, ret = %d!\n", ret);
3488cfc8ddcSGeliang Tang 		return -EIO;
3498cfc8ddcSGeliang Tang 	}
3508cfc8ddcSGeliang Tang 
351d21b5ff1SSven Schmidt 	return ret;
3528cfc8ddcSGeliang Tang }
3538cfc8ddcSGeliang Tang 
354239b7161SGeliang Tang static void free_lz4(void)
355239b7161SGeliang Tang {
356239b7161SGeliang Tang 	kfree(workspace);
357239b7161SGeliang Tang 	kfree(big_oops_buf);
358239b7161SGeliang Tang 	big_oops_buf = NULL;
359239b7161SGeliang Tang 	big_oops_buf_sz = 0;
360239b7161SGeliang Tang }
361239b7161SGeliang Tang #endif
362239b7161SGeliang Tang 
363239b7161SGeliang Tang #ifdef CONFIG_PSTORE_LZ4_COMPRESS
364239b7161SGeliang Tang static int compress_lz4(const void *in, void *out, size_t inlen, size_t outlen)
365239b7161SGeliang Tang {
366239b7161SGeliang Tang 	int ret;
367239b7161SGeliang Tang 
368239b7161SGeliang Tang 	ret = LZ4_compress_default(in, out, inlen, outlen, workspace);
369239b7161SGeliang Tang 	if (!ret) {
370239b7161SGeliang Tang 		pr_err("LZ4_compress_default error; compression failed!\n");
371239b7161SGeliang Tang 		return -EIO;
372239b7161SGeliang Tang 	}
373239b7161SGeliang Tang 
374239b7161SGeliang Tang 	return ret;
375239b7161SGeliang Tang }
376239b7161SGeliang Tang 
3778cfc8ddcSGeliang Tang static void allocate_lz4(void)
3788cfc8ddcSGeliang Tang {
379d21b5ff1SSven Schmidt 	big_oops_buf_sz = LZ4_compressBound(psinfo->bufsize);
3808cfc8ddcSGeliang Tang 	big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
3818cfc8ddcSGeliang Tang 	if (big_oops_buf) {
3828cfc8ddcSGeliang Tang 		workspace = kmalloc(LZ4_MEM_COMPRESS, GFP_KERNEL);
3838cfc8ddcSGeliang Tang 		if (!workspace) {
3848cfc8ddcSGeliang Tang 			pr_err("No memory for compression workspace; skipping compression\n");
3858cfc8ddcSGeliang Tang 			kfree(big_oops_buf);
3868cfc8ddcSGeliang Tang 			big_oops_buf = NULL;
3878cfc8ddcSGeliang Tang 		}
3888cfc8ddcSGeliang Tang 	} else {
3898cfc8ddcSGeliang Tang 		pr_err("No memory for uncompressed data; skipping compression\n");
3908cfc8ddcSGeliang Tang 		workspace = NULL;
3918cfc8ddcSGeliang Tang 	}
3928cfc8ddcSGeliang Tang }
393239b7161SGeliang Tang #endif
394239b7161SGeliang Tang 
395239b7161SGeliang Tang #ifdef CONFIG_PSTORE_LZ4HC_COMPRESS
396239b7161SGeliang Tang static int compress_lz4hc(const void *in, void *out,
397239b7161SGeliang Tang 			  size_t inlen, size_t outlen)
398239b7161SGeliang Tang {
399239b7161SGeliang Tang 	int ret;
400239b7161SGeliang Tang 
401239b7161SGeliang Tang 	ret = LZ4_compress_HC(in, out, inlen, outlen,
402239b7161SGeliang Tang 			      LZ4HC_DEFAULT_CLEVEL, workspace);
403239b7161SGeliang Tang 	if (!ret) {
404239b7161SGeliang Tang 		pr_err("LZ4_compress_HC error; compression failed!\n");
405239b7161SGeliang Tang 		return -EIO;
406239b7161SGeliang Tang 	}
407239b7161SGeliang Tang 
408239b7161SGeliang Tang 	return ret;
409239b7161SGeliang Tang }
410239b7161SGeliang Tang 
411239b7161SGeliang Tang static void allocate_lz4hc(void)
412239b7161SGeliang Tang {
413239b7161SGeliang Tang 	big_oops_buf_sz = LZ4_compressBound(psinfo->bufsize);
414239b7161SGeliang Tang 	big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
415239b7161SGeliang Tang 	if (big_oops_buf) {
416239b7161SGeliang Tang 		workspace = kmalloc(LZ4HC_MEM_COMPRESS, GFP_KERNEL);
417239b7161SGeliang Tang 		if (!workspace) {
418239b7161SGeliang Tang 			pr_err("No memory for compression workspace; skipping compression\n");
419239b7161SGeliang Tang 			kfree(big_oops_buf);
420239b7161SGeliang Tang 			big_oops_buf = NULL;
421239b7161SGeliang Tang 		}
422239b7161SGeliang Tang 	} else {
423239b7161SGeliang Tang 		pr_err("No memory for uncompressed data; skipping compression\n");
424239b7161SGeliang Tang 		workspace = NULL;
425239b7161SGeliang Tang 	}
426239b7161SGeliang Tang }
427239b7161SGeliang Tang #endif
428239b7161SGeliang Tang 
429239b7161SGeliang Tang #ifdef CONFIG_PSTORE_842_COMPRESS
430239b7161SGeliang Tang static int compress_842(const void *in, void *out, size_t inlen, size_t outlen)
431239b7161SGeliang Tang {
432239b7161SGeliang Tang 	int ret;
43355597406SKees Cook 	unsigned int size;
434239b7161SGeliang Tang 
43555597406SKees Cook 	if (outlen > UINT_MAX)
43655597406SKees Cook 		return -EIO;
43755597406SKees Cook 	size = outlen;
43855597406SKees Cook 
43955597406SKees Cook 	ret = sw842_compress(in, inlen, out, &size, workspace);
440239b7161SGeliang Tang 	if (ret) {
441239b7161SGeliang Tang 		pr_err("sw842_compress error; compression failed!\n");
442239b7161SGeliang Tang 		return ret;
443239b7161SGeliang Tang 	}
444239b7161SGeliang Tang 
44555597406SKees Cook 	return size;
446239b7161SGeliang Tang }
447239b7161SGeliang Tang 
448239b7161SGeliang Tang static int decompress_842(void *in, void *out, size_t inlen, size_t outlen)
449239b7161SGeliang Tang {
450239b7161SGeliang Tang 	int ret;
45155597406SKees Cook 	unsigned int size;
452239b7161SGeliang Tang 
45355597406SKees Cook 	if (outlen > UINT_MAX)
45455597406SKees Cook 		return -EIO;
45555597406SKees Cook 	size = outlen;
45655597406SKees Cook 
45755597406SKees Cook 	ret = sw842_decompress(in, inlen, out, &size);
458239b7161SGeliang Tang 	if (ret) {
459239b7161SGeliang Tang 		pr_err("sw842_decompress error, ret = %d!\n", ret);
460239b7161SGeliang Tang 		return ret;
461239b7161SGeliang Tang 	}
462239b7161SGeliang Tang 
46355597406SKees Cook 	return size;
464239b7161SGeliang Tang }
465239b7161SGeliang Tang 
466239b7161SGeliang Tang static void allocate_842(void)
467239b7161SGeliang Tang {
468239b7161SGeliang Tang 	big_oops_buf_sz = psinfo->bufsize;
469239b7161SGeliang Tang 	big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
470239b7161SGeliang Tang 	if (big_oops_buf) {
471239b7161SGeliang Tang 		workspace = kmalloc(SW842_MEM_COMPRESS, GFP_KERNEL);
472239b7161SGeliang Tang 		if (!workspace) {
473239b7161SGeliang Tang 			kfree(big_oops_buf);
474239b7161SGeliang Tang 			big_oops_buf = NULL;
475239b7161SGeliang Tang 		}
476239b7161SGeliang Tang 	} else {
477239b7161SGeliang Tang 		pr_err("No memory for uncompressed data; skipping compression\n");
478239b7161SGeliang Tang 		workspace = NULL;
479239b7161SGeliang Tang 	}
480239b7161SGeliang Tang }
481239b7161SGeliang Tang 
482239b7161SGeliang Tang static void free_842(void)
4838cfc8ddcSGeliang Tang {
4848cfc8ddcSGeliang Tang 	kfree(workspace);
4858cfc8ddcSGeliang Tang 	kfree(big_oops_buf);
4868cfc8ddcSGeliang Tang 	big_oops_buf = NULL;
4878cfc8ddcSGeliang Tang 	big_oops_buf_sz = 0;
4888cfc8ddcSGeliang Tang }
489*fe1d4758SKees Cook #endif
4908cfc8ddcSGeliang Tang 
491*fe1d4758SKees Cook static const struct pstore_zbackend *zbackend __ro_after_init;
492*fe1d4758SKees Cook 
493*fe1d4758SKees Cook static const struct pstore_zbackend zbackends[] = {
494*fe1d4758SKees Cook #ifdef CONFIG_PSTORE_ZLIB_COMPRESS
495*fe1d4758SKees Cook 	{
496*fe1d4758SKees Cook 		.compress	= compress_zlib,
497*fe1d4758SKees Cook 		.decompress	= decompress_zlib,
498*fe1d4758SKees Cook 		.allocate	= allocate_zlib,
499*fe1d4758SKees Cook 		.free		= free_zlib,
500*fe1d4758SKees Cook 		.name		= "zlib",
501*fe1d4758SKees Cook 	},
502*fe1d4758SKees Cook #endif
503*fe1d4758SKees Cook #ifdef CONFIG_PSTORE_LZO_COMPRESS
504*fe1d4758SKees Cook 	{
505*fe1d4758SKees Cook 		.compress	= compress_lzo,
506*fe1d4758SKees Cook 		.decompress	= decompress_lzo,
507*fe1d4758SKees Cook 		.allocate	= allocate_lzo,
508*fe1d4758SKees Cook 		.free		= free_lzo,
509*fe1d4758SKees Cook 		.name		= "lzo",
510*fe1d4758SKees Cook 	},
511*fe1d4758SKees Cook #endif
512*fe1d4758SKees Cook #ifdef CONFIG_PSTORE_LZ4_COMPRESS
513*fe1d4758SKees Cook 	{
514*fe1d4758SKees Cook 		.compress	= compress_lz4,
515*fe1d4758SKees Cook 		.decompress	= decompress_lz4,
516*fe1d4758SKees Cook 		.allocate	= allocate_lz4,
517*fe1d4758SKees Cook 		.free		= free_lz4,
518*fe1d4758SKees Cook 		.name		= "lz4",
519*fe1d4758SKees Cook 	},
520*fe1d4758SKees Cook #endif
521*fe1d4758SKees Cook #ifdef CONFIG_PSTORE_LZ4HC_COMPRESS
522*fe1d4758SKees Cook 	{
523*fe1d4758SKees Cook 		.compress	= compress_lz4hc,
524*fe1d4758SKees Cook 		.decompress	= decompress_lz4,
525*fe1d4758SKees Cook 		.allocate	= allocate_lz4hc,
526*fe1d4758SKees Cook 		.free		= free_lz4,
527*fe1d4758SKees Cook 		.name		= "lz4hc",
528*fe1d4758SKees Cook 	},
529*fe1d4758SKees Cook #endif
530*fe1d4758SKees Cook #ifdef CONFIG_PSTORE_842_COMPRESS
531*fe1d4758SKees Cook 	{
532239b7161SGeliang Tang 		.compress	= compress_842,
533239b7161SGeliang Tang 		.decompress	= decompress_842,
534239b7161SGeliang Tang 		.allocate	= allocate_842,
535239b7161SGeliang Tang 		.free		= free_842,
536239b7161SGeliang Tang 		.name		= "842",
537*fe1d4758SKees Cook 	},
538*fe1d4758SKees Cook #endif
539*fe1d4758SKees Cook 	{ }
5408cfc8ddcSGeliang Tang };
5418cfc8ddcSGeliang Tang 
5428cfc8ddcSGeliang Tang static int pstore_compress(const void *in, void *out,
5438cfc8ddcSGeliang Tang 			   size_t inlen, size_t outlen)
5448cfc8ddcSGeliang Tang {
5458cfc8ddcSGeliang Tang 	if (zbackend)
5468cfc8ddcSGeliang Tang 		return zbackend->compress(in, out, inlen, outlen);
5478cfc8ddcSGeliang Tang 	else
5488cfc8ddcSGeliang Tang 		return -EIO;
5498cfc8ddcSGeliang Tang }
5508cfc8ddcSGeliang Tang 
5518cfc8ddcSGeliang Tang static int pstore_decompress(void *in, void *out, size_t inlen, size_t outlen)
5528cfc8ddcSGeliang Tang {
5538cfc8ddcSGeliang Tang 	if (zbackend)
5548cfc8ddcSGeliang Tang 		return zbackend->decompress(in, out, inlen, outlen);
5558cfc8ddcSGeliang Tang 	else
5568cfc8ddcSGeliang Tang 		return -EIO;
5578cfc8ddcSGeliang Tang }
5588cfc8ddcSGeliang Tang 
5598cfc8ddcSGeliang Tang static void allocate_buf_for_compression(void)
5608cfc8ddcSGeliang Tang {
5618cfc8ddcSGeliang Tang 	if (zbackend) {
5628cfc8ddcSGeliang Tang 		zbackend->allocate();
5638cfc8ddcSGeliang Tang 	} else {
5648cfc8ddcSGeliang Tang 		pr_err("allocate compression buffer error!\n");
5658cfc8ddcSGeliang Tang 	}
5668cfc8ddcSGeliang Tang }
5678cfc8ddcSGeliang Tang 
5688cfc8ddcSGeliang Tang static void free_buf_for_compression(void)
5698cfc8ddcSGeliang Tang {
5708cfc8ddcSGeliang Tang 	if (zbackend)
5718cfc8ddcSGeliang Tang 		zbackend->free();
5728cfc8ddcSGeliang Tang 	else
5738cfc8ddcSGeliang Tang 		pr_err("free compression buffer error!\n");
574ee1d2674SGeliang Tang }
575ee1d2674SGeliang Tang 
576b0aad7a9SAruna Balakrishnaiah /*
577b0aad7a9SAruna Balakrishnaiah  * Called when compression fails, since the printk buffer
578b0aad7a9SAruna Balakrishnaiah  * would be fetched for compression calling it again when
579b0aad7a9SAruna Balakrishnaiah  * compression fails would have moved the iterator of
580b0aad7a9SAruna Balakrishnaiah  * printk buffer which results in fetching old contents.
581b0aad7a9SAruna Balakrishnaiah  * Copy the recent messages from big_oops_buf to psinfo->buf
582b0aad7a9SAruna Balakrishnaiah  */
583b0aad7a9SAruna Balakrishnaiah static size_t copy_kmsg_to_buffer(int hsize, size_t len)
584b0aad7a9SAruna Balakrishnaiah {
585b0aad7a9SAruna Balakrishnaiah 	size_t total_len;
586b0aad7a9SAruna Balakrishnaiah 	size_t diff;
587b0aad7a9SAruna Balakrishnaiah 
588b0aad7a9SAruna Balakrishnaiah 	total_len = hsize + len;
589b0aad7a9SAruna Balakrishnaiah 
590b0aad7a9SAruna Balakrishnaiah 	if (total_len > psinfo->bufsize) {
591b0aad7a9SAruna Balakrishnaiah 		diff = total_len - psinfo->bufsize + hsize;
592b0aad7a9SAruna Balakrishnaiah 		memcpy(psinfo->buf, big_oops_buf, hsize);
593b0aad7a9SAruna Balakrishnaiah 		memcpy(psinfo->buf + hsize, big_oops_buf + diff,
594b0aad7a9SAruna Balakrishnaiah 					psinfo->bufsize - hsize);
595b0aad7a9SAruna Balakrishnaiah 		total_len = psinfo->bufsize;
596b0aad7a9SAruna Balakrishnaiah 	} else
597b0aad7a9SAruna Balakrishnaiah 		memcpy(psinfo->buf, big_oops_buf, total_len);
598b0aad7a9SAruna Balakrishnaiah 
599b0aad7a9SAruna Balakrishnaiah 	return total_len;
600b0aad7a9SAruna Balakrishnaiah }
601b0aad7a9SAruna Balakrishnaiah 
602e581ca81SKees Cook void pstore_record_init(struct pstore_record *record,
603e581ca81SKees Cook 			struct pstore_info *psinfo)
604e581ca81SKees Cook {
605e581ca81SKees Cook 	memset(record, 0, sizeof(*record));
606e581ca81SKees Cook 
607e581ca81SKees Cook 	record->psi = psinfo;
608c7f3c595SKees Cook 
609c7f3c595SKees Cook 	/* Report zeroed timestamp if called before timekeeping has resumed. */
610df27067eSArnd Bergmann 	record->time = ns_to_timespec(ktime_get_real_fast_ns());
611e581ca81SKees Cook }
612e581ca81SKees Cook 
613ca01d6ddSTony Luck /*
614ca01d6ddSTony Luck  * callback from kmsg_dump. (s2,l2) has the most recently
615ca01d6ddSTony Luck  * written bytes, older bytes are in (s1,l1). Save as much
616ca01d6ddSTony Luck  * as we can from the end of the buffer.
617ca01d6ddSTony Luck  */
618ca01d6ddSTony Luck static void pstore_dump(struct kmsg_dumper *dumper,
619e2ae715dSKay Sievers 			enum kmsg_dump_reason reason)
620ca01d6ddSTony Luck {
621e2ae715dSKay Sievers 	unsigned long	total = 0;
622381b872cSSeiji Aguchi 	const char	*why;
623b94fdd07SMatthew Garrett 	unsigned int	part = 1;
624abd4d558SDon Zickus 	unsigned long	flags = 0;
62598e44fdaSNamhyung Kim 	int		is_locked;
626e2ae715dSKay Sievers 	int		ret;
627ca01d6ddSTony Luck 
628381b872cSSeiji Aguchi 	why = get_reason_str(reason);
6299f6af27fSTony Luck 
6309f244e9cSSeiji Aguchi 	if (pstore_cannot_block_path(reason)) {
6319f244e9cSSeiji Aguchi 		is_locked = spin_trylock_irqsave(&psinfo->buf_lock, flags);
6329f244e9cSSeiji Aguchi 		if (!is_locked) {
6339f244e9cSSeiji Aguchi 			pr_err("pstore dump routine blocked in %s path, may corrupt error record\n"
6349f244e9cSSeiji Aguchi 				       , in_nmi() ? "NMI" : why);
635959217c8SLi Pengcheng 			return;
6369f244e9cSSeiji Aguchi 		}
63798e44fdaSNamhyung Kim 	} else {
638abd4d558SDon Zickus 		spin_lock_irqsave(&psinfo->buf_lock, flags);
63998e44fdaSNamhyung Kim 		is_locked = 1;
64098e44fdaSNamhyung Kim 	}
641ca01d6ddSTony Luck 	oopscount++;
642ca01d6ddSTony Luck 	while (total < kmsg_bytes) {
643e2ae715dSKay Sievers 		char *dst;
64476cc9580SKees Cook 		size_t dst_size;
64576cc9580SKees Cook 		int header_size;
646b0aad7a9SAruna Balakrishnaiah 		int zipped_len = -1;
64776cc9580SKees Cook 		size_t dump_size;
648e581ca81SKees Cook 		struct pstore_record record;
649e581ca81SKees Cook 
650e581ca81SKees Cook 		pstore_record_init(&record, psinfo);
651e581ca81SKees Cook 		record.type = PSTORE_TYPE_DMESG;
652e581ca81SKees Cook 		record.count = oopscount;
653e581ca81SKees Cook 		record.reason = reason;
654e581ca81SKees Cook 		record.part = part;
655e581ca81SKees Cook 		record.buf = psinfo->buf;
656e2ae715dSKay Sievers 
657f0e2efcfSKonstantin Khlebnikov 		if (big_oops_buf && is_locked) {
658b0aad7a9SAruna Balakrishnaiah 			dst = big_oops_buf;
65976cc9580SKees Cook 			dst_size = big_oops_buf_sz;
660235f6d15SNamhyung Kim 		} else {
661235f6d15SNamhyung Kim 			dst = psinfo->buf;
66276cc9580SKees Cook 			dst_size = psinfo->bufsize;
663235f6d15SNamhyung Kim 		}
664235f6d15SNamhyung Kim 
66576cc9580SKees Cook 		/* Write dump header. */
66676cc9580SKees Cook 		header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why,
66776cc9580SKees Cook 				 oopscount, part);
66876cc9580SKees Cook 		dst_size -= header_size;
669b0aad7a9SAruna Balakrishnaiah 
67076cc9580SKees Cook 		/* Write dump contents. */
67176cc9580SKees Cook 		if (!kmsg_dump_get_buffer(dumper, true, dst + header_size,
67276cc9580SKees Cook 					  dst_size, &dump_size))
673b0aad7a9SAruna Balakrishnaiah 			break;
674b0aad7a9SAruna Balakrishnaiah 
675235f6d15SNamhyung Kim 		if (big_oops_buf && is_locked) {
676b0aad7a9SAruna Balakrishnaiah 			zipped_len = pstore_compress(dst, psinfo->buf,
67776cc9580SKees Cook 						header_size + dump_size,
67876cc9580SKees Cook 						psinfo->bufsize);
679b0aad7a9SAruna Balakrishnaiah 
680b0aad7a9SAruna Balakrishnaiah 			if (zipped_len > 0) {
68176cc9580SKees Cook 				record.compressed = true;
68276cc9580SKees Cook 				record.size = zipped_len;
683b0aad7a9SAruna Balakrishnaiah 			} else {
68476cc9580SKees Cook 				record.size = copy_kmsg_to_buffer(header_size,
68576cc9580SKees Cook 								  dump_size);
686b0aad7a9SAruna Balakrishnaiah 			}
687b0aad7a9SAruna Balakrishnaiah 		} else {
68876cc9580SKees Cook 			record.size = header_size + dump_size;
689b0aad7a9SAruna Balakrishnaiah 		}
690b0aad7a9SAruna Balakrishnaiah 
69176cc9580SKees Cook 		ret = psinfo->write(&record);
692b238b8faSChen Gong 		if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted())
6936dda9266SLuck, Tony 			pstore_new_entry = 1;
694e2ae715dSKay Sievers 
69576cc9580SKees Cook 		total += record.size;
69656280682SMatthew Garrett 		part++;
697ca01d6ddSTony Luck 	}
698abd4d558SDon Zickus 	if (is_locked)
6999f244e9cSSeiji Aguchi 		spin_unlock_irqrestore(&psinfo->buf_lock, flags);
700ca01d6ddSTony Luck }
701ca01d6ddSTony Luck 
702ca01d6ddSTony Luck static struct kmsg_dumper pstore_dumper = {
703ca01d6ddSTony Luck 	.dump = pstore_dump,
704ca01d6ddSTony Luck };
705ca01d6ddSTony Luck 
706306e5c2aSGeliang Tang /*
707306e5c2aSGeliang Tang  * Register with kmsg_dump to save last part of console log on panic.
708306e5c2aSGeliang Tang  */
70918730411SGeliang Tang static void pstore_register_kmsg(void)
71018730411SGeliang Tang {
71118730411SGeliang Tang 	kmsg_dump_register(&pstore_dumper);
71218730411SGeliang Tang }
71318730411SGeliang Tang 
714ee1d2674SGeliang Tang static void pstore_unregister_kmsg(void)
715ee1d2674SGeliang Tang {
716ee1d2674SGeliang Tang 	kmsg_dump_unregister(&pstore_dumper);
717ee1d2674SGeliang Tang }
718ee1d2674SGeliang Tang 
719f29e5956SAnton Vorontsov #ifdef CONFIG_PSTORE_CONSOLE
720f29e5956SAnton Vorontsov static void pstore_console_write(struct console *con, const char *s, unsigned c)
721f29e5956SAnton Vorontsov {
722f29e5956SAnton Vorontsov 	const char *e = s + c;
723f29e5956SAnton Vorontsov 
724f29e5956SAnton Vorontsov 	while (s < e) {
725e581ca81SKees Cook 		struct pstore_record record;
726f29e5956SAnton Vorontsov 		unsigned long flags;
727f29e5956SAnton Vorontsov 
728e581ca81SKees Cook 		pstore_record_init(&record, psinfo);
729e581ca81SKees Cook 		record.type = PSTORE_TYPE_CONSOLE;
730e581ca81SKees Cook 
731f29e5956SAnton Vorontsov 		if (c > psinfo->bufsize)
732f29e5956SAnton Vorontsov 			c = psinfo->bufsize;
73380c9d03cSChuansheng Liu 
73480c9d03cSChuansheng Liu 		if (oops_in_progress) {
73580c9d03cSChuansheng Liu 			if (!spin_trylock_irqsave(&psinfo->buf_lock, flags))
73680c9d03cSChuansheng Liu 				break;
73780c9d03cSChuansheng Liu 		} else {
738f29e5956SAnton Vorontsov 			spin_lock_irqsave(&psinfo->buf_lock, flags);
73980c9d03cSChuansheng Liu 		}
740b10b4711SKees Cook 		record.buf = (char *)s;
741b10b4711SKees Cook 		record.size = c;
7424c9ec219SKees Cook 		psinfo->write(&record);
743f29e5956SAnton Vorontsov 		spin_unlock_irqrestore(&psinfo->buf_lock, flags);
744f29e5956SAnton Vorontsov 		s += c;
745f29e5956SAnton Vorontsov 		c = e - s;
746f29e5956SAnton Vorontsov 	}
747f29e5956SAnton Vorontsov }
748f29e5956SAnton Vorontsov 
749f29e5956SAnton Vorontsov static struct console pstore_console = {
750f29e5956SAnton Vorontsov 	.name	= "pstore",
751f29e5956SAnton Vorontsov 	.write	= pstore_console_write,
752f29e5956SAnton Vorontsov 	.flags	= CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME,
753f29e5956SAnton Vorontsov 	.index	= -1,
754f29e5956SAnton Vorontsov };
755f29e5956SAnton Vorontsov 
756f29e5956SAnton Vorontsov static void pstore_register_console(void)
757f29e5956SAnton Vorontsov {
758f29e5956SAnton Vorontsov 	register_console(&pstore_console);
759f29e5956SAnton Vorontsov }
760ee1d2674SGeliang Tang 
761ee1d2674SGeliang Tang static void pstore_unregister_console(void)
762ee1d2674SGeliang Tang {
763ee1d2674SGeliang Tang 	unregister_console(&pstore_console);
764ee1d2674SGeliang Tang }
765f29e5956SAnton Vorontsov #else
766f29e5956SAnton Vorontsov static void pstore_register_console(void) {}
767ee1d2674SGeliang Tang static void pstore_unregister_console(void) {}
768f29e5956SAnton Vorontsov #endif
769f29e5956SAnton Vorontsov 
7704c9ec219SKees Cook static int pstore_write_user_compat(struct pstore_record *record,
771fdd03118SKees Cook 				    const char __user *buf)
7725bf6d1b9SMark Salyzyn {
77330800d99SKees Cook 	int ret = 0;
7745bf6d1b9SMark Salyzyn 
77530800d99SKees Cook 	if (record->buf)
77630800d99SKees Cook 		return -EINVAL;
7775bf6d1b9SMark Salyzyn 
778077090afSGeliang Tang 	record->buf = memdup_user(buf, record->size);
779dfd6fa39SHirofumi Nakagawa 	if (IS_ERR(record->buf)) {
780077090afSGeliang Tang 		ret = PTR_ERR(record->buf);
78130800d99SKees Cook 		goto out;
7825bf6d1b9SMark Salyzyn 	}
78330800d99SKees Cook 
7844c9ec219SKees Cook 	ret = record->psi->write(record);
78530800d99SKees Cook 
78630800d99SKees Cook 	kfree(record->buf);
787077090afSGeliang Tang out:
78830800d99SKees Cook 	record->buf = NULL;
78930800d99SKees Cook 
79030800d99SKees Cook 	return unlikely(ret < 0) ? ret : record->size;
7915bf6d1b9SMark Salyzyn }
7925bf6d1b9SMark Salyzyn 
793ca01d6ddSTony Luck /*
794ca01d6ddSTony Luck  * platform specific persistent storage driver registers with
795ca01d6ddSTony Luck  * us here. If pstore is already mounted, call the platform
796ca01d6ddSTony Luck  * read function right away to populate the file system. If not
797ca01d6ddSTony Luck  * then the pstore mount code will call us later to fill out
798ca01d6ddSTony Luck  * the file system.
799ca01d6ddSTony Luck  */
800ca01d6ddSTony Luck int pstore_register(struct pstore_info *psi)
801ca01d6ddSTony Luck {
802ca01d6ddSTony Luck 	struct module *owner = psi->owner;
803ca01d6ddSTony Luck 
8040d7cd09aSKees Cook 	if (backend && strcmp(backend, psi->name)) {
8050d7cd09aSKees Cook 		pr_warn("ignoring unexpected backend '%s'\n", psi->name);
8068e48b1a8SLenny Szubowicz 		return -EPERM;
8070d7cd09aSKees Cook 	}
8088e48b1a8SLenny Szubowicz 
8094c9ec219SKees Cook 	/* Sanity check flags. */
8104c9ec219SKees Cook 	if (!psi->flags) {
8114c9ec219SKees Cook 		pr_warn("backend '%s' must support at least one frontend\n",
8124c9ec219SKees Cook 			psi->name);
8134c9ec219SKees Cook 		return -EINVAL;
8144c9ec219SKees Cook 	}
8154c9ec219SKees Cook 
8164c9ec219SKees Cook 	/* Check for required functions. */
8174c9ec219SKees Cook 	if (!psi->read || !psi->write) {
8184c9ec219SKees Cook 		pr_warn("backend '%s' must implement read() and write()\n",
8194c9ec219SKees Cook 			psi->name);
8204c9ec219SKees Cook 		return -EINVAL;
8214c9ec219SKees Cook 	}
8224c9ec219SKees Cook 
823ca01d6ddSTony Luck 	spin_lock(&pstore_lock);
824ca01d6ddSTony Luck 	if (psinfo) {
8250d7cd09aSKees Cook 		pr_warn("backend '%s' already loaded: ignoring '%s'\n",
8260d7cd09aSKees Cook 			psinfo->name, psi->name);
827ca01d6ddSTony Luck 		spin_unlock(&pstore_lock);
828ca01d6ddSTony Luck 		return -EBUSY;
829ca01d6ddSTony Luck 	}
830dee28e72SMatthew Garrett 
8314c9ec219SKees Cook 	if (!psi->write_user)
8324c9ec219SKees Cook 		psi->write_user = pstore_write_user_compat;
833ca01d6ddSTony Luck 	psinfo = psi;
834f6f82851SKees Cook 	mutex_init(&psinfo->read_mutex);
835ca01d6ddSTony Luck 	spin_unlock(&pstore_lock);
836ca01d6ddSTony Luck 
837ca01d6ddSTony Luck 	if (owner && !try_module_get(owner)) {
838ca01d6ddSTony Luck 		psinfo = NULL;
839ca01d6ddSTony Luck 		return -EINVAL;
840ca01d6ddSTony Luck 	}
841ca01d6ddSTony Luck 
842b0aad7a9SAruna Balakrishnaiah 	allocate_buf_for_compression();
843b0aad7a9SAruna Balakrishnaiah 
844ca01d6ddSTony Luck 	if (pstore_is_mounted())
8456dda9266SLuck, Tony 		pstore_get_records(0);
846ca01d6ddSTony Luck 
847c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_DMESG)
84818730411SGeliang Tang 		pstore_register_kmsg();
849c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_CONSOLE)
850f29e5956SAnton Vorontsov 		pstore_register_console();
851c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_FTRACE)
85265f8c95eSAnton Vorontsov 		pstore_register_ftrace();
853c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_PMSG)
8549d5438f4SMark Salyzyn 		pstore_register_pmsg();
855ca01d6ddSTony Luck 
8566330d553SKees Cook 	/* Start watching for new records, if desired. */
857a3f5f075SAnton Vorontsov 	if (pstore_update_ms >= 0) {
858a3f5f075SAnton Vorontsov 		pstore_timer.expires = jiffies +
859a3f5f075SAnton Vorontsov 			msecs_to_jiffies(pstore_update_ms);
8606dda9266SLuck, Tony 		add_timer(&pstore_timer);
861a3f5f075SAnton Vorontsov 	}
8626dda9266SLuck, Tony 
86342222c2aSWang Long 	/*
86442222c2aSWang Long 	 * Update the module parameter backend, so it is visible
86542222c2aSWang Long 	 * through /sys/module/pstore/parameters/backend
86642222c2aSWang Long 	 */
86742222c2aSWang Long 	backend = psi->name;
86842222c2aSWang Long 
869ef748853SFabian Frederick 	pr_info("Registered %s as persistent store backend\n", psi->name);
8708e48b1a8SLenny Szubowicz 
8711344dd86SKees Cook 	module_put(owner);
8721344dd86SKees Cook 
873ca01d6ddSTony Luck 	return 0;
874ca01d6ddSTony Luck }
875ca01d6ddSTony Luck EXPORT_SYMBOL_GPL(pstore_register);
876ca01d6ddSTony Luck 
877ee1d2674SGeliang Tang void pstore_unregister(struct pstore_info *psi)
878ee1d2674SGeliang Tang {
8796330d553SKees Cook 	/* Stop timer and make sure all work has finished. */
8806330d553SKees Cook 	pstore_update_ms = -1;
8816330d553SKees Cook 	del_timer_sync(&pstore_timer);
8826330d553SKees Cook 	flush_work(&pstore_work);
8836330d553SKees Cook 
884c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_PMSG)
885ee1d2674SGeliang Tang 		pstore_unregister_pmsg();
886c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_FTRACE)
887ee1d2674SGeliang Tang 		pstore_unregister_ftrace();
888c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_CONSOLE)
889ee1d2674SGeliang Tang 		pstore_unregister_console();
890c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_DMESG)
891ee1d2674SGeliang Tang 		pstore_unregister_kmsg();
892ee1d2674SGeliang Tang 
893ee1d2674SGeliang Tang 	free_buf_for_compression();
894ee1d2674SGeliang Tang 
895ee1d2674SGeliang Tang 	psinfo = NULL;
896ee1d2674SGeliang Tang 	backend = NULL;
897ee1d2674SGeliang Tang }
898ee1d2674SGeliang Tang EXPORT_SYMBOL_GPL(pstore_unregister);
899ee1d2674SGeliang Tang 
900634f8f51SKees Cook static void decompress_record(struct pstore_record *record)
901634f8f51SKees Cook {
902634f8f51SKees Cook 	int unzipped_len;
9037e8cc8dcSKees Cook 	char *decompressed;
904634f8f51SKees Cook 
9054a16d1cbSAnkit Kumar 	if (!record->compressed)
9064a16d1cbSAnkit Kumar 		return;
9074a16d1cbSAnkit Kumar 
908634f8f51SKees Cook 	/* Only PSTORE_TYPE_DMESG support compression. */
9094a16d1cbSAnkit Kumar 	if (record->type != PSTORE_TYPE_DMESG) {
910634f8f51SKees Cook 		pr_warn("ignored compressed record type %d\n", record->type);
911634f8f51SKees Cook 		return;
912634f8f51SKees Cook 	}
913634f8f51SKees Cook 
914634f8f51SKees Cook 	/* No compression method has created the common buffer. */
915634f8f51SKees Cook 	if (!big_oops_buf) {
916634f8f51SKees Cook 		pr_warn("no decompression buffer allocated\n");
917634f8f51SKees Cook 		return;
918634f8f51SKees Cook 	}
919634f8f51SKees Cook 
920634f8f51SKees Cook 	unzipped_len = pstore_decompress(record->buf, big_oops_buf,
921634f8f51SKees Cook 					 record->size, big_oops_buf_sz);
9227e8cc8dcSKees Cook 	if (unzipped_len <= 0) {
9237e8cc8dcSKees Cook 		pr_err("decompression failed: %d\n", unzipped_len);
9247e8cc8dcSKees Cook 		return;
9257e8cc8dcSKees Cook 	}
9267e8cc8dcSKees Cook 
9277e8cc8dcSKees Cook 	/* Build new buffer for decompressed contents. */
9287e8cc8dcSKees Cook 	decompressed = kmalloc(unzipped_len + record->ecc_notice_size,
9297e8cc8dcSKees Cook 			       GFP_KERNEL);
9307e8cc8dcSKees Cook 	if (!decompressed) {
9317e8cc8dcSKees Cook 		pr_err("decompression ran out of memory\n");
9327e8cc8dcSKees Cook 		return;
9337e8cc8dcSKees Cook 	}
9347e8cc8dcSKees Cook 	memcpy(decompressed, big_oops_buf, unzipped_len);
9357e8cc8dcSKees Cook 
9367e8cc8dcSKees Cook 	/* Append ECC notice to decompressed buffer. */
9377e8cc8dcSKees Cook 	memcpy(decompressed + unzipped_len, record->buf + record->size,
938634f8f51SKees Cook 	       record->ecc_notice_size);
9397e8cc8dcSKees Cook 
9407e8cc8dcSKees Cook 	/* Swap out compresed contents with decompressed contents. */
941634f8f51SKees Cook 	kfree(record->buf);
9427e8cc8dcSKees Cook 	record->buf = decompressed;
943634f8f51SKees Cook 	record->size = unzipped_len;
944634f8f51SKees Cook 	record->compressed = false;
945634f8f51SKees Cook }
946634f8f51SKees Cook 
947ca01d6ddSTony Luck /*
9483a7d2fd1SKees Cook  * Read all the records from one persistent store backend. Create
9496dda9266SLuck, Tony  * files in our filesystem.  Don't warn about -EEXIST errors
9506dda9266SLuck, Tony  * when we are re-scanning the backing store looking to add new
9516dda9266SLuck, Tony  * error records.
952ca01d6ddSTony Luck  */
9533a7d2fd1SKees Cook void pstore_get_backend_records(struct pstore_info *psi,
9543a7d2fd1SKees Cook 				struct dentry *root, int quiet)
955ca01d6ddSTony Luck {
9562a2b0acfSKees Cook 	int failed = 0;
957656de42eSKees Cook 	unsigned int stop_loop = 65536;
958ca01d6ddSTony Luck 
9593a7d2fd1SKees Cook 	if (!psi || !root)
960ca01d6ddSTony Luck 		return;
961ca01d6ddSTony Luck 
962f6f82851SKees Cook 	mutex_lock(&psi->read_mutex);
9632174f6dfSKees Cook 	if (psi->open && psi->open(psi))
96406cf91b4SChen Gong 		goto out;
96506cf91b4SChen Gong 
9661dfff7ddSKees Cook 	/*
9671dfff7ddSKees Cook 	 * Backend callback read() allocates record.buf. decompress_record()
9681dfff7ddSKees Cook 	 * may reallocate record.buf. On success, pstore_mkfile() will keep
9691dfff7ddSKees Cook 	 * the record.buf, so free it only on failure.
9701dfff7ddSKees Cook 	 */
971656de42eSKees Cook 	for (; stop_loop; stop_loop--) {
9722a2b0acfSKees Cook 		struct pstore_record *record;
9732a2b0acfSKees Cook 		int rc;
9742a2b0acfSKees Cook 
9752a2b0acfSKees Cook 		record = kzalloc(sizeof(*record), GFP_KERNEL);
9762a2b0acfSKees Cook 		if (!record) {
9772a2b0acfSKees Cook 			pr_err("out of memory creating record\n");
9782a2b0acfSKees Cook 			break;
9792a2b0acfSKees Cook 		}
980e581ca81SKees Cook 		pstore_record_init(record, psi);
9812a2b0acfSKees Cook 
9822a2b0acfSKees Cook 		record->size = psi->read(record);
9832a2b0acfSKees Cook 
9842a2b0acfSKees Cook 		/* No more records left in backend? */
985f6525b96SDouglas Anderson 		if (record->size <= 0) {
986f6525b96SDouglas Anderson 			kfree(record);
9872a2b0acfSKees Cook 			break;
988f6525b96SDouglas Anderson 		}
9892a2b0acfSKees Cook 
9902a2b0acfSKees Cook 		decompress_record(record);
9913a7d2fd1SKees Cook 		rc = pstore_mkfile(root, record);
9921dfff7ddSKees Cook 		if (rc) {
99383f70f07SKees Cook 			/* pstore_mkfile() did not take record, so free it. */
9942a2b0acfSKees Cook 			kfree(record->buf);
99583f70f07SKees Cook 			kfree(record);
9961dfff7ddSKees Cook 			if (rc != -EEXIST || !quiet)
9971dfff7ddSKees Cook 				failed++;
9981dfff7ddSKees Cook 		}
999ca01d6ddSTony Luck 	}
10002174f6dfSKees Cook 	if (psi->close)
100106cf91b4SChen Gong 		psi->close(psi);
100206cf91b4SChen Gong out:
1003f6f82851SKees Cook 	mutex_unlock(&psi->read_mutex);
1004ca01d6ddSTony Luck 
1005ca01d6ddSTony Luck 	if (failed)
1006656de42eSKees Cook 		pr_warn("failed to create %d record(s) from '%s'\n",
1007ca01d6ddSTony Luck 			failed, psi->name);
1008656de42eSKees Cook 	if (!stop_loop)
1009656de42eSKees Cook 		pr_err("looping? Too many records seen from '%s'\n",
1010656de42eSKees Cook 			psi->name);
1011ca01d6ddSTony Luck }
1012ca01d6ddSTony Luck 
10136dda9266SLuck, Tony static void pstore_dowork(struct work_struct *work)
10146dda9266SLuck, Tony {
10156dda9266SLuck, Tony 	pstore_get_records(1);
10166dda9266SLuck, Tony }
10176dda9266SLuck, Tony 
101824ed960aSKees Cook static void pstore_timefunc(struct timer_list *unused)
10196dda9266SLuck, Tony {
10206dda9266SLuck, Tony 	if (pstore_new_entry) {
10216dda9266SLuck, Tony 		pstore_new_entry = 0;
10226dda9266SLuck, Tony 		schedule_work(&pstore_work);
10236dda9266SLuck, Tony 	}
10246dda9266SLuck, Tony 
10256330d553SKees Cook 	if (pstore_update_ms >= 0)
10266330d553SKees Cook 		mod_timer(&pstore_timer,
10276330d553SKees Cook 			  jiffies + msecs_to_jiffies(pstore_update_ms));
10286dda9266SLuck, Tony }
10296dda9266SLuck, Tony 
1030*fe1d4758SKees Cook void __init pstore_choose_compression(void)
1031*fe1d4758SKees Cook {
1032*fe1d4758SKees Cook 	const struct pstore_zbackend *step;
1033*fe1d4758SKees Cook 
1034*fe1d4758SKees Cook 	if (!compress)
1035*fe1d4758SKees Cook 		return;
1036*fe1d4758SKees Cook 
1037*fe1d4758SKees Cook 	for (step = zbackends; step->name; step++) {
1038*fe1d4758SKees Cook 		if (!strcmp(compress, step->name)) {
1039*fe1d4758SKees Cook 			zbackend = step;
1040*fe1d4758SKees Cook 			pr_info("using %s compression\n", zbackend->name);
1041*fe1d4758SKees Cook 			return;
1042*fe1d4758SKees Cook 		}
1043*fe1d4758SKees Cook 	}
1044*fe1d4758SKees Cook }
1045*fe1d4758SKees Cook 
1046*fe1d4758SKees Cook module_param(compress, charp, 0444);
1047*fe1d4758SKees Cook MODULE_PARM_DESC(compress, "Pstore compression to use");
1048*fe1d4758SKees Cook 
1049dee28e72SMatthew Garrett module_param(backend, charp, 0444);
1050dee28e72SMatthew Garrett MODULE_PARM_DESC(backend, "Pstore backend to use");
1051