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 64*24ed960aSKees Cook static void pstore_timefunc(struct timer_list *); 651d27e3e2SKees Cook static DEFINE_TIMER(pstore_timer, pstore_timefunc); 666dda9266SLuck, Tony 676dda9266SLuck, Tony static void pstore_dowork(struct work_struct *); 686dda9266SLuck, Tony static DECLARE_WORK(pstore_work, pstore_dowork); 696dda9266SLuck, Tony 706dda9266SLuck, Tony /* 71ca01d6ddSTony Luck * pstore_lock just protects "psinfo" during 72ca01d6ddSTony Luck * calls to pstore_register() 73ca01d6ddSTony Luck */ 74ca01d6ddSTony Luck static DEFINE_SPINLOCK(pstore_lock); 75060287b8SAnton Vorontsov struct pstore_info *psinfo; 76ca01d6ddSTony Luck 77dee28e72SMatthew Garrett static char *backend; 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. */ 485df27067eSArnd Bergmann record->time = ns_to_timespec(ktime_get_real_fast_ns()); 486e581ca81SKees Cook } 487e581ca81SKees Cook 488ca01d6ddSTony Luck /* 489ca01d6ddSTony Luck * callback from kmsg_dump. (s2,l2) has the most recently 490ca01d6ddSTony Luck * written bytes, older bytes are in (s1,l1). Save as much 491ca01d6ddSTony Luck * as we can from the end of the buffer. 492ca01d6ddSTony Luck */ 493ca01d6ddSTony Luck static void pstore_dump(struct kmsg_dumper *dumper, 494e2ae715dSKay Sievers enum kmsg_dump_reason reason) 495ca01d6ddSTony Luck { 496e2ae715dSKay Sievers unsigned long total = 0; 497381b872cSSeiji Aguchi const char *why; 498b94fdd07SMatthew Garrett unsigned int part = 1; 499abd4d558SDon Zickus unsigned long flags = 0; 50098e44fdaSNamhyung Kim int is_locked; 501e2ae715dSKay Sievers int ret; 502ca01d6ddSTony Luck 503381b872cSSeiji Aguchi why = get_reason_str(reason); 5049f6af27fSTony Luck 5059f244e9cSSeiji Aguchi if (pstore_cannot_block_path(reason)) { 5069f244e9cSSeiji Aguchi is_locked = spin_trylock_irqsave(&psinfo->buf_lock, flags); 5079f244e9cSSeiji Aguchi if (!is_locked) { 5089f244e9cSSeiji Aguchi pr_err("pstore dump routine blocked in %s path, may corrupt error record\n" 5099f244e9cSSeiji Aguchi , in_nmi() ? "NMI" : why); 510959217c8SLi Pengcheng return; 5119f244e9cSSeiji Aguchi } 51298e44fdaSNamhyung Kim } else { 513abd4d558SDon Zickus spin_lock_irqsave(&psinfo->buf_lock, flags); 51498e44fdaSNamhyung Kim is_locked = 1; 51598e44fdaSNamhyung Kim } 516ca01d6ddSTony Luck oopscount++; 517ca01d6ddSTony Luck while (total < kmsg_bytes) { 518e2ae715dSKay Sievers char *dst; 51976cc9580SKees Cook size_t dst_size; 52076cc9580SKees Cook int header_size; 521b0aad7a9SAruna Balakrishnaiah int zipped_len = -1; 52276cc9580SKees Cook size_t dump_size; 523e581ca81SKees Cook struct pstore_record record; 524e581ca81SKees Cook 525e581ca81SKees Cook pstore_record_init(&record, psinfo); 526e581ca81SKees Cook record.type = PSTORE_TYPE_DMESG; 527e581ca81SKees Cook record.count = oopscount; 528e581ca81SKees Cook record.reason = reason; 529e581ca81SKees Cook record.part = part; 530e581ca81SKees Cook record.buf = psinfo->buf; 531e2ae715dSKay Sievers 532f0e2efcfSKonstantin Khlebnikov if (big_oops_buf && is_locked) { 533b0aad7a9SAruna Balakrishnaiah dst = big_oops_buf; 53476cc9580SKees Cook dst_size = big_oops_buf_sz; 535235f6d15SNamhyung Kim } else { 536235f6d15SNamhyung Kim dst = psinfo->buf; 53776cc9580SKees Cook dst_size = psinfo->bufsize; 538235f6d15SNamhyung Kim } 539235f6d15SNamhyung Kim 54076cc9580SKees Cook /* Write dump header. */ 54176cc9580SKees Cook header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why, 54276cc9580SKees Cook oopscount, part); 54376cc9580SKees Cook dst_size -= header_size; 544b0aad7a9SAruna Balakrishnaiah 54576cc9580SKees Cook /* Write dump contents. */ 54676cc9580SKees Cook if (!kmsg_dump_get_buffer(dumper, true, dst + header_size, 54776cc9580SKees Cook dst_size, &dump_size)) 548b0aad7a9SAruna Balakrishnaiah break; 549b0aad7a9SAruna Balakrishnaiah 550235f6d15SNamhyung Kim if (big_oops_buf && is_locked) { 551b0aad7a9SAruna Balakrishnaiah zipped_len = pstore_compress(dst, psinfo->buf, 55276cc9580SKees Cook header_size + dump_size, 55376cc9580SKees Cook psinfo->bufsize); 554b0aad7a9SAruna Balakrishnaiah 555b0aad7a9SAruna Balakrishnaiah if (zipped_len > 0) { 55676cc9580SKees Cook record.compressed = true; 55776cc9580SKees Cook record.size = zipped_len; 558b0aad7a9SAruna Balakrishnaiah } else { 55976cc9580SKees Cook record.size = copy_kmsg_to_buffer(header_size, 56076cc9580SKees Cook dump_size); 561b0aad7a9SAruna Balakrishnaiah } 562b0aad7a9SAruna Balakrishnaiah } else { 56376cc9580SKees Cook record.size = header_size + dump_size; 564b0aad7a9SAruna Balakrishnaiah } 565b0aad7a9SAruna Balakrishnaiah 56676cc9580SKees Cook ret = psinfo->write(&record); 567b238b8faSChen Gong if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted()) 5686dda9266SLuck, Tony pstore_new_entry = 1; 569e2ae715dSKay Sievers 57076cc9580SKees Cook total += record.size; 57156280682SMatthew Garrett part++; 572ca01d6ddSTony Luck } 573abd4d558SDon Zickus if (is_locked) 5749f244e9cSSeiji Aguchi spin_unlock_irqrestore(&psinfo->buf_lock, flags); 575ca01d6ddSTony Luck } 576ca01d6ddSTony Luck 577ca01d6ddSTony Luck static struct kmsg_dumper pstore_dumper = { 578ca01d6ddSTony Luck .dump = pstore_dump, 579ca01d6ddSTony Luck }; 580ca01d6ddSTony Luck 581306e5c2aSGeliang Tang /* 582306e5c2aSGeliang Tang * Register with kmsg_dump to save last part of console log on panic. 583306e5c2aSGeliang Tang */ 58418730411SGeliang Tang static void pstore_register_kmsg(void) 58518730411SGeliang Tang { 58618730411SGeliang Tang kmsg_dump_register(&pstore_dumper); 58718730411SGeliang Tang } 58818730411SGeliang Tang 589ee1d2674SGeliang Tang static void pstore_unregister_kmsg(void) 590ee1d2674SGeliang Tang { 591ee1d2674SGeliang Tang kmsg_dump_unregister(&pstore_dumper); 592ee1d2674SGeliang Tang } 593ee1d2674SGeliang Tang 594f29e5956SAnton Vorontsov #ifdef CONFIG_PSTORE_CONSOLE 595f29e5956SAnton Vorontsov static void pstore_console_write(struct console *con, const char *s, unsigned c) 596f29e5956SAnton Vorontsov { 597f29e5956SAnton Vorontsov const char *e = s + c; 598f29e5956SAnton Vorontsov 599f29e5956SAnton Vorontsov while (s < e) { 600e581ca81SKees Cook struct pstore_record record; 601f29e5956SAnton Vorontsov unsigned long flags; 602f29e5956SAnton Vorontsov 603e581ca81SKees Cook pstore_record_init(&record, psinfo); 604e581ca81SKees Cook record.type = PSTORE_TYPE_CONSOLE; 605e581ca81SKees Cook 606f29e5956SAnton Vorontsov if (c > psinfo->bufsize) 607f29e5956SAnton Vorontsov c = psinfo->bufsize; 60880c9d03cSChuansheng Liu 60980c9d03cSChuansheng Liu if (oops_in_progress) { 61080c9d03cSChuansheng Liu if (!spin_trylock_irqsave(&psinfo->buf_lock, flags)) 61180c9d03cSChuansheng Liu break; 61280c9d03cSChuansheng Liu } else { 613f29e5956SAnton Vorontsov spin_lock_irqsave(&psinfo->buf_lock, flags); 61480c9d03cSChuansheng Liu } 615b10b4711SKees Cook record.buf = (char *)s; 616b10b4711SKees Cook record.size = c; 6174c9ec219SKees Cook psinfo->write(&record); 618f29e5956SAnton Vorontsov spin_unlock_irqrestore(&psinfo->buf_lock, flags); 619f29e5956SAnton Vorontsov s += c; 620f29e5956SAnton Vorontsov c = e - s; 621f29e5956SAnton Vorontsov } 622f29e5956SAnton Vorontsov } 623f29e5956SAnton Vorontsov 624f29e5956SAnton Vorontsov static struct console pstore_console = { 625f29e5956SAnton Vorontsov .name = "pstore", 626f29e5956SAnton Vorontsov .write = pstore_console_write, 627f29e5956SAnton Vorontsov .flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME, 628f29e5956SAnton Vorontsov .index = -1, 629f29e5956SAnton Vorontsov }; 630f29e5956SAnton Vorontsov 631f29e5956SAnton Vorontsov static void pstore_register_console(void) 632f29e5956SAnton Vorontsov { 633f29e5956SAnton Vorontsov register_console(&pstore_console); 634f29e5956SAnton Vorontsov } 635ee1d2674SGeliang Tang 636ee1d2674SGeliang Tang static void pstore_unregister_console(void) 637ee1d2674SGeliang Tang { 638ee1d2674SGeliang Tang unregister_console(&pstore_console); 639ee1d2674SGeliang Tang } 640f29e5956SAnton Vorontsov #else 641f29e5956SAnton Vorontsov static void pstore_register_console(void) {} 642ee1d2674SGeliang Tang static void pstore_unregister_console(void) {} 643f29e5956SAnton Vorontsov #endif 644f29e5956SAnton Vorontsov 6454c9ec219SKees Cook static int pstore_write_user_compat(struct pstore_record *record, 646fdd03118SKees Cook const char __user *buf) 6475bf6d1b9SMark Salyzyn { 64830800d99SKees Cook int ret = 0; 6495bf6d1b9SMark Salyzyn 65030800d99SKees Cook if (record->buf) 65130800d99SKees Cook return -EINVAL; 6525bf6d1b9SMark Salyzyn 653077090afSGeliang Tang record->buf = memdup_user(buf, record->size); 654dfd6fa39SHirofumi Nakagawa if (IS_ERR(record->buf)) { 655077090afSGeliang Tang ret = PTR_ERR(record->buf); 65630800d99SKees Cook goto out; 6575bf6d1b9SMark Salyzyn } 65830800d99SKees Cook 6594c9ec219SKees Cook ret = record->psi->write(record); 66030800d99SKees Cook 66130800d99SKees Cook kfree(record->buf); 662077090afSGeliang Tang out: 66330800d99SKees Cook record->buf = NULL; 66430800d99SKees Cook 66530800d99SKees Cook return unlikely(ret < 0) ? ret : record->size; 6665bf6d1b9SMark Salyzyn } 6675bf6d1b9SMark Salyzyn 668ca01d6ddSTony Luck /* 669ca01d6ddSTony Luck * platform specific persistent storage driver registers with 670ca01d6ddSTony Luck * us here. If pstore is already mounted, call the platform 671ca01d6ddSTony Luck * read function right away to populate the file system. If not 672ca01d6ddSTony Luck * then the pstore mount code will call us later to fill out 673ca01d6ddSTony Luck * the file system. 674ca01d6ddSTony Luck */ 675ca01d6ddSTony Luck int pstore_register(struct pstore_info *psi) 676ca01d6ddSTony Luck { 677ca01d6ddSTony Luck struct module *owner = psi->owner; 678ca01d6ddSTony Luck 6790d7cd09aSKees Cook if (backend && strcmp(backend, psi->name)) { 6800d7cd09aSKees Cook pr_warn("ignoring unexpected backend '%s'\n", psi->name); 6818e48b1a8SLenny Szubowicz return -EPERM; 6820d7cd09aSKees Cook } 6838e48b1a8SLenny Szubowicz 6844c9ec219SKees Cook /* Sanity check flags. */ 6854c9ec219SKees Cook if (!psi->flags) { 6864c9ec219SKees Cook pr_warn("backend '%s' must support at least one frontend\n", 6874c9ec219SKees Cook psi->name); 6884c9ec219SKees Cook return -EINVAL; 6894c9ec219SKees Cook } 6904c9ec219SKees Cook 6914c9ec219SKees Cook /* Check for required functions. */ 6924c9ec219SKees Cook if (!psi->read || !psi->write) { 6934c9ec219SKees Cook pr_warn("backend '%s' must implement read() and write()\n", 6944c9ec219SKees Cook psi->name); 6954c9ec219SKees Cook return -EINVAL; 6964c9ec219SKees Cook } 6974c9ec219SKees Cook 698ca01d6ddSTony Luck spin_lock(&pstore_lock); 699ca01d6ddSTony Luck if (psinfo) { 7000d7cd09aSKees Cook pr_warn("backend '%s' already loaded: ignoring '%s'\n", 7010d7cd09aSKees Cook psinfo->name, psi->name); 702ca01d6ddSTony Luck spin_unlock(&pstore_lock); 703ca01d6ddSTony Luck return -EBUSY; 704ca01d6ddSTony Luck } 705dee28e72SMatthew Garrett 7064c9ec219SKees Cook if (!psi->write_user) 7074c9ec219SKees Cook psi->write_user = pstore_write_user_compat; 708ca01d6ddSTony Luck psinfo = psi; 709f6f82851SKees Cook mutex_init(&psinfo->read_mutex); 710ca01d6ddSTony Luck spin_unlock(&pstore_lock); 711ca01d6ddSTony Luck 712ca01d6ddSTony Luck if (owner && !try_module_get(owner)) { 713ca01d6ddSTony Luck psinfo = NULL; 714ca01d6ddSTony Luck return -EINVAL; 715ca01d6ddSTony Luck } 716ca01d6ddSTony Luck 717b0aad7a9SAruna Balakrishnaiah allocate_buf_for_compression(); 718b0aad7a9SAruna Balakrishnaiah 719ca01d6ddSTony Luck if (pstore_is_mounted()) 7206dda9266SLuck, Tony pstore_get_records(0); 721ca01d6ddSTony Luck 722c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_DMESG) 72318730411SGeliang Tang pstore_register_kmsg(); 724c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_CONSOLE) 725f29e5956SAnton Vorontsov pstore_register_console(); 726c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_FTRACE) 72765f8c95eSAnton Vorontsov pstore_register_ftrace(); 728c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_PMSG) 7299d5438f4SMark Salyzyn pstore_register_pmsg(); 730ca01d6ddSTony Luck 7316330d553SKees Cook /* Start watching for new records, if desired. */ 732a3f5f075SAnton Vorontsov if (pstore_update_ms >= 0) { 733a3f5f075SAnton Vorontsov pstore_timer.expires = jiffies + 734a3f5f075SAnton Vorontsov msecs_to_jiffies(pstore_update_ms); 7356dda9266SLuck, Tony add_timer(&pstore_timer); 736a3f5f075SAnton Vorontsov } 7376dda9266SLuck, Tony 73842222c2aSWang Long /* 73942222c2aSWang Long * Update the module parameter backend, so it is visible 74042222c2aSWang Long * through /sys/module/pstore/parameters/backend 74142222c2aSWang Long */ 74242222c2aSWang Long backend = psi->name; 74342222c2aSWang Long 744ef748853SFabian Frederick pr_info("Registered %s as persistent store backend\n", psi->name); 7458e48b1a8SLenny Szubowicz 7461344dd86SKees Cook module_put(owner); 7471344dd86SKees Cook 748ca01d6ddSTony Luck return 0; 749ca01d6ddSTony Luck } 750ca01d6ddSTony Luck EXPORT_SYMBOL_GPL(pstore_register); 751ca01d6ddSTony Luck 752ee1d2674SGeliang Tang void pstore_unregister(struct pstore_info *psi) 753ee1d2674SGeliang Tang { 7546330d553SKees Cook /* Stop timer and make sure all work has finished. */ 7556330d553SKees Cook pstore_update_ms = -1; 7566330d553SKees Cook del_timer_sync(&pstore_timer); 7576330d553SKees Cook flush_work(&pstore_work); 7586330d553SKees Cook 759c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_PMSG) 760ee1d2674SGeliang Tang pstore_unregister_pmsg(); 761c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_FTRACE) 762ee1d2674SGeliang Tang pstore_unregister_ftrace(); 763c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_CONSOLE) 764ee1d2674SGeliang Tang pstore_unregister_console(); 765c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_DMESG) 766ee1d2674SGeliang Tang pstore_unregister_kmsg(); 767ee1d2674SGeliang Tang 768ee1d2674SGeliang Tang free_buf_for_compression(); 769ee1d2674SGeliang Tang 770ee1d2674SGeliang Tang psinfo = NULL; 771ee1d2674SGeliang Tang backend = NULL; 772ee1d2674SGeliang Tang } 773ee1d2674SGeliang Tang EXPORT_SYMBOL_GPL(pstore_unregister); 774ee1d2674SGeliang Tang 775634f8f51SKees Cook static void decompress_record(struct pstore_record *record) 776634f8f51SKees Cook { 777634f8f51SKees Cook int unzipped_len; 7787e8cc8dcSKees Cook char *decompressed; 779634f8f51SKees Cook 7804a16d1cbSAnkit Kumar if (!record->compressed) 7814a16d1cbSAnkit Kumar return; 7824a16d1cbSAnkit Kumar 783634f8f51SKees Cook /* Only PSTORE_TYPE_DMESG support compression. */ 7844a16d1cbSAnkit Kumar if (record->type != PSTORE_TYPE_DMESG) { 785634f8f51SKees Cook pr_warn("ignored compressed record type %d\n", record->type); 786634f8f51SKees Cook return; 787634f8f51SKees Cook } 788634f8f51SKees Cook 789634f8f51SKees Cook /* No compression method has created the common buffer. */ 790634f8f51SKees Cook if (!big_oops_buf) { 791634f8f51SKees Cook pr_warn("no decompression buffer allocated\n"); 792634f8f51SKees Cook return; 793634f8f51SKees Cook } 794634f8f51SKees Cook 795634f8f51SKees Cook unzipped_len = pstore_decompress(record->buf, big_oops_buf, 796634f8f51SKees Cook record->size, big_oops_buf_sz); 7977e8cc8dcSKees Cook if (unzipped_len <= 0) { 7987e8cc8dcSKees Cook pr_err("decompression failed: %d\n", unzipped_len); 7997e8cc8dcSKees Cook return; 8007e8cc8dcSKees Cook } 8017e8cc8dcSKees Cook 8027e8cc8dcSKees Cook /* Build new buffer for decompressed contents. */ 8037e8cc8dcSKees Cook decompressed = kmalloc(unzipped_len + record->ecc_notice_size, 8047e8cc8dcSKees Cook GFP_KERNEL); 8057e8cc8dcSKees Cook if (!decompressed) { 8067e8cc8dcSKees Cook pr_err("decompression ran out of memory\n"); 8077e8cc8dcSKees Cook return; 8087e8cc8dcSKees Cook } 8097e8cc8dcSKees Cook memcpy(decompressed, big_oops_buf, unzipped_len); 8107e8cc8dcSKees Cook 8117e8cc8dcSKees Cook /* Append ECC notice to decompressed buffer. */ 8127e8cc8dcSKees Cook memcpy(decompressed + unzipped_len, record->buf + record->size, 813634f8f51SKees Cook record->ecc_notice_size); 8147e8cc8dcSKees Cook 8157e8cc8dcSKees Cook /* Swap out compresed contents with decompressed contents. */ 816634f8f51SKees Cook kfree(record->buf); 8177e8cc8dcSKees Cook record->buf = decompressed; 818634f8f51SKees Cook record->size = unzipped_len; 819634f8f51SKees Cook record->compressed = false; 820634f8f51SKees Cook } 821634f8f51SKees Cook 822ca01d6ddSTony Luck /* 8233a7d2fd1SKees Cook * Read all the records from one persistent store backend. Create 8246dda9266SLuck, Tony * files in our filesystem. Don't warn about -EEXIST errors 8256dda9266SLuck, Tony * when we are re-scanning the backing store looking to add new 8266dda9266SLuck, Tony * error records. 827ca01d6ddSTony Luck */ 8283a7d2fd1SKees Cook void pstore_get_backend_records(struct pstore_info *psi, 8293a7d2fd1SKees Cook struct dentry *root, int quiet) 830ca01d6ddSTony Luck { 8312a2b0acfSKees Cook int failed = 0; 832656de42eSKees Cook unsigned int stop_loop = 65536; 833ca01d6ddSTony Luck 8343a7d2fd1SKees Cook if (!psi || !root) 835ca01d6ddSTony Luck return; 836ca01d6ddSTony Luck 837f6f82851SKees Cook mutex_lock(&psi->read_mutex); 8382174f6dfSKees Cook if (psi->open && psi->open(psi)) 83906cf91b4SChen Gong goto out; 84006cf91b4SChen Gong 8411dfff7ddSKees Cook /* 8421dfff7ddSKees Cook * Backend callback read() allocates record.buf. decompress_record() 8431dfff7ddSKees Cook * may reallocate record.buf. On success, pstore_mkfile() will keep 8441dfff7ddSKees Cook * the record.buf, so free it only on failure. 8451dfff7ddSKees Cook */ 846656de42eSKees Cook for (; stop_loop; stop_loop--) { 8472a2b0acfSKees Cook struct pstore_record *record; 8482a2b0acfSKees Cook int rc; 8492a2b0acfSKees Cook 8502a2b0acfSKees Cook record = kzalloc(sizeof(*record), GFP_KERNEL); 8512a2b0acfSKees Cook if (!record) { 8522a2b0acfSKees Cook pr_err("out of memory creating record\n"); 8532a2b0acfSKees Cook break; 8542a2b0acfSKees Cook } 855e581ca81SKees Cook pstore_record_init(record, psi); 8562a2b0acfSKees Cook 8572a2b0acfSKees Cook record->size = psi->read(record); 8582a2b0acfSKees Cook 8592a2b0acfSKees Cook /* No more records left in backend? */ 860f6525b96SDouglas Anderson if (record->size <= 0) { 861f6525b96SDouglas Anderson kfree(record); 8622a2b0acfSKees Cook break; 863f6525b96SDouglas Anderson } 8642a2b0acfSKees Cook 8652a2b0acfSKees Cook decompress_record(record); 8663a7d2fd1SKees Cook rc = pstore_mkfile(root, record); 8671dfff7ddSKees Cook if (rc) { 86883f70f07SKees Cook /* pstore_mkfile() did not take record, so free it. */ 8692a2b0acfSKees Cook kfree(record->buf); 87083f70f07SKees Cook kfree(record); 8711dfff7ddSKees Cook if (rc != -EEXIST || !quiet) 8721dfff7ddSKees Cook failed++; 8731dfff7ddSKees Cook } 874ca01d6ddSTony Luck } 8752174f6dfSKees Cook if (psi->close) 87606cf91b4SChen Gong psi->close(psi); 87706cf91b4SChen Gong out: 878f6f82851SKees Cook mutex_unlock(&psi->read_mutex); 879ca01d6ddSTony Luck 880ca01d6ddSTony Luck if (failed) 881656de42eSKees Cook pr_warn("failed to create %d record(s) from '%s'\n", 882ca01d6ddSTony Luck failed, psi->name); 883656de42eSKees Cook if (!stop_loop) 884656de42eSKees Cook pr_err("looping? Too many records seen from '%s'\n", 885656de42eSKees Cook psi->name); 886ca01d6ddSTony Luck } 887ca01d6ddSTony Luck 8886dda9266SLuck, Tony static void pstore_dowork(struct work_struct *work) 8896dda9266SLuck, Tony { 8906dda9266SLuck, Tony pstore_get_records(1); 8916dda9266SLuck, Tony } 8926dda9266SLuck, Tony 893*24ed960aSKees Cook static void pstore_timefunc(struct timer_list *unused) 8946dda9266SLuck, Tony { 8956dda9266SLuck, Tony if (pstore_new_entry) { 8966dda9266SLuck, Tony pstore_new_entry = 0; 8976dda9266SLuck, Tony schedule_work(&pstore_work); 8986dda9266SLuck, Tony } 8996dda9266SLuck, Tony 9006330d553SKees Cook if (pstore_update_ms >= 0) 9016330d553SKees Cook mod_timer(&pstore_timer, 9026330d553SKees Cook jiffies + msecs_to_jiffies(pstore_update_ms)); 9036dda9266SLuck, Tony } 9046dda9266SLuck, Tony 905dee28e72SMatthew Garrett module_param(backend, charp, 0444); 906dee28e72SMatthew Garrett MODULE_PARM_DESC(backend, "Pstore backend to use"); 907