xref: /openbmc/linux/fs/pstore/platform.c (revision 1d27e3e2252ba9d949ca82fbdb73cde102cb2067)
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
378cfc8ddcSGeliang Tang #ifdef CONFIG_PSTORE_LZ4_COMPRESS
388cfc8ddcSGeliang Tang #include <linux/lz4.h>
398cfc8ddcSGeliang Tang #endif
40ca01d6ddSTony Luck #include <linux/string.h>
416dda9266SLuck, Tony #include <linux/timer.h>
42ca01d6ddSTony Luck #include <linux/slab.h>
43ca01d6ddSTony Luck #include <linux/uaccess.h>
44abd4d558SDon Zickus #include <linux/hardirq.h>
45a3f5f075SAnton Vorontsov #include <linux/jiffies.h>
466dda9266SLuck, Tony #include <linux/workqueue.h>
47ca01d6ddSTony Luck 
48ca01d6ddSTony Luck #include "internal.h"
49ca01d6ddSTony Luck 
50ca01d6ddSTony Luck /*
516dda9266SLuck, Tony  * We defer making "oops" entries appear in pstore - see
526dda9266SLuck, Tony  * whether the system is actually still running well enough
536dda9266SLuck, Tony  * to let someone see the entry
546dda9266SLuck, Tony  */
55521f7288SAnton Vorontsov static int pstore_update_ms = -1;
56a3f5f075SAnton Vorontsov module_param_named(update_ms, pstore_update_ms, int, 0600);
57a3f5f075SAnton Vorontsov MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content "
58521f7288SAnton Vorontsov 		 "(default is -1, which means runtime updates are disabled; "
59521f7288SAnton Vorontsov 		 "enabling this option is not safe, it may lead to further "
60521f7288SAnton Vorontsov 		 "corruption on Oopses)");
616dda9266SLuck, Tony 
626dda9266SLuck, Tony static int pstore_new_entry;
636dda9266SLuck, Tony 
646dda9266SLuck, Tony static void pstore_timefunc(unsigned long);
65*1d27e3e2SKees Cook static DEFINE_TIMER(pstore_timer, pstore_timefunc);
666dda9266SLuck, Tony 
676dda9266SLuck, Tony static void pstore_dowork(struct work_struct *);
686dda9266SLuck, Tony static DECLARE_WORK(pstore_work, pstore_dowork);
696dda9266SLuck, Tony 
706dda9266SLuck, Tony /*
71ca01d6ddSTony Luck  * pstore_lock just protects "psinfo" during
72ca01d6ddSTony Luck  * calls to pstore_register()
73ca01d6ddSTony Luck  */
74ca01d6ddSTony Luck static DEFINE_SPINLOCK(pstore_lock);
75060287b8SAnton Vorontsov struct pstore_info *psinfo;
76ca01d6ddSTony Luck 
77dee28e72SMatthew Garrett static char *backend;
78dee28e72SMatthew Garrett 
79b0aad7a9SAruna Balakrishnaiah /* Compression parameters */
808cfc8ddcSGeliang Tang #ifdef CONFIG_PSTORE_ZLIB_COMPRESS
81b0aad7a9SAruna Balakrishnaiah #define COMPR_LEVEL 6
82b0aad7a9SAruna Balakrishnaiah #define WINDOW_BITS 12
83b0aad7a9SAruna Balakrishnaiah #define MEM_LEVEL 4
84b0aad7a9SAruna Balakrishnaiah static struct z_stream_s stream;
858cfc8ddcSGeliang Tang #else
868cfc8ddcSGeliang Tang static unsigned char *workspace;
878cfc8ddcSGeliang Tang #endif
888cfc8ddcSGeliang Tang 
898cfc8ddcSGeliang Tang struct pstore_zbackend {
908cfc8ddcSGeliang Tang 	int (*compress)(const void *in, void *out, size_t inlen, size_t outlen);
918cfc8ddcSGeliang Tang 	int (*decompress)(void *in, void *out, size_t inlen, size_t outlen);
928cfc8ddcSGeliang Tang 	void (*allocate)(void);
938cfc8ddcSGeliang Tang 	void (*free)(void);
948cfc8ddcSGeliang Tang 
958cfc8ddcSGeliang Tang 	const char *name;
968cfc8ddcSGeliang Tang };
97b0aad7a9SAruna Balakrishnaiah 
98b0aad7a9SAruna Balakrishnaiah static char *big_oops_buf;
99b0aad7a9SAruna Balakrishnaiah static size_t big_oops_buf_sz;
100b0aad7a9SAruna Balakrishnaiah 
101366f7e7aSLuck, Tony /* How much of the console log to snapshot */
102349d7438SDavid Howells unsigned long kmsg_bytes = PSTORE_DEFAULT_KMSG_BYTES;
103ca01d6ddSTony Luck 
104366f7e7aSLuck, Tony void pstore_set_kmsg_bytes(int bytes)
105ca01d6ddSTony Luck {
106366f7e7aSLuck, Tony 	kmsg_bytes = bytes;
107ca01d6ddSTony Luck }
108ca01d6ddSTony Luck 
109ca01d6ddSTony Luck /* Tag each group of saved records with a sequence number */
110ca01d6ddSTony Luck static int	oopscount;
111ca01d6ddSTony Luck 
112381b872cSSeiji Aguchi static const char *get_reason_str(enum kmsg_dump_reason reason)
113381b872cSSeiji Aguchi {
114381b872cSSeiji Aguchi 	switch (reason) {
115381b872cSSeiji Aguchi 	case KMSG_DUMP_PANIC:
116381b872cSSeiji Aguchi 		return "Panic";
117381b872cSSeiji Aguchi 	case KMSG_DUMP_OOPS:
118381b872cSSeiji Aguchi 		return "Oops";
119381b872cSSeiji Aguchi 	case KMSG_DUMP_EMERG:
120381b872cSSeiji Aguchi 		return "Emergency";
121381b872cSSeiji Aguchi 	case KMSG_DUMP_RESTART:
122381b872cSSeiji Aguchi 		return "Restart";
123381b872cSSeiji Aguchi 	case KMSG_DUMP_HALT:
124381b872cSSeiji Aguchi 		return "Halt";
125381b872cSSeiji Aguchi 	case KMSG_DUMP_POWEROFF:
126381b872cSSeiji Aguchi 		return "Poweroff";
127381b872cSSeiji Aguchi 	default:
128381b872cSSeiji Aguchi 		return "Unknown";
129381b872cSSeiji Aguchi 	}
130381b872cSSeiji Aguchi }
1319f6af27fSTony Luck 
1329f244e9cSSeiji Aguchi bool pstore_cannot_block_path(enum kmsg_dump_reason reason)
1339f244e9cSSeiji Aguchi {
1349f244e9cSSeiji Aguchi 	/*
1359f244e9cSSeiji Aguchi 	 * In case of NMI path, pstore shouldn't be blocked
1369f244e9cSSeiji Aguchi 	 * regardless of reason.
1379f244e9cSSeiji Aguchi 	 */
1389f244e9cSSeiji Aguchi 	if (in_nmi())
1399f244e9cSSeiji Aguchi 		return true;
1409f244e9cSSeiji Aguchi 
1419f244e9cSSeiji Aguchi 	switch (reason) {
1429f244e9cSSeiji Aguchi 	/* In panic case, other cpus are stopped by smp_send_stop(). */
1439f244e9cSSeiji Aguchi 	case KMSG_DUMP_PANIC:
1449f244e9cSSeiji Aguchi 	/* Emergency restart shouldn't be blocked by spin lock. */
1459f244e9cSSeiji Aguchi 	case KMSG_DUMP_EMERG:
1469f244e9cSSeiji Aguchi 		return true;
1479f244e9cSSeiji Aguchi 	default:
1489f244e9cSSeiji Aguchi 		return false;
1499f244e9cSSeiji Aguchi 	}
1509f244e9cSSeiji Aguchi }
1519f244e9cSSeiji Aguchi EXPORT_SYMBOL_GPL(pstore_cannot_block_path);
1529f244e9cSSeiji Aguchi 
1538cfc8ddcSGeliang Tang #ifdef CONFIG_PSTORE_ZLIB_COMPRESS
154b0aad7a9SAruna Balakrishnaiah /* Derived from logfs_compress() */
1558cfc8ddcSGeliang Tang static int compress_zlib(const void *in, void *out, size_t inlen, size_t outlen)
156b0aad7a9SAruna Balakrishnaiah {
157b0aad7a9SAruna Balakrishnaiah 	int err, ret;
158b0aad7a9SAruna Balakrishnaiah 
159b0aad7a9SAruna Balakrishnaiah 	ret = -EIO;
160b0aad7a9SAruna Balakrishnaiah 	err = zlib_deflateInit2(&stream, COMPR_LEVEL, Z_DEFLATED, WINDOW_BITS,
161b0aad7a9SAruna Balakrishnaiah 						MEM_LEVEL, Z_DEFAULT_STRATEGY);
162b0aad7a9SAruna Balakrishnaiah 	if (err != Z_OK)
163b0aad7a9SAruna Balakrishnaiah 		goto error;
164b0aad7a9SAruna Balakrishnaiah 
165b0aad7a9SAruna Balakrishnaiah 	stream.next_in = in;
166b0aad7a9SAruna Balakrishnaiah 	stream.avail_in = inlen;
167b0aad7a9SAruna Balakrishnaiah 	stream.total_in = 0;
168b0aad7a9SAruna Balakrishnaiah 	stream.next_out = out;
169b0aad7a9SAruna Balakrishnaiah 	stream.avail_out = outlen;
170b0aad7a9SAruna Balakrishnaiah 	stream.total_out = 0;
171b0aad7a9SAruna Balakrishnaiah 
172b0aad7a9SAruna Balakrishnaiah 	err = zlib_deflate(&stream, Z_FINISH);
173b0aad7a9SAruna Balakrishnaiah 	if (err != Z_STREAM_END)
174b0aad7a9SAruna Balakrishnaiah 		goto error;
175b0aad7a9SAruna Balakrishnaiah 
176b0aad7a9SAruna Balakrishnaiah 	err = zlib_deflateEnd(&stream);
177b0aad7a9SAruna Balakrishnaiah 	if (err != Z_OK)
178b0aad7a9SAruna Balakrishnaiah 		goto error;
179b0aad7a9SAruna Balakrishnaiah 
180b0aad7a9SAruna Balakrishnaiah 	if (stream.total_out >= stream.total_in)
181b0aad7a9SAruna Balakrishnaiah 		goto error;
182b0aad7a9SAruna Balakrishnaiah 
183b0aad7a9SAruna Balakrishnaiah 	ret = stream.total_out;
184b0aad7a9SAruna Balakrishnaiah error:
185b0aad7a9SAruna Balakrishnaiah 	return ret;
186b0aad7a9SAruna Balakrishnaiah }
187b0aad7a9SAruna Balakrishnaiah 
188adb42f5eSAruna Balakrishnaiah /* Derived from logfs_uncompress */
1898cfc8ddcSGeliang Tang static int decompress_zlib(void *in, void *out, size_t inlen, size_t outlen)
190adb42f5eSAruna Balakrishnaiah {
191adb42f5eSAruna Balakrishnaiah 	int err, ret;
192adb42f5eSAruna Balakrishnaiah 
193adb42f5eSAruna Balakrishnaiah 	ret = -EIO;
194b61edf8eSAruna Balakrishnaiah 	err = zlib_inflateInit2(&stream, WINDOW_BITS);
195adb42f5eSAruna Balakrishnaiah 	if (err != Z_OK)
196adb42f5eSAruna Balakrishnaiah 		goto error;
197adb42f5eSAruna Balakrishnaiah 
198adb42f5eSAruna Balakrishnaiah 	stream.next_in = in;
199adb42f5eSAruna Balakrishnaiah 	stream.avail_in = inlen;
200adb42f5eSAruna Balakrishnaiah 	stream.total_in = 0;
201adb42f5eSAruna Balakrishnaiah 	stream.next_out = out;
202adb42f5eSAruna Balakrishnaiah 	stream.avail_out = outlen;
203adb42f5eSAruna Balakrishnaiah 	stream.total_out = 0;
204adb42f5eSAruna Balakrishnaiah 
205adb42f5eSAruna Balakrishnaiah 	err = zlib_inflate(&stream, Z_FINISH);
206adb42f5eSAruna Balakrishnaiah 	if (err != Z_STREAM_END)
207adb42f5eSAruna Balakrishnaiah 		goto error;
208adb42f5eSAruna Balakrishnaiah 
209adb42f5eSAruna Balakrishnaiah 	err = zlib_inflateEnd(&stream);
210adb42f5eSAruna Balakrishnaiah 	if (err != Z_OK)
211adb42f5eSAruna Balakrishnaiah 		goto error;
212adb42f5eSAruna Balakrishnaiah 
213adb42f5eSAruna Balakrishnaiah 	ret = stream.total_out;
214adb42f5eSAruna Balakrishnaiah error:
215adb42f5eSAruna Balakrishnaiah 	return ret;
216adb42f5eSAruna Balakrishnaiah }
217adb42f5eSAruna Balakrishnaiah 
2188cfc8ddcSGeliang Tang static void allocate_zlib(void)
219b0aad7a9SAruna Balakrishnaiah {
220b0aad7a9SAruna Balakrishnaiah 	size_t size;
2217de8fe2fSAruna Balakrishnaiah 	size_t cmpr;
222b0aad7a9SAruna Balakrishnaiah 
2237de8fe2fSAruna Balakrishnaiah 	switch (psinfo->bufsize) {
2247de8fe2fSAruna Balakrishnaiah 	/* buffer range for efivars */
2257de8fe2fSAruna Balakrishnaiah 	case 1000 ... 2000:
2267de8fe2fSAruna Balakrishnaiah 		cmpr = 56;
2277de8fe2fSAruna Balakrishnaiah 		break;
2287de8fe2fSAruna Balakrishnaiah 	case 2001 ... 3000:
2297de8fe2fSAruna Balakrishnaiah 		cmpr = 54;
2307de8fe2fSAruna Balakrishnaiah 		break;
2317de8fe2fSAruna Balakrishnaiah 	case 3001 ... 3999:
2327de8fe2fSAruna Balakrishnaiah 		cmpr = 52;
2337de8fe2fSAruna Balakrishnaiah 		break;
2347de8fe2fSAruna Balakrishnaiah 	/* buffer range for nvram, erst */
2357de8fe2fSAruna Balakrishnaiah 	case 4000 ... 10000:
2367de8fe2fSAruna Balakrishnaiah 		cmpr = 45;
2377de8fe2fSAruna Balakrishnaiah 		break;
2387de8fe2fSAruna Balakrishnaiah 	default:
2397de8fe2fSAruna Balakrishnaiah 		cmpr = 60;
2407de8fe2fSAruna Balakrishnaiah 		break;
2417de8fe2fSAruna Balakrishnaiah 	}
2427de8fe2fSAruna Balakrishnaiah 
2437de8fe2fSAruna Balakrishnaiah 	big_oops_buf_sz = (psinfo->bufsize * 100) / cmpr;
244b0aad7a9SAruna Balakrishnaiah 	big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
245b0aad7a9SAruna Balakrishnaiah 	if (big_oops_buf) {
246b0aad7a9SAruna Balakrishnaiah 		size = max(zlib_deflate_workspacesize(WINDOW_BITS, MEM_LEVEL),
247b0aad7a9SAruna Balakrishnaiah 			zlib_inflate_workspacesize());
248b0aad7a9SAruna Balakrishnaiah 		stream.workspace = kmalloc(size, GFP_KERNEL);
249b0aad7a9SAruna Balakrishnaiah 		if (!stream.workspace) {
250ef748853SFabian Frederick 			pr_err("No memory for compression workspace; skipping compression\n");
251b0aad7a9SAruna Balakrishnaiah 			kfree(big_oops_buf);
252b0aad7a9SAruna Balakrishnaiah 			big_oops_buf = NULL;
253b0aad7a9SAruna Balakrishnaiah 		}
254b0aad7a9SAruna Balakrishnaiah 	} else {
255ef748853SFabian Frederick 		pr_err("No memory for uncompressed data; skipping compression\n");
256b0aad7a9SAruna Balakrishnaiah 		stream.workspace = NULL;
257b0aad7a9SAruna Balakrishnaiah 	}
258b0aad7a9SAruna Balakrishnaiah 
259b0aad7a9SAruna Balakrishnaiah }
260b0aad7a9SAruna Balakrishnaiah 
2618cfc8ddcSGeliang Tang static void free_zlib(void)
262ee1d2674SGeliang Tang {
263ee1d2674SGeliang Tang 	kfree(stream.workspace);
264ee1d2674SGeliang Tang 	stream.workspace = NULL;
265ee1d2674SGeliang Tang 	kfree(big_oops_buf);
266ee1d2674SGeliang Tang 	big_oops_buf = NULL;
2678cfc8ddcSGeliang Tang 	big_oops_buf_sz = 0;
2688cfc8ddcSGeliang Tang }
2698cfc8ddcSGeliang Tang 
2703faf9354SBhumika Goyal static const struct pstore_zbackend backend_zlib = {
2718cfc8ddcSGeliang Tang 	.compress	= compress_zlib,
2728cfc8ddcSGeliang Tang 	.decompress	= decompress_zlib,
2738cfc8ddcSGeliang Tang 	.allocate	= allocate_zlib,
2748cfc8ddcSGeliang Tang 	.free		= free_zlib,
2758cfc8ddcSGeliang Tang 	.name		= "zlib",
2768cfc8ddcSGeliang Tang };
2778cfc8ddcSGeliang Tang #endif
2788cfc8ddcSGeliang Tang 
2798cfc8ddcSGeliang Tang #ifdef CONFIG_PSTORE_LZO_COMPRESS
2808cfc8ddcSGeliang Tang static int compress_lzo(const void *in, void *out, size_t inlen, size_t outlen)
2818cfc8ddcSGeliang Tang {
2828cfc8ddcSGeliang Tang 	int ret;
2838cfc8ddcSGeliang Tang 
2848cfc8ddcSGeliang Tang 	ret = lzo1x_1_compress(in, inlen, out, &outlen, workspace);
2858cfc8ddcSGeliang Tang 	if (ret != LZO_E_OK) {
2868cfc8ddcSGeliang Tang 		pr_err("lzo_compress error, ret = %d!\n", ret);
2878cfc8ddcSGeliang Tang 		return -EIO;
2888cfc8ddcSGeliang Tang 	}
2898cfc8ddcSGeliang Tang 
2908cfc8ddcSGeliang Tang 	return outlen;
2918cfc8ddcSGeliang Tang }
2928cfc8ddcSGeliang Tang 
2938cfc8ddcSGeliang Tang static int decompress_lzo(void *in, void *out, size_t inlen, size_t outlen)
2948cfc8ddcSGeliang Tang {
2958cfc8ddcSGeliang Tang 	int ret;
2968cfc8ddcSGeliang Tang 
2978cfc8ddcSGeliang Tang 	ret = lzo1x_decompress_safe(in, inlen, out, &outlen);
2988cfc8ddcSGeliang Tang 	if (ret != LZO_E_OK) {
2998cfc8ddcSGeliang Tang 		pr_err("lzo_decompress error, ret = %d!\n", ret);
3008cfc8ddcSGeliang Tang 		return -EIO;
3018cfc8ddcSGeliang Tang 	}
3028cfc8ddcSGeliang Tang 
3038cfc8ddcSGeliang Tang 	return outlen;
3048cfc8ddcSGeliang Tang }
3058cfc8ddcSGeliang Tang 
3068cfc8ddcSGeliang Tang static void allocate_lzo(void)
3078cfc8ddcSGeliang Tang {
3088cfc8ddcSGeliang Tang 	big_oops_buf_sz = lzo1x_worst_compress(psinfo->bufsize);
3098cfc8ddcSGeliang Tang 	big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
3108cfc8ddcSGeliang Tang 	if (big_oops_buf) {
3118cfc8ddcSGeliang Tang 		workspace = kmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
3128cfc8ddcSGeliang Tang 		if (!workspace) {
3138cfc8ddcSGeliang Tang 			pr_err("No memory for compression workspace; skipping compression\n");
3148cfc8ddcSGeliang Tang 			kfree(big_oops_buf);
3158cfc8ddcSGeliang Tang 			big_oops_buf = NULL;
3168cfc8ddcSGeliang Tang 		}
3178cfc8ddcSGeliang Tang 	} else {
3188cfc8ddcSGeliang Tang 		pr_err("No memory for uncompressed data; skipping compression\n");
3198cfc8ddcSGeliang Tang 		workspace = NULL;
3208cfc8ddcSGeliang Tang 	}
3218cfc8ddcSGeliang Tang }
3228cfc8ddcSGeliang Tang 
3238cfc8ddcSGeliang Tang static void free_lzo(void)
3248cfc8ddcSGeliang Tang {
3258cfc8ddcSGeliang Tang 	kfree(workspace);
3268cfc8ddcSGeliang Tang 	kfree(big_oops_buf);
3278cfc8ddcSGeliang Tang 	big_oops_buf = NULL;
3288cfc8ddcSGeliang Tang 	big_oops_buf_sz = 0;
3298cfc8ddcSGeliang Tang }
3308cfc8ddcSGeliang Tang 
3313faf9354SBhumika Goyal static const struct pstore_zbackend backend_lzo = {
3328cfc8ddcSGeliang Tang 	.compress	= compress_lzo,
3338cfc8ddcSGeliang Tang 	.decompress	= decompress_lzo,
3348cfc8ddcSGeliang Tang 	.allocate	= allocate_lzo,
3358cfc8ddcSGeliang Tang 	.free		= free_lzo,
3368cfc8ddcSGeliang Tang 	.name		= "lzo",
3378cfc8ddcSGeliang Tang };
3388cfc8ddcSGeliang Tang #endif
3398cfc8ddcSGeliang Tang 
3408cfc8ddcSGeliang Tang #ifdef CONFIG_PSTORE_LZ4_COMPRESS
3418cfc8ddcSGeliang Tang static int compress_lz4(const void *in, void *out, size_t inlen, size_t outlen)
3428cfc8ddcSGeliang Tang {
3438cfc8ddcSGeliang Tang 	int ret;
3448cfc8ddcSGeliang Tang 
345d21b5ff1SSven Schmidt 	ret = LZ4_compress_default(in, out, inlen, outlen, workspace);
346d21b5ff1SSven Schmidt 	if (!ret) {
347d21b5ff1SSven Schmidt 		pr_err("LZ4_compress_default error; compression failed!\n");
3488cfc8ddcSGeliang Tang 		return -EIO;
3498cfc8ddcSGeliang Tang 	}
3508cfc8ddcSGeliang Tang 
351d21b5ff1SSven Schmidt 	return ret;
3528cfc8ddcSGeliang Tang }
3538cfc8ddcSGeliang Tang 
3548cfc8ddcSGeliang Tang static int decompress_lz4(void *in, void *out, size_t inlen, size_t outlen)
3558cfc8ddcSGeliang Tang {
3568cfc8ddcSGeliang Tang 	int ret;
3578cfc8ddcSGeliang Tang 
358d21b5ff1SSven Schmidt 	ret = LZ4_decompress_safe(in, out, inlen, outlen);
359d21b5ff1SSven Schmidt 	if (ret < 0) {
360d21b5ff1SSven Schmidt 		/*
361d21b5ff1SSven Schmidt 		 * LZ4_decompress_safe will return an error code
362d21b5ff1SSven Schmidt 		 * (< 0) if decompression failed
363d21b5ff1SSven Schmidt 		 */
364d21b5ff1SSven Schmidt 		pr_err("LZ4_decompress_safe error, ret = %d!\n", ret);
3658cfc8ddcSGeliang Tang 		return -EIO;
3668cfc8ddcSGeliang Tang 	}
3678cfc8ddcSGeliang Tang 
368d21b5ff1SSven Schmidt 	return ret;
3698cfc8ddcSGeliang Tang }
3708cfc8ddcSGeliang Tang 
3718cfc8ddcSGeliang Tang static void allocate_lz4(void)
3728cfc8ddcSGeliang Tang {
373d21b5ff1SSven Schmidt 	big_oops_buf_sz = LZ4_compressBound(psinfo->bufsize);
3748cfc8ddcSGeliang Tang 	big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
3758cfc8ddcSGeliang Tang 	if (big_oops_buf) {
3768cfc8ddcSGeliang Tang 		workspace = kmalloc(LZ4_MEM_COMPRESS, GFP_KERNEL);
3778cfc8ddcSGeliang Tang 		if (!workspace) {
3788cfc8ddcSGeliang Tang 			pr_err("No memory for compression workspace; skipping compression\n");
3798cfc8ddcSGeliang Tang 			kfree(big_oops_buf);
3808cfc8ddcSGeliang Tang 			big_oops_buf = NULL;
3818cfc8ddcSGeliang Tang 		}
3828cfc8ddcSGeliang Tang 	} else {
3838cfc8ddcSGeliang Tang 		pr_err("No memory for uncompressed data; skipping compression\n");
3848cfc8ddcSGeliang Tang 		workspace = NULL;
3858cfc8ddcSGeliang Tang 	}
3868cfc8ddcSGeliang Tang }
3878cfc8ddcSGeliang Tang 
3888cfc8ddcSGeliang Tang static void free_lz4(void)
3898cfc8ddcSGeliang Tang {
3908cfc8ddcSGeliang Tang 	kfree(workspace);
3918cfc8ddcSGeliang Tang 	kfree(big_oops_buf);
3928cfc8ddcSGeliang Tang 	big_oops_buf = NULL;
3938cfc8ddcSGeliang Tang 	big_oops_buf_sz = 0;
3948cfc8ddcSGeliang Tang }
3958cfc8ddcSGeliang Tang 
3963faf9354SBhumika Goyal static const struct pstore_zbackend backend_lz4 = {
3978cfc8ddcSGeliang Tang 	.compress	= compress_lz4,
3988cfc8ddcSGeliang Tang 	.decompress	= decompress_lz4,
3998cfc8ddcSGeliang Tang 	.allocate	= allocate_lz4,
4008cfc8ddcSGeliang Tang 	.free		= free_lz4,
4018cfc8ddcSGeliang Tang 	.name		= "lz4",
4028cfc8ddcSGeliang Tang };
4038cfc8ddcSGeliang Tang #endif
4048cfc8ddcSGeliang Tang 
4053faf9354SBhumika Goyal static const struct pstore_zbackend *zbackend =
4068cfc8ddcSGeliang Tang #if defined(CONFIG_PSTORE_ZLIB_COMPRESS)
4078cfc8ddcSGeliang Tang 	&backend_zlib;
4088cfc8ddcSGeliang Tang #elif defined(CONFIG_PSTORE_LZO_COMPRESS)
4098cfc8ddcSGeliang Tang 	&backend_lzo;
4108cfc8ddcSGeliang Tang #elif defined(CONFIG_PSTORE_LZ4_COMPRESS)
4118cfc8ddcSGeliang Tang 	&backend_lz4;
4128cfc8ddcSGeliang Tang #else
4138cfc8ddcSGeliang Tang 	NULL;
4148cfc8ddcSGeliang Tang #endif
4158cfc8ddcSGeliang Tang 
4168cfc8ddcSGeliang Tang static int pstore_compress(const void *in, void *out,
4178cfc8ddcSGeliang Tang 			   size_t inlen, size_t outlen)
4188cfc8ddcSGeliang Tang {
4198cfc8ddcSGeliang Tang 	if (zbackend)
4208cfc8ddcSGeliang Tang 		return zbackend->compress(in, out, inlen, outlen);
4218cfc8ddcSGeliang Tang 	else
4228cfc8ddcSGeliang Tang 		return -EIO;
4238cfc8ddcSGeliang Tang }
4248cfc8ddcSGeliang Tang 
4258cfc8ddcSGeliang Tang static int pstore_decompress(void *in, void *out, size_t inlen, size_t outlen)
4268cfc8ddcSGeliang Tang {
4278cfc8ddcSGeliang Tang 	if (zbackend)
4288cfc8ddcSGeliang Tang 		return zbackend->decompress(in, out, inlen, outlen);
4298cfc8ddcSGeliang Tang 	else
4308cfc8ddcSGeliang Tang 		return -EIO;
4318cfc8ddcSGeliang Tang }
4328cfc8ddcSGeliang Tang 
4338cfc8ddcSGeliang Tang static void allocate_buf_for_compression(void)
4348cfc8ddcSGeliang Tang {
4358cfc8ddcSGeliang Tang 	if (zbackend) {
4368cfc8ddcSGeliang Tang 		pr_info("using %s compression\n", zbackend->name);
4378cfc8ddcSGeliang Tang 		zbackend->allocate();
4388cfc8ddcSGeliang Tang 	} else {
4398cfc8ddcSGeliang Tang 		pr_err("allocate compression buffer error!\n");
4408cfc8ddcSGeliang Tang 	}
4418cfc8ddcSGeliang Tang }
4428cfc8ddcSGeliang Tang 
4438cfc8ddcSGeliang Tang static void free_buf_for_compression(void)
4448cfc8ddcSGeliang Tang {
4458cfc8ddcSGeliang Tang 	if (zbackend)
4468cfc8ddcSGeliang Tang 		zbackend->free();
4478cfc8ddcSGeliang Tang 	else
4488cfc8ddcSGeliang Tang 		pr_err("free compression buffer error!\n");
449ee1d2674SGeliang Tang }
450ee1d2674SGeliang Tang 
451b0aad7a9SAruna Balakrishnaiah /*
452b0aad7a9SAruna Balakrishnaiah  * Called when compression fails, since the printk buffer
453b0aad7a9SAruna Balakrishnaiah  * would be fetched for compression calling it again when
454b0aad7a9SAruna Balakrishnaiah  * compression fails would have moved the iterator of
455b0aad7a9SAruna Balakrishnaiah  * printk buffer which results in fetching old contents.
456b0aad7a9SAruna Balakrishnaiah  * Copy the recent messages from big_oops_buf to psinfo->buf
457b0aad7a9SAruna Balakrishnaiah  */
458b0aad7a9SAruna Balakrishnaiah static size_t copy_kmsg_to_buffer(int hsize, size_t len)
459b0aad7a9SAruna Balakrishnaiah {
460b0aad7a9SAruna Balakrishnaiah 	size_t total_len;
461b0aad7a9SAruna Balakrishnaiah 	size_t diff;
462b0aad7a9SAruna Balakrishnaiah 
463b0aad7a9SAruna Balakrishnaiah 	total_len = hsize + len;
464b0aad7a9SAruna Balakrishnaiah 
465b0aad7a9SAruna Balakrishnaiah 	if (total_len > psinfo->bufsize) {
466b0aad7a9SAruna Balakrishnaiah 		diff = total_len - psinfo->bufsize + hsize;
467b0aad7a9SAruna Balakrishnaiah 		memcpy(psinfo->buf, big_oops_buf, hsize);
468b0aad7a9SAruna Balakrishnaiah 		memcpy(psinfo->buf + hsize, big_oops_buf + diff,
469b0aad7a9SAruna Balakrishnaiah 					psinfo->bufsize - hsize);
470b0aad7a9SAruna Balakrishnaiah 		total_len = psinfo->bufsize;
471b0aad7a9SAruna Balakrishnaiah 	} else
472b0aad7a9SAruna Balakrishnaiah 		memcpy(psinfo->buf, big_oops_buf, total_len);
473b0aad7a9SAruna Balakrishnaiah 
474b0aad7a9SAruna Balakrishnaiah 	return total_len;
475b0aad7a9SAruna Balakrishnaiah }
476b0aad7a9SAruna Balakrishnaiah 
477e581ca81SKees Cook void pstore_record_init(struct pstore_record *record,
478e581ca81SKees Cook 			struct pstore_info *psinfo)
479e581ca81SKees Cook {
480e581ca81SKees Cook 	memset(record, 0, sizeof(*record));
481e581ca81SKees Cook 
482e581ca81SKees Cook 	record->psi = psinfo;
483c7f3c595SKees Cook 
484c7f3c595SKees Cook 	/* Report zeroed timestamp if called before timekeeping has resumed. */
485c7f3c595SKees Cook 	if (__getnstimeofday(&record->time)) {
486c7f3c595SKees Cook 		record->time.tv_sec = 0;
487c7f3c595SKees Cook 		record->time.tv_nsec = 0;
488c7f3c595SKees Cook 	}
489e581ca81SKees Cook }
490e581ca81SKees Cook 
491ca01d6ddSTony Luck /*
492ca01d6ddSTony Luck  * callback from kmsg_dump. (s2,l2) has the most recently
493ca01d6ddSTony Luck  * written bytes, older bytes are in (s1,l1). Save as much
494ca01d6ddSTony Luck  * as we can from the end of the buffer.
495ca01d6ddSTony Luck  */
496ca01d6ddSTony Luck static void pstore_dump(struct kmsg_dumper *dumper,
497e2ae715dSKay Sievers 			enum kmsg_dump_reason reason)
498ca01d6ddSTony Luck {
499e2ae715dSKay Sievers 	unsigned long	total = 0;
500381b872cSSeiji Aguchi 	const char	*why;
501b94fdd07SMatthew Garrett 	unsigned int	part = 1;
502abd4d558SDon Zickus 	unsigned long	flags = 0;
50398e44fdaSNamhyung Kim 	int		is_locked;
504e2ae715dSKay Sievers 	int		ret;
505ca01d6ddSTony Luck 
506381b872cSSeiji Aguchi 	why = get_reason_str(reason);
5079f6af27fSTony Luck 
5089f244e9cSSeiji Aguchi 	if (pstore_cannot_block_path(reason)) {
5099f244e9cSSeiji Aguchi 		is_locked = spin_trylock_irqsave(&psinfo->buf_lock, flags);
5109f244e9cSSeiji Aguchi 		if (!is_locked) {
5119f244e9cSSeiji Aguchi 			pr_err("pstore dump routine blocked in %s path, may corrupt error record\n"
5129f244e9cSSeiji Aguchi 				       , in_nmi() ? "NMI" : why);
513959217c8SLi Pengcheng 			return;
5149f244e9cSSeiji Aguchi 		}
51598e44fdaSNamhyung Kim 	} else {
516abd4d558SDon Zickus 		spin_lock_irqsave(&psinfo->buf_lock, flags);
51798e44fdaSNamhyung Kim 		is_locked = 1;
51898e44fdaSNamhyung Kim 	}
519ca01d6ddSTony Luck 	oopscount++;
520ca01d6ddSTony Luck 	while (total < kmsg_bytes) {
521e2ae715dSKay Sievers 		char *dst;
52276cc9580SKees Cook 		size_t dst_size;
52376cc9580SKees Cook 		int header_size;
524b0aad7a9SAruna Balakrishnaiah 		int zipped_len = -1;
52576cc9580SKees Cook 		size_t dump_size;
526e581ca81SKees Cook 		struct pstore_record record;
527e581ca81SKees Cook 
528e581ca81SKees Cook 		pstore_record_init(&record, psinfo);
529e581ca81SKees Cook 		record.type = PSTORE_TYPE_DMESG;
530e581ca81SKees Cook 		record.count = oopscount;
531e581ca81SKees Cook 		record.reason = reason;
532e581ca81SKees Cook 		record.part = part;
533e581ca81SKees Cook 		record.buf = psinfo->buf;
534e2ae715dSKay Sievers 
535f0e2efcfSKonstantin Khlebnikov 		if (big_oops_buf && is_locked) {
536b0aad7a9SAruna Balakrishnaiah 			dst = big_oops_buf;
53776cc9580SKees Cook 			dst_size = big_oops_buf_sz;
538235f6d15SNamhyung Kim 		} else {
539235f6d15SNamhyung Kim 			dst = psinfo->buf;
54076cc9580SKees Cook 			dst_size = psinfo->bufsize;
541235f6d15SNamhyung Kim 		}
542235f6d15SNamhyung Kim 
54376cc9580SKees Cook 		/* Write dump header. */
54476cc9580SKees Cook 		header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why,
54576cc9580SKees Cook 				 oopscount, part);
54676cc9580SKees Cook 		dst_size -= header_size;
547b0aad7a9SAruna Balakrishnaiah 
54876cc9580SKees Cook 		/* Write dump contents. */
54976cc9580SKees Cook 		if (!kmsg_dump_get_buffer(dumper, true, dst + header_size,
55076cc9580SKees Cook 					  dst_size, &dump_size))
551b0aad7a9SAruna Balakrishnaiah 			break;
552b0aad7a9SAruna Balakrishnaiah 
553235f6d15SNamhyung Kim 		if (big_oops_buf && is_locked) {
554b0aad7a9SAruna Balakrishnaiah 			zipped_len = pstore_compress(dst, psinfo->buf,
55576cc9580SKees Cook 						header_size + dump_size,
55676cc9580SKees Cook 						psinfo->bufsize);
557b0aad7a9SAruna Balakrishnaiah 
558b0aad7a9SAruna Balakrishnaiah 			if (zipped_len > 0) {
55976cc9580SKees Cook 				record.compressed = true;
56076cc9580SKees Cook 				record.size = zipped_len;
561b0aad7a9SAruna Balakrishnaiah 			} else {
56276cc9580SKees Cook 				record.size = copy_kmsg_to_buffer(header_size,
56376cc9580SKees Cook 								  dump_size);
564b0aad7a9SAruna Balakrishnaiah 			}
565b0aad7a9SAruna Balakrishnaiah 		} else {
56676cc9580SKees Cook 			record.size = header_size + dump_size;
567b0aad7a9SAruna Balakrishnaiah 		}
568b0aad7a9SAruna Balakrishnaiah 
56976cc9580SKees Cook 		ret = psinfo->write(&record);
570b238b8faSChen Gong 		if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted())
5716dda9266SLuck, Tony 			pstore_new_entry = 1;
572e2ae715dSKay Sievers 
57376cc9580SKees Cook 		total += record.size;
57456280682SMatthew Garrett 		part++;
575ca01d6ddSTony Luck 	}
576abd4d558SDon Zickus 	if (is_locked)
5779f244e9cSSeiji Aguchi 		spin_unlock_irqrestore(&psinfo->buf_lock, flags);
578ca01d6ddSTony Luck }
579ca01d6ddSTony Luck 
580ca01d6ddSTony Luck static struct kmsg_dumper pstore_dumper = {
581ca01d6ddSTony Luck 	.dump = pstore_dump,
582ca01d6ddSTony Luck };
583ca01d6ddSTony Luck 
584306e5c2aSGeliang Tang /*
585306e5c2aSGeliang Tang  * Register with kmsg_dump to save last part of console log on panic.
586306e5c2aSGeliang Tang  */
58718730411SGeliang Tang static void pstore_register_kmsg(void)
58818730411SGeliang Tang {
58918730411SGeliang Tang 	kmsg_dump_register(&pstore_dumper);
59018730411SGeliang Tang }
59118730411SGeliang Tang 
592ee1d2674SGeliang Tang static void pstore_unregister_kmsg(void)
593ee1d2674SGeliang Tang {
594ee1d2674SGeliang Tang 	kmsg_dump_unregister(&pstore_dumper);
595ee1d2674SGeliang Tang }
596ee1d2674SGeliang Tang 
597f29e5956SAnton Vorontsov #ifdef CONFIG_PSTORE_CONSOLE
598f29e5956SAnton Vorontsov static void pstore_console_write(struct console *con, const char *s, unsigned c)
599f29e5956SAnton Vorontsov {
600f29e5956SAnton Vorontsov 	const char *e = s + c;
601f29e5956SAnton Vorontsov 
602f29e5956SAnton Vorontsov 	while (s < e) {
603e581ca81SKees Cook 		struct pstore_record record;
604f29e5956SAnton Vorontsov 		unsigned long flags;
605f29e5956SAnton Vorontsov 
606e581ca81SKees Cook 		pstore_record_init(&record, psinfo);
607e581ca81SKees Cook 		record.type = PSTORE_TYPE_CONSOLE;
608e581ca81SKees Cook 
609f29e5956SAnton Vorontsov 		if (c > psinfo->bufsize)
610f29e5956SAnton Vorontsov 			c = psinfo->bufsize;
61180c9d03cSChuansheng Liu 
61280c9d03cSChuansheng Liu 		if (oops_in_progress) {
61380c9d03cSChuansheng Liu 			if (!spin_trylock_irqsave(&psinfo->buf_lock, flags))
61480c9d03cSChuansheng Liu 				break;
61580c9d03cSChuansheng Liu 		} else {
616f29e5956SAnton Vorontsov 			spin_lock_irqsave(&psinfo->buf_lock, flags);
61780c9d03cSChuansheng Liu 		}
618b10b4711SKees Cook 		record.buf = (char *)s;
619b10b4711SKees Cook 		record.size = c;
6204c9ec219SKees Cook 		psinfo->write(&record);
621f29e5956SAnton Vorontsov 		spin_unlock_irqrestore(&psinfo->buf_lock, flags);
622f29e5956SAnton Vorontsov 		s += c;
623f29e5956SAnton Vorontsov 		c = e - s;
624f29e5956SAnton Vorontsov 	}
625f29e5956SAnton Vorontsov }
626f29e5956SAnton Vorontsov 
627f29e5956SAnton Vorontsov static struct console pstore_console = {
628f29e5956SAnton Vorontsov 	.name	= "pstore",
629f29e5956SAnton Vorontsov 	.write	= pstore_console_write,
630f29e5956SAnton Vorontsov 	.flags	= CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME,
631f29e5956SAnton Vorontsov 	.index	= -1,
632f29e5956SAnton Vorontsov };
633f29e5956SAnton Vorontsov 
634f29e5956SAnton Vorontsov static void pstore_register_console(void)
635f29e5956SAnton Vorontsov {
636f29e5956SAnton Vorontsov 	register_console(&pstore_console);
637f29e5956SAnton Vorontsov }
638ee1d2674SGeliang Tang 
639ee1d2674SGeliang Tang static void pstore_unregister_console(void)
640ee1d2674SGeliang Tang {
641ee1d2674SGeliang Tang 	unregister_console(&pstore_console);
642ee1d2674SGeliang Tang }
643f29e5956SAnton Vorontsov #else
644f29e5956SAnton Vorontsov static void pstore_register_console(void) {}
645ee1d2674SGeliang Tang static void pstore_unregister_console(void) {}
646f29e5956SAnton Vorontsov #endif
647f29e5956SAnton Vorontsov 
6484c9ec219SKees Cook static int pstore_write_user_compat(struct pstore_record *record,
649fdd03118SKees Cook 				    const char __user *buf)
6505bf6d1b9SMark Salyzyn {
65130800d99SKees Cook 	int ret = 0;
6525bf6d1b9SMark Salyzyn 
65330800d99SKees Cook 	if (record->buf)
65430800d99SKees Cook 		return -EINVAL;
6555bf6d1b9SMark Salyzyn 
656077090afSGeliang Tang 	record->buf = memdup_user(buf, record->size);
657077090afSGeliang Tang 	if (unlikely(IS_ERR(record->buf))) {
658077090afSGeliang Tang 		ret = PTR_ERR(record->buf);
65930800d99SKees Cook 		goto out;
6605bf6d1b9SMark Salyzyn 	}
66130800d99SKees Cook 
6624c9ec219SKees Cook 	ret = record->psi->write(record);
66330800d99SKees Cook 
66430800d99SKees Cook 	kfree(record->buf);
665077090afSGeliang Tang out:
66630800d99SKees Cook 	record->buf = NULL;
66730800d99SKees Cook 
66830800d99SKees Cook 	return unlikely(ret < 0) ? ret : record->size;
6695bf6d1b9SMark Salyzyn }
6705bf6d1b9SMark Salyzyn 
671ca01d6ddSTony Luck /*
672ca01d6ddSTony Luck  * platform specific persistent storage driver registers with
673ca01d6ddSTony Luck  * us here. If pstore is already mounted, call the platform
674ca01d6ddSTony Luck  * read function right away to populate the file system. If not
675ca01d6ddSTony Luck  * then the pstore mount code will call us later to fill out
676ca01d6ddSTony Luck  * the file system.
677ca01d6ddSTony Luck  */
678ca01d6ddSTony Luck int pstore_register(struct pstore_info *psi)
679ca01d6ddSTony Luck {
680ca01d6ddSTony Luck 	struct module *owner = psi->owner;
681ca01d6ddSTony Luck 
6820d7cd09aSKees Cook 	if (backend && strcmp(backend, psi->name)) {
6830d7cd09aSKees Cook 		pr_warn("ignoring unexpected backend '%s'\n", psi->name);
6848e48b1a8SLenny Szubowicz 		return -EPERM;
6850d7cd09aSKees Cook 	}
6868e48b1a8SLenny Szubowicz 
6874c9ec219SKees Cook 	/* Sanity check flags. */
6884c9ec219SKees Cook 	if (!psi->flags) {
6894c9ec219SKees Cook 		pr_warn("backend '%s' must support at least one frontend\n",
6904c9ec219SKees Cook 			psi->name);
6914c9ec219SKees Cook 		return -EINVAL;
6924c9ec219SKees Cook 	}
6934c9ec219SKees Cook 
6944c9ec219SKees Cook 	/* Check for required functions. */
6954c9ec219SKees Cook 	if (!psi->read || !psi->write) {
6964c9ec219SKees Cook 		pr_warn("backend '%s' must implement read() and write()\n",
6974c9ec219SKees Cook 			psi->name);
6984c9ec219SKees Cook 		return -EINVAL;
6994c9ec219SKees Cook 	}
7004c9ec219SKees Cook 
701ca01d6ddSTony Luck 	spin_lock(&pstore_lock);
702ca01d6ddSTony Luck 	if (psinfo) {
7030d7cd09aSKees Cook 		pr_warn("backend '%s' already loaded: ignoring '%s'\n",
7040d7cd09aSKees Cook 			psinfo->name, psi->name);
705ca01d6ddSTony Luck 		spin_unlock(&pstore_lock);
706ca01d6ddSTony Luck 		return -EBUSY;
707ca01d6ddSTony Luck 	}
708dee28e72SMatthew Garrett 
7094c9ec219SKees Cook 	if (!psi->write_user)
7104c9ec219SKees Cook 		psi->write_user = pstore_write_user_compat;
711ca01d6ddSTony Luck 	psinfo = psi;
712f6f82851SKees Cook 	mutex_init(&psinfo->read_mutex);
713ca01d6ddSTony Luck 	spin_unlock(&pstore_lock);
714ca01d6ddSTony Luck 
715ca01d6ddSTony Luck 	if (owner && !try_module_get(owner)) {
716ca01d6ddSTony Luck 		psinfo = NULL;
717ca01d6ddSTony Luck 		return -EINVAL;
718ca01d6ddSTony Luck 	}
719ca01d6ddSTony Luck 
720b0aad7a9SAruna Balakrishnaiah 	allocate_buf_for_compression();
721b0aad7a9SAruna Balakrishnaiah 
722ca01d6ddSTony Luck 	if (pstore_is_mounted())
7236dda9266SLuck, Tony 		pstore_get_records(0);
724ca01d6ddSTony Luck 
725c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_DMESG)
72618730411SGeliang Tang 		pstore_register_kmsg();
727c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_CONSOLE)
728f29e5956SAnton Vorontsov 		pstore_register_console();
729c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_FTRACE)
73065f8c95eSAnton Vorontsov 		pstore_register_ftrace();
731c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_PMSG)
7329d5438f4SMark Salyzyn 		pstore_register_pmsg();
733ca01d6ddSTony Luck 
7346330d553SKees Cook 	/* Start watching for new records, if desired. */
735a3f5f075SAnton Vorontsov 	if (pstore_update_ms >= 0) {
736a3f5f075SAnton Vorontsov 		pstore_timer.expires = jiffies +
737a3f5f075SAnton Vorontsov 			msecs_to_jiffies(pstore_update_ms);
7386dda9266SLuck, Tony 		add_timer(&pstore_timer);
739a3f5f075SAnton Vorontsov 	}
7406dda9266SLuck, Tony 
74142222c2aSWang Long 	/*
74242222c2aSWang Long 	 * Update the module parameter backend, so it is visible
74342222c2aSWang Long 	 * through /sys/module/pstore/parameters/backend
74442222c2aSWang Long 	 */
74542222c2aSWang Long 	backend = psi->name;
74642222c2aSWang Long 
747ef748853SFabian Frederick 	pr_info("Registered %s as persistent store backend\n", psi->name);
7488e48b1a8SLenny Szubowicz 
7491344dd86SKees Cook 	module_put(owner);
7501344dd86SKees Cook 
751ca01d6ddSTony Luck 	return 0;
752ca01d6ddSTony Luck }
753ca01d6ddSTony Luck EXPORT_SYMBOL_GPL(pstore_register);
754ca01d6ddSTony Luck 
755ee1d2674SGeliang Tang void pstore_unregister(struct pstore_info *psi)
756ee1d2674SGeliang Tang {
7576330d553SKees Cook 	/* Stop timer and make sure all work has finished. */
7586330d553SKees Cook 	pstore_update_ms = -1;
7596330d553SKees Cook 	del_timer_sync(&pstore_timer);
7606330d553SKees Cook 	flush_work(&pstore_work);
7616330d553SKees Cook 
762c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_PMSG)
763ee1d2674SGeliang Tang 		pstore_unregister_pmsg();
764c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_FTRACE)
765ee1d2674SGeliang Tang 		pstore_unregister_ftrace();
766c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_CONSOLE)
767ee1d2674SGeliang Tang 		pstore_unregister_console();
768c950fd6fSNamhyung Kim 	if (psi->flags & PSTORE_FLAGS_DMESG)
769ee1d2674SGeliang Tang 		pstore_unregister_kmsg();
770ee1d2674SGeliang Tang 
771ee1d2674SGeliang Tang 	free_buf_for_compression();
772ee1d2674SGeliang Tang 
773ee1d2674SGeliang Tang 	psinfo = NULL;
774ee1d2674SGeliang Tang 	backend = NULL;
775ee1d2674SGeliang Tang }
776ee1d2674SGeliang Tang EXPORT_SYMBOL_GPL(pstore_unregister);
777ee1d2674SGeliang Tang 
778634f8f51SKees Cook static void decompress_record(struct pstore_record *record)
779634f8f51SKees Cook {
780634f8f51SKees Cook 	int unzipped_len;
7817e8cc8dcSKees Cook 	char *decompressed;
782634f8f51SKees Cook 
7834a16d1cbSAnkit Kumar 	if (!record->compressed)
7844a16d1cbSAnkit Kumar 		return;
7854a16d1cbSAnkit Kumar 
786634f8f51SKees Cook 	/* Only PSTORE_TYPE_DMESG support compression. */
7874a16d1cbSAnkit Kumar 	if (record->type != PSTORE_TYPE_DMESG) {
788634f8f51SKees Cook 		pr_warn("ignored compressed record type %d\n", record->type);
789634f8f51SKees Cook 		return;
790634f8f51SKees Cook 	}
791634f8f51SKees Cook 
792634f8f51SKees Cook 	/* No compression method has created the common buffer. */
793634f8f51SKees Cook 	if (!big_oops_buf) {
794634f8f51SKees Cook 		pr_warn("no decompression buffer allocated\n");
795634f8f51SKees Cook 		return;
796634f8f51SKees Cook 	}
797634f8f51SKees Cook 
798634f8f51SKees Cook 	unzipped_len = pstore_decompress(record->buf, big_oops_buf,
799634f8f51SKees Cook 					 record->size, big_oops_buf_sz);
8007e8cc8dcSKees Cook 	if (unzipped_len <= 0) {
8017e8cc8dcSKees Cook 		pr_err("decompression failed: %d\n", unzipped_len);
8027e8cc8dcSKees Cook 		return;
8037e8cc8dcSKees Cook 	}
8047e8cc8dcSKees Cook 
8057e8cc8dcSKees Cook 	/* Build new buffer for decompressed contents. */
8067e8cc8dcSKees Cook 	decompressed = kmalloc(unzipped_len + record->ecc_notice_size,
8077e8cc8dcSKees Cook 			       GFP_KERNEL);
8087e8cc8dcSKees Cook 	if (!decompressed) {
8097e8cc8dcSKees Cook 		pr_err("decompression ran out of memory\n");
8107e8cc8dcSKees Cook 		return;
8117e8cc8dcSKees Cook 	}
8127e8cc8dcSKees Cook 	memcpy(decompressed, big_oops_buf, unzipped_len);
8137e8cc8dcSKees Cook 
8147e8cc8dcSKees Cook 	/* Append ECC notice to decompressed buffer. */
8157e8cc8dcSKees Cook 	memcpy(decompressed + unzipped_len, record->buf + record->size,
816634f8f51SKees Cook 	       record->ecc_notice_size);
8177e8cc8dcSKees Cook 
8187e8cc8dcSKees Cook 	/* Swap out compresed contents with decompressed contents. */
819634f8f51SKees Cook 	kfree(record->buf);
8207e8cc8dcSKees Cook 	record->buf = decompressed;
821634f8f51SKees Cook 	record->size = unzipped_len;
822634f8f51SKees Cook 	record->compressed = false;
823634f8f51SKees Cook }
824634f8f51SKees Cook 
825ca01d6ddSTony Luck /*
8263a7d2fd1SKees Cook  * Read all the records from one persistent store backend. Create
8276dda9266SLuck, Tony  * files in our filesystem.  Don't warn about -EEXIST errors
8286dda9266SLuck, Tony  * when we are re-scanning the backing store looking to add new
8296dda9266SLuck, Tony  * error records.
830ca01d6ddSTony Luck  */
8313a7d2fd1SKees Cook void pstore_get_backend_records(struct pstore_info *psi,
8323a7d2fd1SKees Cook 				struct dentry *root, int quiet)
833ca01d6ddSTony Luck {
8342a2b0acfSKees Cook 	int failed = 0;
835656de42eSKees Cook 	unsigned int stop_loop = 65536;
836ca01d6ddSTony Luck 
8373a7d2fd1SKees Cook 	if (!psi || !root)
838ca01d6ddSTony Luck 		return;
839ca01d6ddSTony Luck 
840f6f82851SKees Cook 	mutex_lock(&psi->read_mutex);
8412174f6dfSKees Cook 	if (psi->open && psi->open(psi))
84206cf91b4SChen Gong 		goto out;
84306cf91b4SChen Gong 
8441dfff7ddSKees Cook 	/*
8451dfff7ddSKees Cook 	 * Backend callback read() allocates record.buf. decompress_record()
8461dfff7ddSKees Cook 	 * may reallocate record.buf. On success, pstore_mkfile() will keep
8471dfff7ddSKees Cook 	 * the record.buf, so free it only on failure.
8481dfff7ddSKees Cook 	 */
849656de42eSKees Cook 	for (; stop_loop; stop_loop--) {
8502a2b0acfSKees Cook 		struct pstore_record *record;
8512a2b0acfSKees Cook 		int rc;
8522a2b0acfSKees Cook 
8532a2b0acfSKees Cook 		record = kzalloc(sizeof(*record), GFP_KERNEL);
8542a2b0acfSKees Cook 		if (!record) {
8552a2b0acfSKees Cook 			pr_err("out of memory creating record\n");
8562a2b0acfSKees Cook 			break;
8572a2b0acfSKees Cook 		}
858e581ca81SKees Cook 		pstore_record_init(record, psi);
8592a2b0acfSKees Cook 
8602a2b0acfSKees Cook 		record->size = psi->read(record);
8612a2b0acfSKees Cook 
8622a2b0acfSKees Cook 		/* No more records left in backend? */
863f6525b96SDouglas Anderson 		if (record->size <= 0) {
864f6525b96SDouglas Anderson 			kfree(record);
8652a2b0acfSKees Cook 			break;
866f6525b96SDouglas Anderson 		}
8672a2b0acfSKees Cook 
8682a2b0acfSKees Cook 		decompress_record(record);
8693a7d2fd1SKees Cook 		rc = pstore_mkfile(root, record);
8701dfff7ddSKees Cook 		if (rc) {
87183f70f07SKees Cook 			/* pstore_mkfile() did not take record, so free it. */
8722a2b0acfSKees Cook 			kfree(record->buf);
87383f70f07SKees Cook 			kfree(record);
8741dfff7ddSKees Cook 			if (rc != -EEXIST || !quiet)
8751dfff7ddSKees Cook 				failed++;
8761dfff7ddSKees Cook 		}
877ca01d6ddSTony Luck 	}
8782174f6dfSKees Cook 	if (psi->close)
87906cf91b4SChen Gong 		psi->close(psi);
88006cf91b4SChen Gong out:
881f6f82851SKees Cook 	mutex_unlock(&psi->read_mutex);
882ca01d6ddSTony Luck 
883ca01d6ddSTony Luck 	if (failed)
884656de42eSKees Cook 		pr_warn("failed to create %d record(s) from '%s'\n",
885ca01d6ddSTony Luck 			failed, psi->name);
886656de42eSKees Cook 	if (!stop_loop)
887656de42eSKees Cook 		pr_err("looping? Too many records seen from '%s'\n",
888656de42eSKees Cook 			psi->name);
889ca01d6ddSTony Luck }
890ca01d6ddSTony Luck 
8916dda9266SLuck, Tony static void pstore_dowork(struct work_struct *work)
8926dda9266SLuck, Tony {
8936dda9266SLuck, Tony 	pstore_get_records(1);
8946dda9266SLuck, Tony }
8956dda9266SLuck, Tony 
8966dda9266SLuck, Tony static void pstore_timefunc(unsigned long dummy)
8976dda9266SLuck, Tony {
8986dda9266SLuck, Tony 	if (pstore_new_entry) {
8996dda9266SLuck, Tony 		pstore_new_entry = 0;
9006dda9266SLuck, Tony 		schedule_work(&pstore_work);
9016dda9266SLuck, Tony 	}
9026dda9266SLuck, Tony 
9036330d553SKees Cook 	if (pstore_update_ms >= 0)
9046330d553SKees Cook 		mod_timer(&pstore_timer,
9056330d553SKees Cook 			  jiffies + msecs_to_jiffies(pstore_update_ms));
9066dda9266SLuck, Tony }
9076dda9266SLuck, Tony 
908dee28e72SMatthew Garrett module_param(backend, charp, 0444);
909dee28e72SMatthew Garrett MODULE_PARM_DESC(backend, "Pstore backend to use");
910