xref: /openbmc/linux/fs/pstore/platform.c (revision 239b716199d9aff0d09444b0086e23aacd6bd445)
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
37*239b7161SGeliang Tang #if defined(CONFIG_PSTORE_LZ4_COMPRESS) || defined(CONFIG_PSTORE_LZ4HC_COMPRESS)
388cfc8ddcSGeliang Tang #include <linux/lz4.h>
398cfc8ddcSGeliang Tang #endif
40*239b7161SGeliang Tang #ifdef CONFIG_PSTORE_842_COMPRESS
41*239b7161SGeliang Tang #include <linux/sw842.h>
42*239b7161SGeliang 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;
80dee28e72SMatthew Garrett 
81b0aad7a9SAruna Balakrishnaiah /* Compression parameters */
828cfc8ddcSGeliang Tang #ifdef CONFIG_PSTORE_ZLIB_COMPRESS
83b0aad7a9SAruna Balakrishnaiah #define COMPR_LEVEL 6
84b0aad7a9SAruna Balakrishnaiah #define WINDOW_BITS 12
85b0aad7a9SAruna Balakrishnaiah #define MEM_LEVEL 4
86b0aad7a9SAruna Balakrishnaiah static struct z_stream_s stream;
878cfc8ddcSGeliang Tang #else
888cfc8ddcSGeliang Tang static unsigned char *workspace;
898cfc8ddcSGeliang Tang #endif
908cfc8ddcSGeliang Tang 
918cfc8ddcSGeliang Tang struct pstore_zbackend {
928cfc8ddcSGeliang Tang 	int (*compress)(const void *in, void *out, size_t inlen, size_t outlen);
938cfc8ddcSGeliang Tang 	int (*decompress)(void *in, void *out, size_t inlen, size_t outlen);
948cfc8ddcSGeliang Tang 	void (*allocate)(void);
958cfc8ddcSGeliang Tang 	void (*free)(void);
968cfc8ddcSGeliang Tang 
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 
114381b872cSSeiji Aguchi static const char *get_reason_str(enum kmsg_dump_reason reason)
115381b872cSSeiji Aguchi {
116381b872cSSeiji Aguchi 	switch (reason) {
117381b872cSSeiji Aguchi 	case KMSG_DUMP_PANIC:
118381b872cSSeiji Aguchi 		return "Panic";
119381b872cSSeiji Aguchi 	case KMSG_DUMP_OOPS:
120381b872cSSeiji Aguchi 		return "Oops";
121381b872cSSeiji Aguchi 	case KMSG_DUMP_EMERG:
122381b872cSSeiji Aguchi 		return "Emergency";
123381b872cSSeiji Aguchi 	case KMSG_DUMP_RESTART:
124381b872cSSeiji Aguchi 		return "Restart";
125381b872cSSeiji Aguchi 	case KMSG_DUMP_HALT:
126381b872cSSeiji Aguchi 		return "Halt";
127381b872cSSeiji Aguchi 	case KMSG_DUMP_POWEROFF:
128381b872cSSeiji Aguchi 		return "Poweroff";
129381b872cSSeiji Aguchi 	default:
130381b872cSSeiji Aguchi 		return "Unknown";
131381b872cSSeiji Aguchi 	}
132381b872cSSeiji Aguchi }
1339f6af27fSTony Luck 
1349f244e9cSSeiji Aguchi bool pstore_cannot_block_path(enum kmsg_dump_reason reason)
1359f244e9cSSeiji Aguchi {
1369f244e9cSSeiji Aguchi 	/*
1379f244e9cSSeiji Aguchi 	 * In case of NMI path, pstore shouldn't be blocked
1389f244e9cSSeiji Aguchi 	 * regardless of reason.
1399f244e9cSSeiji Aguchi 	 */
1409f244e9cSSeiji Aguchi 	if (in_nmi())
1419f244e9cSSeiji Aguchi 		return true;
1429f244e9cSSeiji Aguchi 
1439f244e9cSSeiji Aguchi 	switch (reason) {
1449f244e9cSSeiji Aguchi 	/* In panic case, other cpus are stopped by smp_send_stop(). */
1459f244e9cSSeiji Aguchi 	case KMSG_DUMP_PANIC:
1469f244e9cSSeiji Aguchi 	/* Emergency restart shouldn't be blocked by spin lock. */
1479f244e9cSSeiji Aguchi 	case KMSG_DUMP_EMERG:
1489f244e9cSSeiji Aguchi 		return true;
1499f244e9cSSeiji Aguchi 	default:
1509f244e9cSSeiji Aguchi 		return false;
1519f244e9cSSeiji Aguchi 	}
1529f244e9cSSeiji Aguchi }
1539f244e9cSSeiji Aguchi EXPORT_SYMBOL_GPL(pstore_cannot_block_path);
1549f244e9cSSeiji Aguchi 
1558cfc8ddcSGeliang Tang #ifdef CONFIG_PSTORE_ZLIB_COMPRESS
156b0aad7a9SAruna Balakrishnaiah /* Derived from logfs_compress() */
1578cfc8ddcSGeliang Tang static int compress_zlib(const void *in, void *out, size_t inlen, size_t outlen)
158b0aad7a9SAruna Balakrishnaiah {
159b0aad7a9SAruna Balakrishnaiah 	int err, ret;
160b0aad7a9SAruna Balakrishnaiah 
161b0aad7a9SAruna Balakrishnaiah 	ret = -EIO;
162b0aad7a9SAruna Balakrishnaiah 	err = zlib_deflateInit2(&stream, COMPR_LEVEL, Z_DEFLATED, WINDOW_BITS,
163b0aad7a9SAruna Balakrishnaiah 						MEM_LEVEL, Z_DEFAULT_STRATEGY);
164b0aad7a9SAruna Balakrishnaiah 	if (err != Z_OK)
165b0aad7a9SAruna Balakrishnaiah 		goto error;
166b0aad7a9SAruna Balakrishnaiah 
167b0aad7a9SAruna Balakrishnaiah 	stream.next_in = in;
168b0aad7a9SAruna Balakrishnaiah 	stream.avail_in = inlen;
169b0aad7a9SAruna Balakrishnaiah 	stream.total_in = 0;
170b0aad7a9SAruna Balakrishnaiah 	stream.next_out = out;
171b0aad7a9SAruna Balakrishnaiah 	stream.avail_out = outlen;
172b0aad7a9SAruna Balakrishnaiah 	stream.total_out = 0;
173b0aad7a9SAruna Balakrishnaiah 
174b0aad7a9SAruna Balakrishnaiah 	err = zlib_deflate(&stream, Z_FINISH);
175b0aad7a9SAruna Balakrishnaiah 	if (err != Z_STREAM_END)
176b0aad7a9SAruna Balakrishnaiah 		goto error;
177b0aad7a9SAruna Balakrishnaiah 
178b0aad7a9SAruna Balakrishnaiah 	err = zlib_deflateEnd(&stream);
179b0aad7a9SAruna Balakrishnaiah 	if (err != Z_OK)
180b0aad7a9SAruna Balakrishnaiah 		goto error;
181b0aad7a9SAruna Balakrishnaiah 
182b0aad7a9SAruna Balakrishnaiah 	if (stream.total_out >= stream.total_in)
183b0aad7a9SAruna Balakrishnaiah 		goto error;
184b0aad7a9SAruna Balakrishnaiah 
185b0aad7a9SAruna Balakrishnaiah 	ret = stream.total_out;
186b0aad7a9SAruna Balakrishnaiah error:
187b0aad7a9SAruna Balakrishnaiah 	return ret;
188b0aad7a9SAruna Balakrishnaiah }
189b0aad7a9SAruna Balakrishnaiah 
190adb42f5eSAruna Balakrishnaiah /* Derived from logfs_uncompress */
1918cfc8ddcSGeliang Tang static int decompress_zlib(void *in, void *out, size_t inlen, size_t outlen)
192adb42f5eSAruna Balakrishnaiah {
193adb42f5eSAruna Balakrishnaiah 	int err, ret;
194adb42f5eSAruna Balakrishnaiah 
195adb42f5eSAruna Balakrishnaiah 	ret = -EIO;
196b61edf8eSAruna Balakrishnaiah 	err = zlib_inflateInit2(&stream, WINDOW_BITS);
197adb42f5eSAruna Balakrishnaiah 	if (err != Z_OK)
198adb42f5eSAruna Balakrishnaiah 		goto error;
199adb42f5eSAruna Balakrishnaiah 
200adb42f5eSAruna Balakrishnaiah 	stream.next_in = in;
201adb42f5eSAruna Balakrishnaiah 	stream.avail_in = inlen;
202adb42f5eSAruna Balakrishnaiah 	stream.total_in = 0;
203adb42f5eSAruna Balakrishnaiah 	stream.next_out = out;
204adb42f5eSAruna Balakrishnaiah 	stream.avail_out = outlen;
205adb42f5eSAruna Balakrishnaiah 	stream.total_out = 0;
206adb42f5eSAruna Balakrishnaiah 
207adb42f5eSAruna Balakrishnaiah 	err = zlib_inflate(&stream, Z_FINISH);
208adb42f5eSAruna Balakrishnaiah 	if (err != Z_STREAM_END)
209adb42f5eSAruna Balakrishnaiah 		goto error;
210adb42f5eSAruna Balakrishnaiah 
211adb42f5eSAruna Balakrishnaiah 	err = zlib_inflateEnd(&stream);
212adb42f5eSAruna Balakrishnaiah 	if (err != Z_OK)
213adb42f5eSAruna Balakrishnaiah 		goto error;
214adb42f5eSAruna Balakrishnaiah 
215adb42f5eSAruna Balakrishnaiah 	ret = stream.total_out;
216adb42f5eSAruna Balakrishnaiah error:
217adb42f5eSAruna Balakrishnaiah 	return ret;
218adb42f5eSAruna Balakrishnaiah }
219adb42f5eSAruna Balakrishnaiah 
2208cfc8ddcSGeliang Tang static void allocate_zlib(void)
221b0aad7a9SAruna Balakrishnaiah {
222b0aad7a9SAruna Balakrishnaiah 	size_t size;
2237de8fe2fSAruna Balakrishnaiah 	size_t cmpr;
224b0aad7a9SAruna Balakrishnaiah 
2257de8fe2fSAruna Balakrishnaiah 	switch (psinfo->bufsize) {
2267de8fe2fSAruna Balakrishnaiah 	/* buffer range for efivars */
2277de8fe2fSAruna Balakrishnaiah 	case 1000 ... 2000:
2287de8fe2fSAruna Balakrishnaiah 		cmpr = 56;
2297de8fe2fSAruna Balakrishnaiah 		break;
2307de8fe2fSAruna Balakrishnaiah 	case 2001 ... 3000:
2317de8fe2fSAruna Balakrishnaiah 		cmpr = 54;
2327de8fe2fSAruna Balakrishnaiah 		break;
2337de8fe2fSAruna Balakrishnaiah 	case 3001 ... 3999:
2347de8fe2fSAruna Balakrishnaiah 		cmpr = 52;
2357de8fe2fSAruna Balakrishnaiah 		break;
2367de8fe2fSAruna Balakrishnaiah 	/* buffer range for nvram, erst */
2377de8fe2fSAruna Balakrishnaiah 	case 4000 ... 10000:
2387de8fe2fSAruna Balakrishnaiah 		cmpr = 45;
2397de8fe2fSAruna Balakrishnaiah 		break;
2407de8fe2fSAruna Balakrishnaiah 	default:
2417de8fe2fSAruna Balakrishnaiah 		cmpr = 60;
2427de8fe2fSAruna Balakrishnaiah 		break;
2437de8fe2fSAruna Balakrishnaiah 	}
2447de8fe2fSAruna Balakrishnaiah 
2457de8fe2fSAruna Balakrishnaiah 	big_oops_buf_sz = (psinfo->bufsize * 100) / cmpr;
246b0aad7a9SAruna Balakrishnaiah 	big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
247b0aad7a9SAruna Balakrishnaiah 	if (big_oops_buf) {
248b0aad7a9SAruna Balakrishnaiah 		size = max(zlib_deflate_workspacesize(WINDOW_BITS, MEM_LEVEL),
249b0aad7a9SAruna Balakrishnaiah 			zlib_inflate_workspacesize());
250b0aad7a9SAruna Balakrishnaiah 		stream.workspace = kmalloc(size, GFP_KERNEL);
251b0aad7a9SAruna Balakrishnaiah 		if (!stream.workspace) {
252ef748853SFabian Frederick 			pr_err("No memory for compression workspace; skipping compression\n");
253b0aad7a9SAruna Balakrishnaiah 			kfree(big_oops_buf);
254b0aad7a9SAruna Balakrishnaiah 			big_oops_buf = NULL;
255b0aad7a9SAruna Balakrishnaiah 		}
256b0aad7a9SAruna Balakrishnaiah 	} else {
257ef748853SFabian Frederick 		pr_err("No memory for uncompressed data; skipping compression\n");
258b0aad7a9SAruna Balakrishnaiah 		stream.workspace = NULL;
259b0aad7a9SAruna Balakrishnaiah 	}
260b0aad7a9SAruna Balakrishnaiah 
261b0aad7a9SAruna Balakrishnaiah }
262b0aad7a9SAruna Balakrishnaiah 
2638cfc8ddcSGeliang Tang static void free_zlib(void)
264ee1d2674SGeliang Tang {
265ee1d2674SGeliang Tang 	kfree(stream.workspace);
266ee1d2674SGeliang Tang 	stream.workspace = NULL;
267ee1d2674SGeliang Tang 	kfree(big_oops_buf);
268ee1d2674SGeliang Tang 	big_oops_buf = NULL;
2698cfc8ddcSGeliang Tang 	big_oops_buf_sz = 0;
2708cfc8ddcSGeliang Tang }
2718cfc8ddcSGeliang Tang 
2723faf9354SBhumika Goyal static const struct pstore_zbackend backend_zlib = {
2738cfc8ddcSGeliang Tang 	.compress	= compress_zlib,
2748cfc8ddcSGeliang Tang 	.decompress	= decompress_zlib,
2758cfc8ddcSGeliang Tang 	.allocate	= allocate_zlib,
2768cfc8ddcSGeliang Tang 	.free		= free_zlib,
2778cfc8ddcSGeliang Tang 	.name		= "zlib",
2788cfc8ddcSGeliang Tang };
2798cfc8ddcSGeliang Tang #endif
2808cfc8ddcSGeliang Tang 
2818cfc8ddcSGeliang Tang #ifdef CONFIG_PSTORE_LZO_COMPRESS
2828cfc8ddcSGeliang Tang static int compress_lzo(const void *in, void *out, size_t inlen, size_t outlen)
2838cfc8ddcSGeliang Tang {
2848cfc8ddcSGeliang Tang 	int ret;
2858cfc8ddcSGeliang Tang 
2868cfc8ddcSGeliang Tang 	ret = lzo1x_1_compress(in, inlen, out, &outlen, workspace);
2878cfc8ddcSGeliang Tang 	if (ret != LZO_E_OK) {
2888cfc8ddcSGeliang Tang 		pr_err("lzo_compress error, ret = %d!\n", ret);
2898cfc8ddcSGeliang Tang 		return -EIO;
2908cfc8ddcSGeliang Tang 	}
2918cfc8ddcSGeliang Tang 
2928cfc8ddcSGeliang Tang 	return outlen;
2938cfc8ddcSGeliang Tang }
2948cfc8ddcSGeliang Tang 
2958cfc8ddcSGeliang Tang static int decompress_lzo(void *in, void *out, size_t inlen, size_t outlen)
2968cfc8ddcSGeliang Tang {
2978cfc8ddcSGeliang Tang 	int ret;
2988cfc8ddcSGeliang Tang 
2998cfc8ddcSGeliang Tang 	ret = lzo1x_decompress_safe(in, inlen, out, &outlen);
3008cfc8ddcSGeliang Tang 	if (ret != LZO_E_OK) {
3018cfc8ddcSGeliang Tang 		pr_err("lzo_decompress error, ret = %d!\n", ret);
3028cfc8ddcSGeliang Tang 		return -EIO;
3038cfc8ddcSGeliang Tang 	}
3048cfc8ddcSGeliang Tang 
3058cfc8ddcSGeliang Tang 	return outlen;
3068cfc8ddcSGeliang Tang }
3078cfc8ddcSGeliang Tang 
3088cfc8ddcSGeliang Tang static void allocate_lzo(void)
3098cfc8ddcSGeliang Tang {
3108cfc8ddcSGeliang Tang 	big_oops_buf_sz = lzo1x_worst_compress(psinfo->bufsize);
3118cfc8ddcSGeliang Tang 	big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
3128cfc8ddcSGeliang Tang 	if (big_oops_buf) {
3138cfc8ddcSGeliang Tang 		workspace = kmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
3148cfc8ddcSGeliang Tang 		if (!workspace) {
3158cfc8ddcSGeliang Tang 			pr_err("No memory for compression workspace; skipping compression\n");
3168cfc8ddcSGeliang Tang 			kfree(big_oops_buf);
3178cfc8ddcSGeliang Tang 			big_oops_buf = NULL;
3188cfc8ddcSGeliang Tang 		}
3198cfc8ddcSGeliang Tang 	} else {
3208cfc8ddcSGeliang Tang 		pr_err("No memory for uncompressed data; skipping compression\n");
3218cfc8ddcSGeliang Tang 		workspace = NULL;
3228cfc8ddcSGeliang Tang 	}
3238cfc8ddcSGeliang Tang }
3248cfc8ddcSGeliang Tang 
3258cfc8ddcSGeliang Tang static void free_lzo(void)
3268cfc8ddcSGeliang Tang {
3278cfc8ddcSGeliang Tang 	kfree(workspace);
3288cfc8ddcSGeliang Tang 	kfree(big_oops_buf);
3298cfc8ddcSGeliang Tang 	big_oops_buf = NULL;
3308cfc8ddcSGeliang Tang 	big_oops_buf_sz = 0;
3318cfc8ddcSGeliang Tang }
3328cfc8ddcSGeliang Tang 
3333faf9354SBhumika Goyal static const struct pstore_zbackend backend_lzo = {
3348cfc8ddcSGeliang Tang 	.compress	= compress_lzo,
3358cfc8ddcSGeliang Tang 	.decompress	= decompress_lzo,
3368cfc8ddcSGeliang Tang 	.allocate	= allocate_lzo,
3378cfc8ddcSGeliang Tang 	.free		= free_lzo,
3388cfc8ddcSGeliang Tang 	.name		= "lzo",
3398cfc8ddcSGeliang Tang };
3408cfc8ddcSGeliang Tang #endif
3418cfc8ddcSGeliang Tang 
342*239b7161SGeliang Tang #if defined(CONFIG_PSTORE_LZ4_COMPRESS) || defined(CONFIG_PSTORE_LZ4HC_COMPRESS)
3438cfc8ddcSGeliang Tang static int decompress_lz4(void *in, void *out, size_t inlen, size_t outlen)
3448cfc8ddcSGeliang Tang {
3458cfc8ddcSGeliang Tang 	int ret;
3468cfc8ddcSGeliang Tang 
347d21b5ff1SSven Schmidt 	ret = LZ4_decompress_safe(in, out, inlen, outlen);
348d21b5ff1SSven Schmidt 	if (ret < 0) {
349d21b5ff1SSven Schmidt 		/*
350d21b5ff1SSven Schmidt 		 * LZ4_decompress_safe will return an error code
351d21b5ff1SSven Schmidt 		 * (< 0) if decompression failed
352d21b5ff1SSven Schmidt 		 */
353d21b5ff1SSven Schmidt 		pr_err("LZ4_decompress_safe error, ret = %d!\n", ret);
3548cfc8ddcSGeliang Tang 		return -EIO;
3558cfc8ddcSGeliang Tang 	}
3568cfc8ddcSGeliang Tang 
357d21b5ff1SSven Schmidt 	return ret;
3588cfc8ddcSGeliang Tang }
3598cfc8ddcSGeliang Tang 
360*239b7161SGeliang Tang static void free_lz4(void)
361*239b7161SGeliang Tang {
362*239b7161SGeliang Tang 	kfree(workspace);
363*239b7161SGeliang Tang 	kfree(big_oops_buf);
364*239b7161SGeliang Tang 	big_oops_buf = NULL;
365*239b7161SGeliang Tang 	big_oops_buf_sz = 0;
366*239b7161SGeliang Tang }
367*239b7161SGeliang Tang #endif
368*239b7161SGeliang Tang 
369*239b7161SGeliang Tang #ifdef CONFIG_PSTORE_LZ4_COMPRESS
370*239b7161SGeliang Tang static int compress_lz4(const void *in, void *out, size_t inlen, size_t outlen)
371*239b7161SGeliang Tang {
372*239b7161SGeliang Tang 	int ret;
373*239b7161SGeliang Tang 
374*239b7161SGeliang Tang 	ret = LZ4_compress_default(in, out, inlen, outlen, workspace);
375*239b7161SGeliang Tang 	if (!ret) {
376*239b7161SGeliang Tang 		pr_err("LZ4_compress_default error; compression failed!\n");
377*239b7161SGeliang Tang 		return -EIO;
378*239b7161SGeliang Tang 	}
379*239b7161SGeliang Tang 
380*239b7161SGeliang Tang 	return ret;
381*239b7161SGeliang Tang }
382*239b7161SGeliang Tang 
3838cfc8ddcSGeliang Tang static void allocate_lz4(void)
3848cfc8ddcSGeliang Tang {
385d21b5ff1SSven Schmidt 	big_oops_buf_sz = LZ4_compressBound(psinfo->bufsize);
3868cfc8ddcSGeliang Tang 	big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
3878cfc8ddcSGeliang Tang 	if (big_oops_buf) {
3888cfc8ddcSGeliang Tang 		workspace = kmalloc(LZ4_MEM_COMPRESS, GFP_KERNEL);
3898cfc8ddcSGeliang Tang 		if (!workspace) {
3908cfc8ddcSGeliang Tang 			pr_err("No memory for compression workspace; skipping compression\n");
3918cfc8ddcSGeliang Tang 			kfree(big_oops_buf);
3928cfc8ddcSGeliang Tang 			big_oops_buf = NULL;
3938cfc8ddcSGeliang Tang 		}
3948cfc8ddcSGeliang Tang 	} else {
3958cfc8ddcSGeliang Tang 		pr_err("No memory for uncompressed data; skipping compression\n");
3968cfc8ddcSGeliang Tang 		workspace = NULL;
3978cfc8ddcSGeliang Tang 	}
3988cfc8ddcSGeliang Tang }
3998cfc8ddcSGeliang Tang 
400*239b7161SGeliang Tang static const struct pstore_zbackend backend_lz4 = {
401*239b7161SGeliang Tang 	.compress	= compress_lz4,
402*239b7161SGeliang Tang 	.decompress	= decompress_lz4,
403*239b7161SGeliang Tang 	.allocate	= allocate_lz4,
404*239b7161SGeliang Tang 	.free		= free_lz4,
405*239b7161SGeliang Tang 	.name		= "lz4",
406*239b7161SGeliang Tang };
407*239b7161SGeliang Tang #endif
408*239b7161SGeliang Tang 
409*239b7161SGeliang Tang #ifdef CONFIG_PSTORE_LZ4HC_COMPRESS
410*239b7161SGeliang Tang static int compress_lz4hc(const void *in, void *out,
411*239b7161SGeliang Tang 			  size_t inlen, size_t outlen)
412*239b7161SGeliang Tang {
413*239b7161SGeliang Tang 	int ret;
414*239b7161SGeliang Tang 
415*239b7161SGeliang Tang 	ret = LZ4_compress_HC(in, out, inlen, outlen,
416*239b7161SGeliang Tang 			      LZ4HC_DEFAULT_CLEVEL, workspace);
417*239b7161SGeliang Tang 	if (!ret) {
418*239b7161SGeliang Tang 		pr_err("LZ4_compress_HC error; compression failed!\n");
419*239b7161SGeliang Tang 		return -EIO;
420*239b7161SGeliang Tang 	}
421*239b7161SGeliang Tang 
422*239b7161SGeliang Tang 	return ret;
423*239b7161SGeliang Tang }
424*239b7161SGeliang Tang 
425*239b7161SGeliang Tang static void allocate_lz4hc(void)
426*239b7161SGeliang Tang {
427*239b7161SGeliang Tang 	big_oops_buf_sz = LZ4_compressBound(psinfo->bufsize);
428*239b7161SGeliang Tang 	big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
429*239b7161SGeliang Tang 	if (big_oops_buf) {
430*239b7161SGeliang Tang 		workspace = kmalloc(LZ4HC_MEM_COMPRESS, GFP_KERNEL);
431*239b7161SGeliang Tang 		if (!workspace) {
432*239b7161SGeliang Tang 			pr_err("No memory for compression workspace; skipping compression\n");
433*239b7161SGeliang Tang 			kfree(big_oops_buf);
434*239b7161SGeliang Tang 			big_oops_buf = NULL;
435*239b7161SGeliang Tang 		}
436*239b7161SGeliang Tang 	} else {
437*239b7161SGeliang Tang 		pr_err("No memory for uncompressed data; skipping compression\n");
438*239b7161SGeliang Tang 		workspace = NULL;
439*239b7161SGeliang Tang 	}
440*239b7161SGeliang Tang }
441*239b7161SGeliang Tang 
442*239b7161SGeliang Tang static const struct pstore_zbackend backend_lz4hc = {
443*239b7161SGeliang Tang 	.compress	= compress_lz4hc,
444*239b7161SGeliang Tang 	.decompress	= decompress_lz4,
445*239b7161SGeliang Tang 	.allocate	= allocate_lz4hc,
446*239b7161SGeliang Tang 	.free		= free_lz4,
447*239b7161SGeliang Tang 	.name		= "lz4hc",
448*239b7161SGeliang Tang };
449*239b7161SGeliang Tang #endif
450*239b7161SGeliang Tang 
451*239b7161SGeliang Tang #ifdef CONFIG_PSTORE_842_COMPRESS
452*239b7161SGeliang Tang static int compress_842(const void *in, void *out, size_t inlen, size_t outlen)
453*239b7161SGeliang Tang {
454*239b7161SGeliang Tang 	int ret;
455*239b7161SGeliang Tang 
456*239b7161SGeliang Tang 	ret = sw842_compress(in, inlen, out, (unsigned int *)&outlen, workspace);
457*239b7161SGeliang Tang 	if (ret) {
458*239b7161SGeliang Tang 		pr_err("sw842_compress error; compression failed!\n");
459*239b7161SGeliang Tang 		return ret;
460*239b7161SGeliang Tang 	}
461*239b7161SGeliang Tang 
462*239b7161SGeliang Tang 	return outlen;
463*239b7161SGeliang Tang }
464*239b7161SGeliang Tang 
465*239b7161SGeliang Tang static int decompress_842(void *in, void *out, size_t inlen, size_t outlen)
466*239b7161SGeliang Tang {
467*239b7161SGeliang Tang 	int ret;
468*239b7161SGeliang Tang 
469*239b7161SGeliang Tang 	ret = sw842_decompress(in, inlen, out, (unsigned int *)&outlen);
470*239b7161SGeliang Tang 	if (ret) {
471*239b7161SGeliang Tang 		pr_err("sw842_decompress error, ret = %d!\n", ret);
472*239b7161SGeliang Tang 		return ret;
473*239b7161SGeliang Tang 	}
474*239b7161SGeliang Tang 
475*239b7161SGeliang Tang 	return outlen;
476*239b7161SGeliang Tang }
477*239b7161SGeliang Tang 
478*239b7161SGeliang Tang static void allocate_842(void)
479*239b7161SGeliang Tang {
480*239b7161SGeliang Tang 	big_oops_buf_sz = psinfo->bufsize;
481*239b7161SGeliang Tang 	big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
482*239b7161SGeliang Tang 	if (big_oops_buf) {
483*239b7161SGeliang Tang 		workspace = kmalloc(SW842_MEM_COMPRESS, GFP_KERNEL);
484*239b7161SGeliang Tang 		if (!workspace) {
485*239b7161SGeliang Tang 			kfree(big_oops_buf);
486*239b7161SGeliang Tang 			big_oops_buf = NULL;
487*239b7161SGeliang Tang 		}
488*239b7161SGeliang Tang 	} else {
489*239b7161SGeliang Tang 		pr_err("No memory for uncompressed data; skipping compression\n");
490*239b7161SGeliang Tang 		workspace = NULL;
491*239b7161SGeliang Tang 	}
492*239b7161SGeliang Tang }
493*239b7161SGeliang Tang 
494*239b7161SGeliang Tang static void free_842(void)
4958cfc8ddcSGeliang Tang {
4968cfc8ddcSGeliang Tang 	kfree(workspace);
4978cfc8ddcSGeliang Tang 	kfree(big_oops_buf);
4988cfc8ddcSGeliang Tang 	big_oops_buf = NULL;
4998cfc8ddcSGeliang Tang 	big_oops_buf_sz = 0;
5008cfc8ddcSGeliang Tang }
5018cfc8ddcSGeliang Tang 
502*239b7161SGeliang Tang static const struct pstore_zbackend backend_842 = {
503*239b7161SGeliang Tang 	.compress	= compress_842,
504*239b7161SGeliang Tang 	.decompress	= decompress_842,
505*239b7161SGeliang Tang 	.allocate	= allocate_842,
506*239b7161SGeliang Tang 	.free		= free_842,
507*239b7161SGeliang Tang 	.name		= "842",
5088cfc8ddcSGeliang Tang };
5098cfc8ddcSGeliang Tang #endif
5108cfc8ddcSGeliang Tang 
5113faf9354SBhumika Goyal static const struct pstore_zbackend *zbackend =
5128cfc8ddcSGeliang Tang #if defined(CONFIG_PSTORE_ZLIB_COMPRESS)
5138cfc8ddcSGeliang Tang 	&backend_zlib;
5148cfc8ddcSGeliang Tang #elif defined(CONFIG_PSTORE_LZO_COMPRESS)
5158cfc8ddcSGeliang Tang 	&backend_lzo;
5168cfc8ddcSGeliang Tang #elif defined(CONFIG_PSTORE_LZ4_COMPRESS)
5178cfc8ddcSGeliang Tang 	&backend_lz4;
518*239b7161SGeliang Tang #elif defined(CONFIG_PSTORE_LZ4HC_COMPRESS)
519*239b7161SGeliang Tang 	&backend_lz4hc;
520*239b7161SGeliang Tang #elif defined(CONFIG_PSTORE_842_COMPRESS)
521*239b7161SGeliang Tang 	&backend_842;
5228cfc8ddcSGeliang Tang #else
5238cfc8ddcSGeliang Tang 	NULL;
5248cfc8ddcSGeliang Tang #endif
5258cfc8ddcSGeliang Tang 
5268cfc8ddcSGeliang Tang static int pstore_compress(const void *in, void *out,
5278cfc8ddcSGeliang Tang 			   size_t inlen, size_t outlen)
5288cfc8ddcSGeliang Tang {
5298cfc8ddcSGeliang Tang 	if (zbackend)
5308cfc8ddcSGeliang Tang 		return zbackend->compress(in, out, inlen, outlen);
5318cfc8ddcSGeliang Tang 	else
5328cfc8ddcSGeliang Tang 		return -EIO;
5338cfc8ddcSGeliang Tang }
5348cfc8ddcSGeliang Tang 
5358cfc8ddcSGeliang Tang static int pstore_decompress(void *in, void *out, size_t inlen, size_t outlen)
5368cfc8ddcSGeliang Tang {
5378cfc8ddcSGeliang Tang 	if (zbackend)
5388cfc8ddcSGeliang Tang 		return zbackend->decompress(in, out, inlen, outlen);
5398cfc8ddcSGeliang Tang 	else
5408cfc8ddcSGeliang Tang 		return -EIO;
5418cfc8ddcSGeliang Tang }
5428cfc8ddcSGeliang Tang 
5438cfc8ddcSGeliang Tang static void allocate_buf_for_compression(void)
5448cfc8ddcSGeliang Tang {
5458cfc8ddcSGeliang Tang 	if (zbackend) {
5468cfc8ddcSGeliang Tang 		pr_info("using %s compression\n", zbackend->name);
5478cfc8ddcSGeliang Tang 		zbackend->allocate();
5488cfc8ddcSGeliang Tang 	} else {
5498cfc8ddcSGeliang Tang 		pr_err("allocate compression buffer error!\n");
5508cfc8ddcSGeliang Tang 	}
5518cfc8ddcSGeliang Tang }
5528cfc8ddcSGeliang Tang 
5538cfc8ddcSGeliang Tang static void free_buf_for_compression(void)
5548cfc8ddcSGeliang Tang {
5558cfc8ddcSGeliang Tang 	if (zbackend)
5568cfc8ddcSGeliang Tang 		zbackend->free();
5578cfc8ddcSGeliang Tang 	else
5588cfc8ddcSGeliang Tang 		pr_err("free compression buffer error!\n");
559ee1d2674SGeliang Tang }
560ee1d2674SGeliang Tang 
561b0aad7a9SAruna Balakrishnaiah /*
562b0aad7a9SAruna Balakrishnaiah  * Called when compression fails, since the printk buffer
563b0aad7a9SAruna Balakrishnaiah  * would be fetched for compression calling it again when
564b0aad7a9SAruna Balakrishnaiah  * compression fails would have moved the iterator of
565b0aad7a9SAruna Balakrishnaiah  * printk buffer which results in fetching old contents.
566b0aad7a9SAruna Balakrishnaiah  * Copy the recent messages from big_oops_buf to psinfo->buf
567b0aad7a9SAruna Balakrishnaiah  */
568b0aad7a9SAruna Balakrishnaiah static size_t copy_kmsg_to_buffer(int hsize, size_t len)
569b0aad7a9SAruna Balakrishnaiah {
570b0aad7a9SAruna Balakrishnaiah 	size_t total_len;
571b0aad7a9SAruna Balakrishnaiah 	size_t diff;
572b0aad7a9SAruna Balakrishnaiah 
573b0aad7a9SAruna Balakrishnaiah 	total_len = hsize + len;
574b0aad7a9SAruna Balakrishnaiah 
575b0aad7a9SAruna Balakrishnaiah 	if (total_len > psinfo->bufsize) {
576b0aad7a9SAruna Balakrishnaiah 		diff = total_len - psinfo->bufsize + hsize;
577b0aad7a9SAruna Balakrishnaiah 		memcpy(psinfo->buf, big_oops_buf, hsize);
578b0aad7a9SAruna Balakrishnaiah 		memcpy(psinfo->buf + hsize, big_oops_buf + diff,
579b0aad7a9SAruna Balakrishnaiah 					psinfo->bufsize - hsize);
580b0aad7a9SAruna Balakrishnaiah 		total_len = psinfo->bufsize;
581b0aad7a9SAruna Balakrishnaiah 	} else
582b0aad7a9SAruna Balakrishnaiah 		memcpy(psinfo->buf, big_oops_buf, total_len);
583b0aad7a9SAruna Balakrishnaiah 
584b0aad7a9SAruna Balakrishnaiah 	return total_len;
585b0aad7a9SAruna Balakrishnaiah }
586b0aad7a9SAruna Balakrishnaiah 
587e581ca81SKees Cook void pstore_record_init(struct pstore_record *record,
588e581ca81SKees Cook 			struct pstore_info *psinfo)
589e581ca81SKees Cook {
590e581ca81SKees Cook 	memset(record, 0, sizeof(*record));
591e581ca81SKees Cook 
592e581ca81SKees Cook 	record->psi = psinfo;
593c7f3c595SKees Cook 
594c7f3c595SKees Cook 	/* Report zeroed timestamp if called before timekeeping has resumed. */
595df27067eSArnd Bergmann 	record->time = ns_to_timespec(ktime_get_real_fast_ns());
596e581ca81SKees Cook }
597e581ca81SKees Cook 
598ca01d6ddSTony Luck /*
599ca01d6ddSTony Luck  * callback from kmsg_dump. (s2,l2) has the most recently
600ca01d6ddSTony Luck  * written bytes, older bytes are in (s1,l1). Save as much
601ca01d6ddSTony Luck  * as we can from the end of the buffer.
602ca01d6ddSTony Luck  */
603ca01d6ddSTony Luck static void pstore_dump(struct kmsg_dumper *dumper,
604e2ae715dSKay Sievers 			enum kmsg_dump_reason reason)
605ca01d6ddSTony Luck {
606e2ae715dSKay Sievers 	unsigned long	total = 0;
607381b872cSSeiji Aguchi 	const char	*why;
608b94fdd07SMatthew Garrett 	unsigned int	part = 1;
609abd4d558SDon Zickus 	unsigned long	flags = 0;
61098e44fdaSNamhyung Kim 	int		is_locked;
611e2ae715dSKay Sievers 	int		ret;
612ca01d6ddSTony Luck 
613381b872cSSeiji Aguchi 	why = get_reason_str(reason);
6149f6af27fSTony Luck 
6159f244e9cSSeiji Aguchi 	if (pstore_cannot_block_path(reason)) {
6169f244e9cSSeiji Aguchi 		is_locked = spin_trylock_irqsave(&psinfo->buf_lock, flags);
6179f244e9cSSeiji Aguchi 		if (!is_locked) {
6189f244e9cSSeiji Aguchi 			pr_err("pstore dump routine blocked in %s path, may corrupt error record\n"
6199f244e9cSSeiji Aguchi 				       , in_nmi() ? "NMI" : why);
620959217c8SLi Pengcheng 			return;
6219f244e9cSSeiji Aguchi 		}
62298e44fdaSNamhyung Kim 	} else {
623abd4d558SDon Zickus 		spin_lock_irqsave(&psinfo->buf_lock, flags);
62498e44fdaSNamhyung Kim 		is_locked = 1;
62598e44fdaSNamhyung Kim 	}
626ca01d6ddSTony Luck 	oopscount++;
627ca01d6ddSTony Luck 	while (total < kmsg_bytes) {
628e2ae715dSKay Sievers 		char *dst;
62976cc9580SKees Cook 		size_t dst_size;
63076cc9580SKees Cook 		int header_size;
631b0aad7a9SAruna Balakrishnaiah 		int zipped_len = -1;
63276cc9580SKees Cook 		size_t dump_size;
633e581ca81SKees Cook 		struct pstore_record record;
634e581ca81SKees Cook 
635e581ca81SKees Cook 		pstore_record_init(&record, psinfo);
636e581ca81SKees Cook 		record.type = PSTORE_TYPE_DMESG;
637e581ca81SKees Cook 		record.count = oopscount;
638e581ca81SKees Cook 		record.reason = reason;
639e581ca81SKees Cook 		record.part = part;
640e581ca81SKees Cook 		record.buf = psinfo->buf;
641e2ae715dSKay Sievers 
642f0e2efcfSKonstantin Khlebnikov 		if (big_oops_buf && is_locked) {
643b0aad7a9SAruna Balakrishnaiah 			dst = big_oops_buf;
64476cc9580SKees Cook 			dst_size = big_oops_buf_sz;
645235f6d15SNamhyung Kim 		} else {
646235f6d15SNamhyung Kim 			dst = psinfo->buf;
64776cc9580SKees Cook 			dst_size = psinfo->bufsize;
648235f6d15SNamhyung Kim 		}
649235f6d15SNamhyung Kim 
65076cc9580SKees Cook 		/* Write dump header. */
65176cc9580SKees Cook 		header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why,
65276cc9580SKees Cook 				 oopscount, part);
65376cc9580SKees Cook 		dst_size -= header_size;
654b0aad7a9SAruna Balakrishnaiah 
65576cc9580SKees Cook 		/* Write dump contents. */
65676cc9580SKees Cook 		if (!kmsg_dump_get_buffer(dumper, true, dst + header_size,
65776cc9580SKees Cook 					  dst_size, &dump_size))
658b0aad7a9SAruna Balakrishnaiah 			break;
659b0aad7a9SAruna Balakrishnaiah 
660235f6d15SNamhyung Kim 		if (big_oops_buf && is_locked) {
661b0aad7a9SAruna Balakrishnaiah 			zipped_len = pstore_compress(dst, psinfo->buf,
66276cc9580SKees Cook 						header_size + dump_size,
66376cc9580SKees Cook 						psinfo->bufsize);
664b0aad7a9SAruna Balakrishnaiah 
665b0aad7a9SAruna Balakrishnaiah 			if (zipped_len > 0) {
66676cc9580SKees Cook 				record.compressed = true;
66776cc9580SKees Cook 				record.size = zipped_len;
668b0aad7a9SAruna Balakrishnaiah 			} else {
66976cc9580SKees Cook 				record.size = copy_kmsg_to_buffer(header_size,
67076cc9580SKees Cook 								  dump_size);
671b0aad7a9SAruna Balakrishnaiah 			}
672b0aad7a9SAruna Balakrishnaiah 		} else {
67376cc9580SKees Cook 			record.size = header_size + dump_size;
674b0aad7a9SAruna Balakrishnaiah 		}
675b0aad7a9SAruna Balakrishnaiah 
67676cc9580SKees Cook 		ret = psinfo->write(&record);
677b238b8faSChen Gong 		if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted())
6786dda9266SLuck, Tony 			pstore_new_entry = 1;
679e2ae715dSKay Sievers 
68076cc9580SKees Cook 		total += record.size;
68156280682SMatthew Garrett 		part++;
682ca01d6ddSTony Luck 	}
683abd4d558SDon Zickus 	if (is_locked)
6849f244e9cSSeiji Aguchi 		spin_unlock_irqrestore(&psinfo->buf_lock, flags);
685ca01d6ddSTony Luck }
686ca01d6ddSTony Luck 
687ca01d6ddSTony Luck static struct kmsg_dumper pstore_dumper = {
688ca01d6ddSTony Luck 	.dump = pstore_dump,
689ca01d6ddSTony Luck };
690ca01d6ddSTony Luck 
691306e5c2aSGeliang Tang /*
692306e5c2aSGeliang Tang  * Register with kmsg_dump to save last part of console log on panic.
693306e5c2aSGeliang Tang  */
69418730411SGeliang Tang static void pstore_register_kmsg(void)
69518730411SGeliang Tang {
69618730411SGeliang Tang 	kmsg_dump_register(&pstore_dumper);
69718730411SGeliang Tang }
69818730411SGeliang Tang 
699ee1d2674SGeliang Tang static void pstore_unregister_kmsg(void)
700ee1d2674SGeliang Tang {
701ee1d2674SGeliang Tang 	kmsg_dump_unregister(&pstore_dumper);
702ee1d2674SGeliang Tang }
703ee1d2674SGeliang Tang 
704f29e5956SAnton Vorontsov #ifdef CONFIG_PSTORE_CONSOLE
705f29e5956SAnton Vorontsov static void pstore_console_write(struct console *con, const char *s, unsigned c)
706f29e5956SAnton Vorontsov {
707f29e5956SAnton Vorontsov 	const char *e = s + c;
708f29e5956SAnton Vorontsov 
709f29e5956SAnton Vorontsov 	while (s < e) {
710e581ca81SKees Cook 		struct pstore_record record;
711f29e5956SAnton Vorontsov 		unsigned long flags;
712f29e5956SAnton Vorontsov 
713e581ca81SKees Cook 		pstore_record_init(&record, psinfo);
714e581ca81SKees Cook 		record.type = PSTORE_TYPE_CONSOLE;
715e581ca81SKees Cook 
716f29e5956SAnton Vorontsov 		if (c > psinfo->bufsize)
717f29e5956SAnton Vorontsov 			c = psinfo->bufsize;
71880c9d03cSChuansheng Liu 
71980c9d03cSChuansheng Liu 		if (oops_in_progress) {
72080c9d03cSChuansheng Liu 			if (!spin_trylock_irqsave(&psinfo->buf_lock, flags))
72180c9d03cSChuansheng Liu 				break;
72280c9d03cSChuansheng Liu 		} else {
723f29e5956SAnton Vorontsov 			spin_lock_irqsave(&psinfo->buf_lock, flags);
72480c9d03cSChuansheng Liu 		}
725b10b4711SKees Cook 		record.buf = (char *)s;
726b10b4711SKees Cook 		record.size = c;
7274c9ec219SKees Cook 		psinfo->write(&record);
728f29e5956SAnton Vorontsov 		spin_unlock_irqrestore(&psinfo->buf_lock, flags);
729f29e5956SAnton Vorontsov 		s += c;
730f29e5956SAnton Vorontsov 		c = e - s;
731f29e5956SAnton Vorontsov 	}
732f29e5956SAnton Vorontsov }
733f29e5956SAnton Vorontsov 
734f29e5956SAnton Vorontsov static struct console pstore_console = {
735f29e5956SAnton Vorontsov 	.name	= "pstore",
736f29e5956SAnton Vorontsov 	.write	= pstore_console_write,
737f29e5956SAnton Vorontsov 	.flags	= CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME,
738f29e5956SAnton Vorontsov 	.index	= -1,
739f29e5956SAnton Vorontsov };
740f29e5956SAnton Vorontsov 
741f29e5956SAnton Vorontsov static void pstore_register_console(void)
742f29e5956SAnton Vorontsov {
743f29e5956SAnton Vorontsov 	register_console(&pstore_console);
744f29e5956SAnton Vorontsov }
745ee1d2674SGeliang Tang 
746ee1d2674SGeliang Tang static void pstore_unregister_console(void)
747ee1d2674SGeliang Tang {
748ee1d2674SGeliang Tang 	unregister_console(&pstore_console);
749ee1d2674SGeliang Tang }
750f29e5956SAnton Vorontsov #else
751f29e5956SAnton Vorontsov static void pstore_register_console(void) {}
752ee1d2674SGeliang Tang static void pstore_unregister_console(void) {}
753f29e5956SAnton Vorontsov #endif
754f29e5956SAnton Vorontsov 
7554c9ec219SKees Cook static int pstore_write_user_compat(struct pstore_record *record,
756fdd03118SKees Cook 				    const char __user *buf)
7575bf6d1b9SMark Salyzyn {
75830800d99SKees Cook 	int ret = 0;
7595bf6d1b9SMark Salyzyn 
76030800d99SKees Cook 	if (record->buf)
76130800d99SKees Cook 		return -EINVAL;
7625bf6d1b9SMark Salyzyn 
763077090afSGeliang Tang 	record->buf = memdup_user(buf, record->size);
764dfd6fa39SHirofumi Nakagawa 	if (IS_ERR(record->buf)) {
765077090afSGeliang Tang 		ret = PTR_ERR(record->buf);
76630800d99SKees Cook 		goto out;
7675bf6d1b9SMark Salyzyn 	}
76830800d99SKees Cook 
7694c9ec219SKees Cook 	ret = record->psi->write(record);
77030800d99SKees Cook 
77130800d99SKees Cook 	kfree(record->buf);
772077090afSGeliang Tang out:
77330800d99SKees Cook 	record->buf = NULL;
77430800d99SKees Cook 
77530800d99SKees Cook 	return unlikely(ret < 0) ? ret : record->size;
7765bf6d1b9SMark Salyzyn }
7775bf6d1b9SMark Salyzyn 
778ca01d6ddSTony Luck /*
779ca01d6ddSTony Luck  * platform specific persistent storage driver registers with
780ca01d6ddSTony Luck  * us here. If pstore is already mounted, call the platform
781ca01d6ddSTony Luck  * read function right away to populate the file system. If not
782ca01d6ddSTony Luck  * then the pstore mount code will call us later to fill out
783ca01d6ddSTony Luck  * the file system.
784ca01d6ddSTony Luck  */
785ca01d6ddSTony Luck int pstore_register(struct pstore_info *psi)
786ca01d6ddSTony Luck {
787ca01d6ddSTony Luck 	struct module *owner = psi->owner;
788ca01d6ddSTony Luck 
7890d7cd09aSKees Cook 	if (backend && strcmp(backend, psi->name)) {
7900d7cd09aSKees Cook 		pr_warn("ignoring unexpected backend '%s'\n", psi->name);
7918e48b1a8SLenny Szubowicz 		return -EPERM;
7920d7cd09aSKees Cook 	}
7938e48b1a8SLenny Szubowicz 
7944c9ec219SKees Cook 	/* Sanity check flags. */
7954c9ec219SKees Cook 	if (!psi->flags) {
7964c9ec219SKees Cook 		pr_warn("backend '%s' must support at least one frontend\n",
7974c9ec219SKees Cook 			psi->name);
7984c9ec219SKees Cook 		return -EINVAL;
7994c9ec219SKees Cook 	}
8004c9ec219SKees Cook 
8014c9ec219SKees Cook 	/* Check for required functions. */
8024c9ec219SKees Cook 	if (!psi->read || !psi->write) {
8034c9ec219SKees Cook 		pr_warn("backend '%s' must implement read() and write()\n",
8044c9ec219SKees Cook 			psi->name);
8054c9ec219SKees Cook 		return -EINVAL;
8064c9ec219SKees Cook 	}
8074c9ec219SKees Cook 
808ca01d6ddSTony Luck 	spin_lock(&pstore_lock);
809ca01d6ddSTony Luck 	if (psinfo) {
8100d7cd09aSKees Cook 		pr_warn("backend '%s' already loaded: ignoring '%s'\n",
8110d7cd09aSKees Cook 			psinfo->name, psi->name);
812ca01d6ddSTony Luck 		spin_unlock(&pstore_lock);
813ca01d6ddSTony Luck 		return -EBUSY;
814ca01d6ddSTony Luck 	}
815dee28e72SMatthew Garrett 
8164c9ec219SKees Cook 	if (!psi->write_user)
8174c9ec219SKees Cook 		psi->write_user = pstore_write_user_compat;
818ca01d6ddSTony Luck 	psinfo = psi;
819f6f82851SKees Cook 	mutex_init(&psinfo->read_mutex);
820ca01d6ddSTony Luck 	spin_unlock(&pstore_lock);
821ca01d6ddSTony Luck 
822ca01d6ddSTony Luck 	if (owner && !try_module_get(owner)) {
823ca01d6ddSTony Luck 		psinfo = NULL;
824ca01d6ddSTony Luck 		return -EINVAL;
825ca01d6ddSTony Luck 	}
826ca01d6ddSTony Luck 
827b0aad7a9SAruna Balakrishnaiah 	allocate_buf_for_compression();
828b0aad7a9SAruna Balakrishnaiah 
829ca01d6ddSTony Luck 	if (pstore_is_mounted())
8306dda9266SLuck, Tony 		pstore_get_records(0);
831ca01d6ddSTony Luck 
832c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_DMESG)
83318730411SGeliang Tang 		pstore_register_kmsg();
834c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_CONSOLE)
835f29e5956SAnton Vorontsov 		pstore_register_console();
836c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_FTRACE)
83765f8c95eSAnton Vorontsov 		pstore_register_ftrace();
838c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_PMSG)
8399d5438f4SMark Salyzyn 		pstore_register_pmsg();
840ca01d6ddSTony Luck 
8416330d553SKees Cook 	/* Start watching for new records, if desired. */
842a3f5f075SAnton Vorontsov 	if (pstore_update_ms >= 0) {
843a3f5f075SAnton Vorontsov 		pstore_timer.expires = jiffies +
844a3f5f075SAnton Vorontsov 			msecs_to_jiffies(pstore_update_ms);
8456dda9266SLuck, Tony 		add_timer(&pstore_timer);
846a3f5f075SAnton Vorontsov 	}
8476dda9266SLuck, Tony 
84842222c2aSWang Long 	/*
84942222c2aSWang Long 	 * Update the module parameter backend, so it is visible
85042222c2aSWang Long 	 * through /sys/module/pstore/parameters/backend
85142222c2aSWang Long 	 */
85242222c2aSWang Long 	backend = psi->name;
85342222c2aSWang Long 
854ef748853SFabian Frederick 	pr_info("Registered %s as persistent store backend\n", psi->name);
8558e48b1a8SLenny Szubowicz 
8561344dd86SKees Cook 	module_put(owner);
8571344dd86SKees Cook 
858ca01d6ddSTony Luck 	return 0;
859ca01d6ddSTony Luck }
860ca01d6ddSTony Luck EXPORT_SYMBOL_GPL(pstore_register);
861ca01d6ddSTony Luck 
862ee1d2674SGeliang Tang void pstore_unregister(struct pstore_info *psi)
863ee1d2674SGeliang Tang {
8646330d553SKees Cook 	/* Stop timer and make sure all work has finished. */
8656330d553SKees Cook 	pstore_update_ms = -1;
8666330d553SKees Cook 	del_timer_sync(&pstore_timer);
8676330d553SKees Cook 	flush_work(&pstore_work);
8686330d553SKees Cook 
869c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_PMSG)
870ee1d2674SGeliang Tang 		pstore_unregister_pmsg();
871c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_FTRACE)
872ee1d2674SGeliang Tang 		pstore_unregister_ftrace();
873c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_CONSOLE)
874ee1d2674SGeliang Tang 		pstore_unregister_console();
875c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_DMESG)
876ee1d2674SGeliang Tang 		pstore_unregister_kmsg();
877ee1d2674SGeliang Tang 
878ee1d2674SGeliang Tang 	free_buf_for_compression();
879ee1d2674SGeliang Tang 
880ee1d2674SGeliang Tang 	psinfo = NULL;
881ee1d2674SGeliang Tang 	backend = NULL;
882ee1d2674SGeliang Tang }
883ee1d2674SGeliang Tang EXPORT_SYMBOL_GPL(pstore_unregister);
884ee1d2674SGeliang Tang 
885634f8f51SKees Cook static void decompress_record(struct pstore_record *record)
886634f8f51SKees Cook {
887634f8f51SKees Cook 	int unzipped_len;
8887e8cc8dcSKees Cook 	char *decompressed;
889634f8f51SKees Cook 
8904a16d1cbSAnkit Kumar 	if (!record->compressed)
8914a16d1cbSAnkit Kumar 		return;
8924a16d1cbSAnkit Kumar 
893634f8f51SKees Cook 	/* Only PSTORE_TYPE_DMESG support compression. */
8944a16d1cbSAnkit Kumar 	if (record->type != PSTORE_TYPE_DMESG) {
895634f8f51SKees Cook 		pr_warn("ignored compressed record type %d\n", record->type);
896634f8f51SKees Cook 		return;
897634f8f51SKees Cook 	}
898634f8f51SKees Cook 
899634f8f51SKees Cook 	/* No compression method has created the common buffer. */
900634f8f51SKees Cook 	if (!big_oops_buf) {
901634f8f51SKees Cook 		pr_warn("no decompression buffer allocated\n");
902634f8f51SKees Cook 		return;
903634f8f51SKees Cook 	}
904634f8f51SKees Cook 
905634f8f51SKees Cook 	unzipped_len = pstore_decompress(record->buf, big_oops_buf,
906634f8f51SKees Cook 					 record->size, big_oops_buf_sz);
9077e8cc8dcSKees Cook 	if (unzipped_len <= 0) {
9087e8cc8dcSKees Cook 		pr_err("decompression failed: %d\n", unzipped_len);
9097e8cc8dcSKees Cook 		return;
9107e8cc8dcSKees Cook 	}
9117e8cc8dcSKees Cook 
9127e8cc8dcSKees Cook 	/* Build new buffer for decompressed contents. */
9137e8cc8dcSKees Cook 	decompressed = kmalloc(unzipped_len + record->ecc_notice_size,
9147e8cc8dcSKees Cook 			       GFP_KERNEL);
9157e8cc8dcSKees Cook 	if (!decompressed) {
9167e8cc8dcSKees Cook 		pr_err("decompression ran out of memory\n");
9177e8cc8dcSKees Cook 		return;
9187e8cc8dcSKees Cook 	}
9197e8cc8dcSKees Cook 	memcpy(decompressed, big_oops_buf, unzipped_len);
9207e8cc8dcSKees Cook 
9217e8cc8dcSKees Cook 	/* Append ECC notice to decompressed buffer. */
9227e8cc8dcSKees Cook 	memcpy(decompressed + unzipped_len, record->buf + record->size,
923634f8f51SKees Cook 	       record->ecc_notice_size);
9247e8cc8dcSKees Cook 
9257e8cc8dcSKees Cook 	/* Swap out compresed contents with decompressed contents. */
926634f8f51SKees Cook 	kfree(record->buf);
9277e8cc8dcSKees Cook 	record->buf = decompressed;
928634f8f51SKees Cook 	record->size = unzipped_len;
929634f8f51SKees Cook 	record->compressed = false;
930634f8f51SKees Cook }
931634f8f51SKees Cook 
932ca01d6ddSTony Luck /*
9333a7d2fd1SKees Cook  * Read all the records from one persistent store backend. Create
9346dda9266SLuck, Tony  * files in our filesystem.  Don't warn about -EEXIST errors
9356dda9266SLuck, Tony  * when we are re-scanning the backing store looking to add new
9366dda9266SLuck, Tony  * error records.
937ca01d6ddSTony Luck  */
9383a7d2fd1SKees Cook void pstore_get_backend_records(struct pstore_info *psi,
9393a7d2fd1SKees Cook 				struct dentry *root, int quiet)
940ca01d6ddSTony Luck {
9412a2b0acfSKees Cook 	int failed = 0;
942656de42eSKees Cook 	unsigned int stop_loop = 65536;
943ca01d6ddSTony Luck 
9443a7d2fd1SKees Cook 	if (!psi || !root)
945ca01d6ddSTony Luck 		return;
946ca01d6ddSTony Luck 
947f6f82851SKees Cook 	mutex_lock(&psi->read_mutex);
9482174f6dfSKees Cook 	if (psi->open && psi->open(psi))
94906cf91b4SChen Gong 		goto out;
95006cf91b4SChen Gong 
9511dfff7ddSKees Cook 	/*
9521dfff7ddSKees Cook 	 * Backend callback read() allocates record.buf. decompress_record()
9531dfff7ddSKees Cook 	 * may reallocate record.buf. On success, pstore_mkfile() will keep
9541dfff7ddSKees Cook 	 * the record.buf, so free it only on failure.
9551dfff7ddSKees Cook 	 */
956656de42eSKees Cook 	for (; stop_loop; stop_loop--) {
9572a2b0acfSKees Cook 		struct pstore_record *record;
9582a2b0acfSKees Cook 		int rc;
9592a2b0acfSKees Cook 
9602a2b0acfSKees Cook 		record = kzalloc(sizeof(*record), GFP_KERNEL);
9612a2b0acfSKees Cook 		if (!record) {
9622a2b0acfSKees Cook 			pr_err("out of memory creating record\n");
9632a2b0acfSKees Cook 			break;
9642a2b0acfSKees Cook 		}
965e581ca81SKees Cook 		pstore_record_init(record, psi);
9662a2b0acfSKees Cook 
9672a2b0acfSKees Cook 		record->size = psi->read(record);
9682a2b0acfSKees Cook 
9692a2b0acfSKees Cook 		/* No more records left in backend? */
970f6525b96SDouglas Anderson 		if (record->size <= 0) {
971f6525b96SDouglas Anderson 			kfree(record);
9722a2b0acfSKees Cook 			break;
973f6525b96SDouglas Anderson 		}
9742a2b0acfSKees Cook 
9752a2b0acfSKees Cook 		decompress_record(record);
9763a7d2fd1SKees Cook 		rc = pstore_mkfile(root, record);
9771dfff7ddSKees Cook 		if (rc) {
97883f70f07SKees Cook 			/* pstore_mkfile() did not take record, so free it. */
9792a2b0acfSKees Cook 			kfree(record->buf);
98083f70f07SKees Cook 			kfree(record);
9811dfff7ddSKees Cook 			if (rc != -EEXIST || !quiet)
9821dfff7ddSKees Cook 				failed++;
9831dfff7ddSKees Cook 		}
984ca01d6ddSTony Luck 	}
9852174f6dfSKees Cook 	if (psi->close)
98606cf91b4SChen Gong 		psi->close(psi);
98706cf91b4SChen Gong out:
988f6f82851SKees Cook 	mutex_unlock(&psi->read_mutex);
989ca01d6ddSTony Luck 
990ca01d6ddSTony Luck 	if (failed)
991656de42eSKees Cook 		pr_warn("failed to create %d record(s) from '%s'\n",
992ca01d6ddSTony Luck 			failed, psi->name);
993656de42eSKees Cook 	if (!stop_loop)
994656de42eSKees Cook 		pr_err("looping? Too many records seen from '%s'\n",
995656de42eSKees Cook 			psi->name);
996ca01d6ddSTony Luck }
997ca01d6ddSTony Luck 
9986dda9266SLuck, Tony static void pstore_dowork(struct work_struct *work)
9996dda9266SLuck, Tony {
10006dda9266SLuck, Tony 	pstore_get_records(1);
10016dda9266SLuck, Tony }
10026dda9266SLuck, Tony 
100324ed960aSKees Cook static void pstore_timefunc(struct timer_list *unused)
10046dda9266SLuck, Tony {
10056dda9266SLuck, Tony 	if (pstore_new_entry) {
10066dda9266SLuck, Tony 		pstore_new_entry = 0;
10076dda9266SLuck, Tony 		schedule_work(&pstore_work);
10086dda9266SLuck, Tony 	}
10096dda9266SLuck, Tony 
10106330d553SKees Cook 	if (pstore_update_ms >= 0)
10116330d553SKees Cook 		mod_timer(&pstore_timer,
10126330d553SKees Cook 			  jiffies + msecs_to_jiffies(pstore_update_ms));
10136dda9266SLuck, Tony }
10146dda9266SLuck, Tony 
1015dee28e72SMatthew Garrett module_param(backend, charp, 0444);
1016dee28e72SMatthew Garrett MODULE_PARM_DESC(backend, "Pstore backend to use");
1017