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); 656dda9266SLuck, Tony static DEFINE_TIMER(pstore_timer, pstore_timefunc, 0, 0); 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 */ 102ca01d6ddSTony Luck static unsigned long kmsg_bytes = 10240; 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 477ca01d6ddSTony Luck /* 478ca01d6ddSTony Luck * callback from kmsg_dump. (s2,l2) has the most recently 479ca01d6ddSTony Luck * written bytes, older bytes are in (s1,l1). Save as much 480ca01d6ddSTony Luck * as we can from the end of the buffer. 481ca01d6ddSTony Luck */ 482ca01d6ddSTony Luck static void pstore_dump(struct kmsg_dumper *dumper, 483e2ae715dSKay Sievers enum kmsg_dump_reason reason) 484ca01d6ddSTony Luck { 485e2ae715dSKay Sievers unsigned long total = 0; 486381b872cSSeiji Aguchi const char *why; 487b94fdd07SMatthew Garrett unsigned int part = 1; 488abd4d558SDon Zickus unsigned long flags = 0; 48998e44fdaSNamhyung Kim int is_locked; 490e2ae715dSKay Sievers int ret; 491ca01d6ddSTony Luck 492381b872cSSeiji Aguchi why = get_reason_str(reason); 4939f6af27fSTony Luck 4949f244e9cSSeiji Aguchi if (pstore_cannot_block_path(reason)) { 4959f244e9cSSeiji Aguchi is_locked = spin_trylock_irqsave(&psinfo->buf_lock, flags); 4969f244e9cSSeiji Aguchi if (!is_locked) { 4979f244e9cSSeiji Aguchi pr_err("pstore dump routine blocked in %s path, may corrupt error record\n" 4989f244e9cSSeiji Aguchi , in_nmi() ? "NMI" : why); 499959217c8SLi Pengcheng return; 5009f244e9cSSeiji Aguchi } 50198e44fdaSNamhyung Kim } else { 502abd4d558SDon Zickus spin_lock_irqsave(&psinfo->buf_lock, flags); 50398e44fdaSNamhyung Kim is_locked = 1; 50498e44fdaSNamhyung Kim } 505ca01d6ddSTony Luck oopscount++; 506ca01d6ddSTony Luck while (total < kmsg_bytes) { 507e2ae715dSKay Sievers char *dst; 50876cc9580SKees Cook size_t dst_size; 50976cc9580SKees Cook int header_size; 510b0aad7a9SAruna Balakrishnaiah int zipped_len = -1; 51176cc9580SKees Cook size_t dump_size; 51276cc9580SKees Cook struct pstore_record record = { 51376cc9580SKees Cook .type = PSTORE_TYPE_DMESG, 51476cc9580SKees Cook .count = oopscount, 51576cc9580SKees Cook .reason = reason, 51676cc9580SKees Cook .part = part, 51776cc9580SKees Cook .compressed = false, 51876cc9580SKees Cook .buf = psinfo->buf, 51976cc9580SKees Cook .psi = psinfo, 52076cc9580SKees Cook }; 521e2ae715dSKay Sievers 522f0e2efcfSKonstantin Khlebnikov if (big_oops_buf && is_locked) { 523b0aad7a9SAruna Balakrishnaiah dst = big_oops_buf; 52476cc9580SKees Cook dst_size = big_oops_buf_sz; 525235f6d15SNamhyung Kim } else { 526235f6d15SNamhyung Kim dst = psinfo->buf; 52776cc9580SKees Cook dst_size = psinfo->bufsize; 528235f6d15SNamhyung Kim } 529235f6d15SNamhyung Kim 53076cc9580SKees Cook /* Write dump header. */ 53176cc9580SKees Cook header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why, 53276cc9580SKees Cook oopscount, part); 53376cc9580SKees Cook dst_size -= header_size; 534b0aad7a9SAruna Balakrishnaiah 53576cc9580SKees Cook /* Write dump contents. */ 53676cc9580SKees Cook if (!kmsg_dump_get_buffer(dumper, true, dst + header_size, 53776cc9580SKees Cook dst_size, &dump_size)) 538b0aad7a9SAruna Balakrishnaiah break; 539b0aad7a9SAruna Balakrishnaiah 540235f6d15SNamhyung Kim if (big_oops_buf && is_locked) { 541b0aad7a9SAruna Balakrishnaiah zipped_len = pstore_compress(dst, psinfo->buf, 54276cc9580SKees Cook header_size + dump_size, 54376cc9580SKees Cook psinfo->bufsize); 544b0aad7a9SAruna Balakrishnaiah 545b0aad7a9SAruna Balakrishnaiah if (zipped_len > 0) { 54676cc9580SKees Cook record.compressed = true; 54776cc9580SKees Cook record.size = zipped_len; 548b0aad7a9SAruna Balakrishnaiah } else { 54976cc9580SKees Cook record.size = copy_kmsg_to_buffer(header_size, 55076cc9580SKees Cook dump_size); 551b0aad7a9SAruna Balakrishnaiah } 552b0aad7a9SAruna Balakrishnaiah } else { 55376cc9580SKees Cook record.size = header_size + dump_size; 554b0aad7a9SAruna Balakrishnaiah } 555b0aad7a9SAruna Balakrishnaiah 55676cc9580SKees Cook ret = psinfo->write(&record); 557b238b8faSChen Gong if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted()) 5586dda9266SLuck, Tony pstore_new_entry = 1; 559e2ae715dSKay Sievers 56076cc9580SKees Cook total += record.size; 56156280682SMatthew Garrett part++; 562ca01d6ddSTony Luck } 563abd4d558SDon Zickus if (is_locked) 5649f244e9cSSeiji Aguchi spin_unlock_irqrestore(&psinfo->buf_lock, flags); 565ca01d6ddSTony Luck } 566ca01d6ddSTony Luck 567ca01d6ddSTony Luck static struct kmsg_dumper pstore_dumper = { 568ca01d6ddSTony Luck .dump = pstore_dump, 569ca01d6ddSTony Luck }; 570ca01d6ddSTony Luck 571306e5c2aSGeliang Tang /* 572306e5c2aSGeliang Tang * Register with kmsg_dump to save last part of console log on panic. 573306e5c2aSGeliang Tang */ 57418730411SGeliang Tang static void pstore_register_kmsg(void) 57518730411SGeliang Tang { 57618730411SGeliang Tang kmsg_dump_register(&pstore_dumper); 57718730411SGeliang Tang } 57818730411SGeliang Tang 579ee1d2674SGeliang Tang static void pstore_unregister_kmsg(void) 580ee1d2674SGeliang Tang { 581ee1d2674SGeliang Tang kmsg_dump_unregister(&pstore_dumper); 582ee1d2674SGeliang Tang } 583ee1d2674SGeliang Tang 584f29e5956SAnton Vorontsov #ifdef CONFIG_PSTORE_CONSOLE 585f29e5956SAnton Vorontsov static void pstore_console_write(struct console *con, const char *s, unsigned c) 586f29e5956SAnton Vorontsov { 587f29e5956SAnton Vorontsov const char *e = s + c; 588f29e5956SAnton Vorontsov 589f29e5956SAnton Vorontsov while (s < e) { 590b10b4711SKees Cook struct pstore_record record = { 591b10b4711SKees Cook .type = PSTORE_TYPE_CONSOLE, 592b10b4711SKees Cook .psi = psinfo, 593b10b4711SKees Cook }; 594f29e5956SAnton Vorontsov unsigned long flags; 595f29e5956SAnton Vorontsov 596f29e5956SAnton Vorontsov if (c > psinfo->bufsize) 597f29e5956SAnton Vorontsov c = psinfo->bufsize; 59880c9d03cSChuansheng Liu 59980c9d03cSChuansheng Liu if (oops_in_progress) { 60080c9d03cSChuansheng Liu if (!spin_trylock_irqsave(&psinfo->buf_lock, flags)) 60180c9d03cSChuansheng Liu break; 60280c9d03cSChuansheng Liu } else { 603f29e5956SAnton Vorontsov spin_lock_irqsave(&psinfo->buf_lock, flags); 60480c9d03cSChuansheng Liu } 605b10b4711SKees Cook record.buf = (char *)s; 606b10b4711SKees Cook record.size = c; 6074c9ec219SKees Cook psinfo->write(&record); 608f29e5956SAnton Vorontsov spin_unlock_irqrestore(&psinfo->buf_lock, flags); 609f29e5956SAnton Vorontsov s += c; 610f29e5956SAnton Vorontsov c = e - s; 611f29e5956SAnton Vorontsov } 612f29e5956SAnton Vorontsov } 613f29e5956SAnton Vorontsov 614f29e5956SAnton Vorontsov static struct console pstore_console = { 615f29e5956SAnton Vorontsov .name = "pstore", 616f29e5956SAnton Vorontsov .write = pstore_console_write, 617f29e5956SAnton Vorontsov .flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME, 618f29e5956SAnton Vorontsov .index = -1, 619f29e5956SAnton Vorontsov }; 620f29e5956SAnton Vorontsov 621f29e5956SAnton Vorontsov static void pstore_register_console(void) 622f29e5956SAnton Vorontsov { 623f29e5956SAnton Vorontsov register_console(&pstore_console); 624f29e5956SAnton Vorontsov } 625ee1d2674SGeliang Tang 626ee1d2674SGeliang Tang static void pstore_unregister_console(void) 627ee1d2674SGeliang Tang { 628ee1d2674SGeliang Tang unregister_console(&pstore_console); 629ee1d2674SGeliang Tang } 630f29e5956SAnton Vorontsov #else 631f29e5956SAnton Vorontsov static void pstore_register_console(void) {} 632ee1d2674SGeliang Tang static void pstore_unregister_console(void) {} 633f29e5956SAnton Vorontsov #endif 634f29e5956SAnton Vorontsov 6354c9ec219SKees Cook static int pstore_write_user_compat(struct pstore_record *record, 636fdd03118SKees Cook const char __user *buf) 6375bf6d1b9SMark Salyzyn { 63830800d99SKees Cook int ret = 0; 6395bf6d1b9SMark Salyzyn 64030800d99SKees Cook if (record->buf) 64130800d99SKees Cook return -EINVAL; 6425bf6d1b9SMark Salyzyn 64330800d99SKees Cook record->buf = kmalloc(record->size, GFP_KERNEL); 64430800d99SKees Cook if (!record->buf) 64530800d99SKees Cook return -ENOMEM; 64630800d99SKees Cook 64730800d99SKees Cook if (unlikely(copy_from_user(record->buf, buf, record->size))) { 6485bf6d1b9SMark Salyzyn ret = -EFAULT; 64930800d99SKees Cook goto out; 6505bf6d1b9SMark Salyzyn } 65130800d99SKees Cook 6524c9ec219SKees Cook ret = record->psi->write(record); 65330800d99SKees Cook 65430800d99SKees Cook out: 65530800d99SKees Cook kfree(record->buf); 65630800d99SKees Cook record->buf = NULL; 65730800d99SKees Cook 65830800d99SKees Cook return unlikely(ret < 0) ? ret : record->size; 6595bf6d1b9SMark Salyzyn } 6605bf6d1b9SMark Salyzyn 661ca01d6ddSTony Luck /* 662ca01d6ddSTony Luck * platform specific persistent storage driver registers with 663ca01d6ddSTony Luck * us here. If pstore is already mounted, call the platform 664ca01d6ddSTony Luck * read function right away to populate the file system. If not 665ca01d6ddSTony Luck * then the pstore mount code will call us later to fill out 666ca01d6ddSTony Luck * the file system. 667ca01d6ddSTony Luck */ 668ca01d6ddSTony Luck int pstore_register(struct pstore_info *psi) 669ca01d6ddSTony Luck { 670ca01d6ddSTony Luck struct module *owner = psi->owner; 671ca01d6ddSTony Luck 6720d7cd09aSKees Cook if (backend && strcmp(backend, psi->name)) { 6730d7cd09aSKees Cook pr_warn("ignoring unexpected backend '%s'\n", psi->name); 6748e48b1a8SLenny Szubowicz return -EPERM; 6750d7cd09aSKees Cook } 6768e48b1a8SLenny Szubowicz 6774c9ec219SKees Cook /* Sanity check flags. */ 6784c9ec219SKees Cook if (!psi->flags) { 6794c9ec219SKees Cook pr_warn("backend '%s' must support at least one frontend\n", 6804c9ec219SKees Cook psi->name); 6814c9ec219SKees Cook return -EINVAL; 6824c9ec219SKees Cook } 6834c9ec219SKees Cook 6844c9ec219SKees Cook /* Check for required functions. */ 6854c9ec219SKees Cook if (!psi->read || !psi->write) { 6864c9ec219SKees Cook pr_warn("backend '%s' must implement read() and write()\n", 6874c9ec219SKees Cook psi->name); 6884c9ec219SKees Cook return -EINVAL; 6894c9ec219SKees Cook } 6904c9ec219SKees Cook 691ca01d6ddSTony Luck spin_lock(&pstore_lock); 692ca01d6ddSTony Luck if (psinfo) { 6930d7cd09aSKees Cook pr_warn("backend '%s' already loaded: ignoring '%s'\n", 6940d7cd09aSKees Cook psinfo->name, psi->name); 695ca01d6ddSTony Luck spin_unlock(&pstore_lock); 696ca01d6ddSTony Luck return -EBUSY; 697ca01d6ddSTony Luck } 698dee28e72SMatthew Garrett 6994c9ec219SKees Cook if (!psi->write_user) 7004c9ec219SKees Cook psi->write_user = pstore_write_user_compat; 701ca01d6ddSTony Luck psinfo = psi; 702f6f82851SKees Cook mutex_init(&psinfo->read_mutex); 703ca01d6ddSTony Luck spin_unlock(&pstore_lock); 704ca01d6ddSTony Luck 705ca01d6ddSTony Luck if (owner && !try_module_get(owner)) { 706ca01d6ddSTony Luck psinfo = NULL; 707ca01d6ddSTony Luck return -EINVAL; 708ca01d6ddSTony Luck } 709ca01d6ddSTony Luck 710b0aad7a9SAruna Balakrishnaiah allocate_buf_for_compression(); 711b0aad7a9SAruna Balakrishnaiah 712ca01d6ddSTony Luck if (pstore_is_mounted()) 7136dda9266SLuck, Tony pstore_get_records(0); 714ca01d6ddSTony Luck 715c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_DMESG) 71618730411SGeliang Tang pstore_register_kmsg(); 717c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_CONSOLE) 718f29e5956SAnton Vorontsov pstore_register_console(); 719c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_FTRACE) 72065f8c95eSAnton Vorontsov pstore_register_ftrace(); 721c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_PMSG) 7229d5438f4SMark Salyzyn pstore_register_pmsg(); 723ca01d6ddSTony Luck 7246330d553SKees Cook /* Start watching for new records, if desired. */ 725a3f5f075SAnton Vorontsov if (pstore_update_ms >= 0) { 726a3f5f075SAnton Vorontsov pstore_timer.expires = jiffies + 727a3f5f075SAnton Vorontsov msecs_to_jiffies(pstore_update_ms); 7286dda9266SLuck, Tony add_timer(&pstore_timer); 729a3f5f075SAnton Vorontsov } 7306dda9266SLuck, Tony 73142222c2aSWang Long /* 73242222c2aSWang Long * Update the module parameter backend, so it is visible 73342222c2aSWang Long * through /sys/module/pstore/parameters/backend 73442222c2aSWang Long */ 73542222c2aSWang Long backend = psi->name; 73642222c2aSWang Long 737ef748853SFabian Frederick pr_info("Registered %s as persistent store backend\n", psi->name); 7388e48b1a8SLenny Szubowicz 7391344dd86SKees Cook module_put(owner); 7401344dd86SKees Cook 741ca01d6ddSTony Luck return 0; 742ca01d6ddSTony Luck } 743ca01d6ddSTony Luck EXPORT_SYMBOL_GPL(pstore_register); 744ca01d6ddSTony Luck 745ee1d2674SGeliang Tang void pstore_unregister(struct pstore_info *psi) 746ee1d2674SGeliang Tang { 7476330d553SKees Cook /* Stop timer and make sure all work has finished. */ 7486330d553SKees Cook pstore_update_ms = -1; 7496330d553SKees Cook del_timer_sync(&pstore_timer); 7506330d553SKees Cook flush_work(&pstore_work); 7516330d553SKees Cook 752c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_PMSG) 753ee1d2674SGeliang Tang pstore_unregister_pmsg(); 754c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_FTRACE) 755ee1d2674SGeliang Tang pstore_unregister_ftrace(); 756c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_CONSOLE) 757ee1d2674SGeliang Tang pstore_unregister_console(); 758c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_DMESG) 759ee1d2674SGeliang Tang pstore_unregister_kmsg(); 760ee1d2674SGeliang Tang 761ee1d2674SGeliang Tang free_buf_for_compression(); 762ee1d2674SGeliang Tang 763ee1d2674SGeliang Tang psinfo = NULL; 764ee1d2674SGeliang Tang backend = NULL; 765ee1d2674SGeliang Tang } 766ee1d2674SGeliang Tang EXPORT_SYMBOL_GPL(pstore_unregister); 767ee1d2674SGeliang Tang 768634f8f51SKees Cook static void decompress_record(struct pstore_record *record) 769634f8f51SKees Cook { 770634f8f51SKees Cook int unzipped_len; 7717e8cc8dcSKees Cook char *decompressed; 772634f8f51SKees Cook 773*4a16d1cbSAnkit Kumar if (!record->compressed) 774*4a16d1cbSAnkit Kumar return; 775*4a16d1cbSAnkit Kumar 776634f8f51SKees Cook /* Only PSTORE_TYPE_DMESG support compression. */ 777*4a16d1cbSAnkit Kumar if (record->type != PSTORE_TYPE_DMESG) { 778634f8f51SKees Cook pr_warn("ignored compressed record type %d\n", record->type); 779634f8f51SKees Cook return; 780634f8f51SKees Cook } 781634f8f51SKees Cook 782634f8f51SKees Cook /* No compression method has created the common buffer. */ 783634f8f51SKees Cook if (!big_oops_buf) { 784634f8f51SKees Cook pr_warn("no decompression buffer allocated\n"); 785634f8f51SKees Cook return; 786634f8f51SKees Cook } 787634f8f51SKees Cook 788634f8f51SKees Cook unzipped_len = pstore_decompress(record->buf, big_oops_buf, 789634f8f51SKees Cook record->size, big_oops_buf_sz); 7907e8cc8dcSKees Cook if (unzipped_len <= 0) { 7917e8cc8dcSKees Cook pr_err("decompression failed: %d\n", unzipped_len); 7927e8cc8dcSKees Cook return; 7937e8cc8dcSKees Cook } 7947e8cc8dcSKees Cook 7957e8cc8dcSKees Cook /* Build new buffer for decompressed contents. */ 7967e8cc8dcSKees Cook decompressed = kmalloc(unzipped_len + record->ecc_notice_size, 7977e8cc8dcSKees Cook GFP_KERNEL); 7987e8cc8dcSKees Cook if (!decompressed) { 7997e8cc8dcSKees Cook pr_err("decompression ran out of memory\n"); 8007e8cc8dcSKees Cook return; 8017e8cc8dcSKees Cook } 8027e8cc8dcSKees Cook memcpy(decompressed, big_oops_buf, unzipped_len); 8037e8cc8dcSKees Cook 8047e8cc8dcSKees Cook /* Append ECC notice to decompressed buffer. */ 8057e8cc8dcSKees Cook memcpy(decompressed + unzipped_len, record->buf + record->size, 806634f8f51SKees Cook record->ecc_notice_size); 8077e8cc8dcSKees Cook 8087e8cc8dcSKees Cook /* Swap out compresed contents with decompressed contents. */ 809634f8f51SKees Cook kfree(record->buf); 8107e8cc8dcSKees Cook record->buf = decompressed; 811634f8f51SKees Cook record->size = unzipped_len; 812634f8f51SKees Cook record->compressed = false; 813634f8f51SKees Cook } 814634f8f51SKees Cook 815ca01d6ddSTony Luck /* 8163a7d2fd1SKees Cook * Read all the records from one persistent store backend. Create 8176dda9266SLuck, Tony * files in our filesystem. Don't warn about -EEXIST errors 8186dda9266SLuck, Tony * when we are re-scanning the backing store looking to add new 8196dda9266SLuck, Tony * error records. 820ca01d6ddSTony Luck */ 8213a7d2fd1SKees Cook void pstore_get_backend_records(struct pstore_info *psi, 8223a7d2fd1SKees Cook struct dentry *root, int quiet) 823ca01d6ddSTony Luck { 8242a2b0acfSKees Cook int failed = 0; 825ca01d6ddSTony Luck 8263a7d2fd1SKees Cook if (!psi || !root) 827ca01d6ddSTony Luck return; 828ca01d6ddSTony Luck 829f6f82851SKees Cook mutex_lock(&psi->read_mutex); 8302174f6dfSKees Cook if (psi->open && psi->open(psi)) 83106cf91b4SChen Gong goto out; 83206cf91b4SChen Gong 8331dfff7ddSKees Cook /* 8341dfff7ddSKees Cook * Backend callback read() allocates record.buf. decompress_record() 8351dfff7ddSKees Cook * may reallocate record.buf. On success, pstore_mkfile() will keep 8361dfff7ddSKees Cook * the record.buf, so free it only on failure. 8371dfff7ddSKees Cook */ 8382a2b0acfSKees Cook for (;;) { 8392a2b0acfSKees Cook struct pstore_record *record; 8402a2b0acfSKees Cook int rc; 8412a2b0acfSKees Cook 8422a2b0acfSKees Cook record = kzalloc(sizeof(*record), GFP_KERNEL); 8432a2b0acfSKees Cook if (!record) { 8442a2b0acfSKees Cook pr_err("out of memory creating record\n"); 8452a2b0acfSKees Cook break; 8462a2b0acfSKees Cook } 8472a2b0acfSKees Cook record->psi = psi; 8482a2b0acfSKees Cook 8492a2b0acfSKees Cook record->size = psi->read(record); 8502a2b0acfSKees Cook 8512a2b0acfSKees Cook /* No more records left in backend? */ 8522a2b0acfSKees Cook if (record->size <= 0) 8532a2b0acfSKees Cook break; 8542a2b0acfSKees Cook 8552a2b0acfSKees Cook decompress_record(record); 8563a7d2fd1SKees Cook rc = pstore_mkfile(root, record); 8571dfff7ddSKees Cook if (rc) { 85883f70f07SKees Cook /* pstore_mkfile() did not take record, so free it. */ 8592a2b0acfSKees Cook kfree(record->buf); 86083f70f07SKees Cook kfree(record); 8611dfff7ddSKees Cook if (rc != -EEXIST || !quiet) 8621dfff7ddSKees Cook failed++; 8631dfff7ddSKees Cook } 864ca01d6ddSTony Luck } 8652174f6dfSKees Cook if (psi->close) 86606cf91b4SChen Gong psi->close(psi); 86706cf91b4SChen Gong out: 868f6f82851SKees Cook mutex_unlock(&psi->read_mutex); 869ca01d6ddSTony Luck 870ca01d6ddSTony Luck if (failed) 871ef748853SFabian Frederick pr_warn("failed to load %d record(s) from '%s'\n", 872ca01d6ddSTony Luck failed, psi->name); 873ca01d6ddSTony Luck } 874ca01d6ddSTony Luck 8756dda9266SLuck, Tony static void pstore_dowork(struct work_struct *work) 8766dda9266SLuck, Tony { 8776dda9266SLuck, Tony pstore_get_records(1); 8786dda9266SLuck, Tony } 8796dda9266SLuck, Tony 8806dda9266SLuck, Tony static void pstore_timefunc(unsigned long dummy) 8816dda9266SLuck, Tony { 8826dda9266SLuck, Tony if (pstore_new_entry) { 8836dda9266SLuck, Tony pstore_new_entry = 0; 8846dda9266SLuck, Tony schedule_work(&pstore_work); 8856dda9266SLuck, Tony } 8866dda9266SLuck, Tony 8876330d553SKees Cook if (pstore_update_ms >= 0) 8886330d553SKees Cook mod_timer(&pstore_timer, 8896330d553SKees Cook jiffies + msecs_to_jiffies(pstore_update_ms)); 8906dda9266SLuck, Tony } 8916dda9266SLuck, Tony 892dee28e72SMatthew Garrett module_param(backend, charp, 0444); 893dee28e72SMatthew Garrett MODULE_PARM_DESC(backend, "Pstore backend to use"); 894