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> 31b0aad7a9SAruna Balakrishnaiah #include <linux/zlib.h> 32ca01d6ddSTony Luck #include <linux/string.h> 336dda9266SLuck, Tony #include <linux/timer.h> 34ca01d6ddSTony Luck #include <linux/slab.h> 35ca01d6ddSTony Luck #include <linux/uaccess.h> 36abd4d558SDon Zickus #include <linux/hardirq.h> 37a3f5f075SAnton Vorontsov #include <linux/jiffies.h> 386dda9266SLuck, Tony #include <linux/workqueue.h> 39ca01d6ddSTony Luck 40ca01d6ddSTony Luck #include "internal.h" 41ca01d6ddSTony Luck 42ca01d6ddSTony Luck /* 436dda9266SLuck, Tony * We defer making "oops" entries appear in pstore - see 446dda9266SLuck, Tony * whether the system is actually still running well enough 456dda9266SLuck, Tony * to let someone see the entry 466dda9266SLuck, Tony */ 47521f7288SAnton Vorontsov static int pstore_update_ms = -1; 48a3f5f075SAnton Vorontsov module_param_named(update_ms, pstore_update_ms, int, 0600); 49a3f5f075SAnton Vorontsov MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content " 50521f7288SAnton Vorontsov "(default is -1, which means runtime updates are disabled; " 51521f7288SAnton Vorontsov "enabling this option is not safe, it may lead to further " 52521f7288SAnton Vorontsov "corruption on Oopses)"); 536dda9266SLuck, Tony 546dda9266SLuck, Tony static int pstore_new_entry; 556dda9266SLuck, Tony 566dda9266SLuck, Tony static void pstore_timefunc(unsigned long); 576dda9266SLuck, Tony static DEFINE_TIMER(pstore_timer, pstore_timefunc, 0, 0); 586dda9266SLuck, Tony 596dda9266SLuck, Tony static void pstore_dowork(struct work_struct *); 606dda9266SLuck, Tony static DECLARE_WORK(pstore_work, pstore_dowork); 616dda9266SLuck, Tony 626dda9266SLuck, Tony /* 63ca01d6ddSTony Luck * pstore_lock just protects "psinfo" during 64ca01d6ddSTony Luck * calls to pstore_register() 65ca01d6ddSTony Luck */ 66ca01d6ddSTony Luck static DEFINE_SPINLOCK(pstore_lock); 67060287b8SAnton Vorontsov struct pstore_info *psinfo; 68ca01d6ddSTony Luck 69dee28e72SMatthew Garrett static char *backend; 70dee28e72SMatthew Garrett 71b0aad7a9SAruna Balakrishnaiah /* Compression parameters */ 72b0aad7a9SAruna Balakrishnaiah #define COMPR_LEVEL 6 73b0aad7a9SAruna Balakrishnaiah #define WINDOW_BITS 12 74b0aad7a9SAruna Balakrishnaiah #define MEM_LEVEL 4 75b0aad7a9SAruna Balakrishnaiah static struct z_stream_s stream; 76b0aad7a9SAruna Balakrishnaiah 77b0aad7a9SAruna Balakrishnaiah static char *big_oops_buf; 78b0aad7a9SAruna Balakrishnaiah static size_t big_oops_buf_sz; 79b0aad7a9SAruna Balakrishnaiah 80366f7e7aSLuck, Tony /* How much of the console log to snapshot */ 81ca01d6ddSTony Luck static unsigned long kmsg_bytes = 10240; 82ca01d6ddSTony Luck 83366f7e7aSLuck, Tony void pstore_set_kmsg_bytes(int bytes) 84ca01d6ddSTony Luck { 85366f7e7aSLuck, Tony kmsg_bytes = bytes; 86ca01d6ddSTony Luck } 87ca01d6ddSTony Luck 88ca01d6ddSTony Luck /* Tag each group of saved records with a sequence number */ 89ca01d6ddSTony Luck static int oopscount; 90ca01d6ddSTony Luck 91381b872cSSeiji Aguchi static const char *get_reason_str(enum kmsg_dump_reason reason) 92381b872cSSeiji Aguchi { 93381b872cSSeiji Aguchi switch (reason) { 94381b872cSSeiji Aguchi case KMSG_DUMP_PANIC: 95381b872cSSeiji Aguchi return "Panic"; 96381b872cSSeiji Aguchi case KMSG_DUMP_OOPS: 97381b872cSSeiji Aguchi return "Oops"; 98381b872cSSeiji Aguchi case KMSG_DUMP_EMERG: 99381b872cSSeiji Aguchi return "Emergency"; 100381b872cSSeiji Aguchi case KMSG_DUMP_RESTART: 101381b872cSSeiji Aguchi return "Restart"; 102381b872cSSeiji Aguchi case KMSG_DUMP_HALT: 103381b872cSSeiji Aguchi return "Halt"; 104381b872cSSeiji Aguchi case KMSG_DUMP_POWEROFF: 105381b872cSSeiji Aguchi return "Poweroff"; 106381b872cSSeiji Aguchi default: 107381b872cSSeiji Aguchi return "Unknown"; 108381b872cSSeiji Aguchi } 109381b872cSSeiji Aguchi } 1109f6af27fSTony Luck 1119f244e9cSSeiji Aguchi bool pstore_cannot_block_path(enum kmsg_dump_reason reason) 1129f244e9cSSeiji Aguchi { 1139f244e9cSSeiji Aguchi /* 1149f244e9cSSeiji Aguchi * In case of NMI path, pstore shouldn't be blocked 1159f244e9cSSeiji Aguchi * regardless of reason. 1169f244e9cSSeiji Aguchi */ 1179f244e9cSSeiji Aguchi if (in_nmi()) 1189f244e9cSSeiji Aguchi return true; 1199f244e9cSSeiji Aguchi 1209f244e9cSSeiji Aguchi switch (reason) { 1219f244e9cSSeiji Aguchi /* In panic case, other cpus are stopped by smp_send_stop(). */ 1229f244e9cSSeiji Aguchi case KMSG_DUMP_PANIC: 1239f244e9cSSeiji Aguchi /* Emergency restart shouldn't be blocked by spin lock. */ 1249f244e9cSSeiji Aguchi case KMSG_DUMP_EMERG: 1259f244e9cSSeiji Aguchi return true; 1269f244e9cSSeiji Aguchi default: 1279f244e9cSSeiji Aguchi return false; 1289f244e9cSSeiji Aguchi } 1299f244e9cSSeiji Aguchi } 1309f244e9cSSeiji Aguchi EXPORT_SYMBOL_GPL(pstore_cannot_block_path); 1319f244e9cSSeiji Aguchi 132b0aad7a9SAruna Balakrishnaiah /* Derived from logfs_compress() */ 133b0aad7a9SAruna Balakrishnaiah static int pstore_compress(const void *in, void *out, size_t inlen, 134b0aad7a9SAruna Balakrishnaiah size_t outlen) 135b0aad7a9SAruna Balakrishnaiah { 136b0aad7a9SAruna Balakrishnaiah int err, ret; 137b0aad7a9SAruna Balakrishnaiah 138b0aad7a9SAruna Balakrishnaiah ret = -EIO; 139b0aad7a9SAruna Balakrishnaiah err = zlib_deflateInit2(&stream, COMPR_LEVEL, Z_DEFLATED, WINDOW_BITS, 140b0aad7a9SAruna Balakrishnaiah MEM_LEVEL, Z_DEFAULT_STRATEGY); 141b0aad7a9SAruna Balakrishnaiah if (err != Z_OK) 142b0aad7a9SAruna Balakrishnaiah goto error; 143b0aad7a9SAruna Balakrishnaiah 144b0aad7a9SAruna Balakrishnaiah stream.next_in = in; 145b0aad7a9SAruna Balakrishnaiah stream.avail_in = inlen; 146b0aad7a9SAruna Balakrishnaiah stream.total_in = 0; 147b0aad7a9SAruna Balakrishnaiah stream.next_out = out; 148b0aad7a9SAruna Balakrishnaiah stream.avail_out = outlen; 149b0aad7a9SAruna Balakrishnaiah stream.total_out = 0; 150b0aad7a9SAruna Balakrishnaiah 151b0aad7a9SAruna Balakrishnaiah err = zlib_deflate(&stream, Z_FINISH); 152b0aad7a9SAruna Balakrishnaiah if (err != Z_STREAM_END) 153b0aad7a9SAruna Balakrishnaiah goto error; 154b0aad7a9SAruna Balakrishnaiah 155b0aad7a9SAruna Balakrishnaiah err = zlib_deflateEnd(&stream); 156b0aad7a9SAruna Balakrishnaiah if (err != Z_OK) 157b0aad7a9SAruna Balakrishnaiah goto error; 158b0aad7a9SAruna Balakrishnaiah 159b0aad7a9SAruna Balakrishnaiah if (stream.total_out >= stream.total_in) 160b0aad7a9SAruna Balakrishnaiah goto error; 161b0aad7a9SAruna Balakrishnaiah 162b0aad7a9SAruna Balakrishnaiah ret = stream.total_out; 163b0aad7a9SAruna Balakrishnaiah error: 164b0aad7a9SAruna Balakrishnaiah return ret; 165b0aad7a9SAruna Balakrishnaiah } 166b0aad7a9SAruna Balakrishnaiah 167adb42f5eSAruna Balakrishnaiah /* Derived from logfs_uncompress */ 168adb42f5eSAruna Balakrishnaiah static int pstore_decompress(void *in, void *out, size_t inlen, size_t outlen) 169adb42f5eSAruna Balakrishnaiah { 170adb42f5eSAruna Balakrishnaiah int err, ret; 171adb42f5eSAruna Balakrishnaiah 172adb42f5eSAruna Balakrishnaiah ret = -EIO; 173b61edf8eSAruna Balakrishnaiah err = zlib_inflateInit2(&stream, WINDOW_BITS); 174adb42f5eSAruna Balakrishnaiah if (err != Z_OK) 175adb42f5eSAruna Balakrishnaiah goto error; 176adb42f5eSAruna Balakrishnaiah 177adb42f5eSAruna Balakrishnaiah stream.next_in = in; 178adb42f5eSAruna Balakrishnaiah stream.avail_in = inlen; 179adb42f5eSAruna Balakrishnaiah stream.total_in = 0; 180adb42f5eSAruna Balakrishnaiah stream.next_out = out; 181adb42f5eSAruna Balakrishnaiah stream.avail_out = outlen; 182adb42f5eSAruna Balakrishnaiah stream.total_out = 0; 183adb42f5eSAruna Balakrishnaiah 184adb42f5eSAruna Balakrishnaiah err = zlib_inflate(&stream, Z_FINISH); 185adb42f5eSAruna Balakrishnaiah if (err != Z_STREAM_END) 186adb42f5eSAruna Balakrishnaiah goto error; 187adb42f5eSAruna Balakrishnaiah 188adb42f5eSAruna Balakrishnaiah err = zlib_inflateEnd(&stream); 189adb42f5eSAruna Balakrishnaiah if (err != Z_OK) 190adb42f5eSAruna Balakrishnaiah goto error; 191adb42f5eSAruna Balakrishnaiah 192adb42f5eSAruna Balakrishnaiah ret = stream.total_out; 193adb42f5eSAruna Balakrishnaiah error: 194adb42f5eSAruna Balakrishnaiah return ret; 195adb42f5eSAruna Balakrishnaiah } 196adb42f5eSAruna Balakrishnaiah 197b0aad7a9SAruna Balakrishnaiah static void allocate_buf_for_compression(void) 198b0aad7a9SAruna Balakrishnaiah { 199b0aad7a9SAruna Balakrishnaiah size_t size; 2007de8fe2fSAruna Balakrishnaiah size_t cmpr; 201b0aad7a9SAruna Balakrishnaiah 2027de8fe2fSAruna Balakrishnaiah switch (psinfo->bufsize) { 2037de8fe2fSAruna Balakrishnaiah /* buffer range for efivars */ 2047de8fe2fSAruna Balakrishnaiah case 1000 ... 2000: 2057de8fe2fSAruna Balakrishnaiah cmpr = 56; 2067de8fe2fSAruna Balakrishnaiah break; 2077de8fe2fSAruna Balakrishnaiah case 2001 ... 3000: 2087de8fe2fSAruna Balakrishnaiah cmpr = 54; 2097de8fe2fSAruna Balakrishnaiah break; 2107de8fe2fSAruna Balakrishnaiah case 3001 ... 3999: 2117de8fe2fSAruna Balakrishnaiah cmpr = 52; 2127de8fe2fSAruna Balakrishnaiah break; 2137de8fe2fSAruna Balakrishnaiah /* buffer range for nvram, erst */ 2147de8fe2fSAruna Balakrishnaiah case 4000 ... 10000: 2157de8fe2fSAruna Balakrishnaiah cmpr = 45; 2167de8fe2fSAruna Balakrishnaiah break; 2177de8fe2fSAruna Balakrishnaiah default: 2187de8fe2fSAruna Balakrishnaiah cmpr = 60; 2197de8fe2fSAruna Balakrishnaiah break; 2207de8fe2fSAruna Balakrishnaiah } 2217de8fe2fSAruna Balakrishnaiah 2227de8fe2fSAruna Balakrishnaiah big_oops_buf_sz = (psinfo->bufsize * 100) / cmpr; 223b0aad7a9SAruna Balakrishnaiah big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL); 224b0aad7a9SAruna Balakrishnaiah if (big_oops_buf) { 225b0aad7a9SAruna Balakrishnaiah size = max(zlib_deflate_workspacesize(WINDOW_BITS, MEM_LEVEL), 226b0aad7a9SAruna Balakrishnaiah zlib_inflate_workspacesize()); 227b0aad7a9SAruna Balakrishnaiah stream.workspace = kmalloc(size, GFP_KERNEL); 228b0aad7a9SAruna Balakrishnaiah if (!stream.workspace) { 229ef748853SFabian Frederick pr_err("No memory for compression workspace; skipping compression\n"); 230b0aad7a9SAruna Balakrishnaiah kfree(big_oops_buf); 231b0aad7a9SAruna Balakrishnaiah big_oops_buf = NULL; 232b0aad7a9SAruna Balakrishnaiah } 233b0aad7a9SAruna Balakrishnaiah } else { 234ef748853SFabian Frederick pr_err("No memory for uncompressed data; skipping compression\n"); 235b0aad7a9SAruna Balakrishnaiah stream.workspace = NULL; 236b0aad7a9SAruna Balakrishnaiah } 237b0aad7a9SAruna Balakrishnaiah 238b0aad7a9SAruna Balakrishnaiah } 239b0aad7a9SAruna Balakrishnaiah 240ee1d2674SGeliang Tang static void free_buf_for_compression(void) 241ee1d2674SGeliang Tang { 242ee1d2674SGeliang Tang kfree(stream.workspace); 243ee1d2674SGeliang Tang stream.workspace = NULL; 244ee1d2674SGeliang Tang kfree(big_oops_buf); 245ee1d2674SGeliang Tang big_oops_buf = NULL; 246ee1d2674SGeliang Tang } 247ee1d2674SGeliang Tang 248b0aad7a9SAruna Balakrishnaiah /* 249b0aad7a9SAruna Balakrishnaiah * Called when compression fails, since the printk buffer 250b0aad7a9SAruna Balakrishnaiah * would be fetched for compression calling it again when 251b0aad7a9SAruna Balakrishnaiah * compression fails would have moved the iterator of 252b0aad7a9SAruna Balakrishnaiah * printk buffer which results in fetching old contents. 253b0aad7a9SAruna Balakrishnaiah * Copy the recent messages from big_oops_buf to psinfo->buf 254b0aad7a9SAruna Balakrishnaiah */ 255b0aad7a9SAruna Balakrishnaiah static size_t copy_kmsg_to_buffer(int hsize, size_t len) 256b0aad7a9SAruna Balakrishnaiah { 257b0aad7a9SAruna Balakrishnaiah size_t total_len; 258b0aad7a9SAruna Balakrishnaiah size_t diff; 259b0aad7a9SAruna Balakrishnaiah 260b0aad7a9SAruna Balakrishnaiah total_len = hsize + len; 261b0aad7a9SAruna Balakrishnaiah 262b0aad7a9SAruna Balakrishnaiah if (total_len > psinfo->bufsize) { 263b0aad7a9SAruna Balakrishnaiah diff = total_len - psinfo->bufsize + hsize; 264b0aad7a9SAruna Balakrishnaiah memcpy(psinfo->buf, big_oops_buf, hsize); 265b0aad7a9SAruna Balakrishnaiah memcpy(psinfo->buf + hsize, big_oops_buf + diff, 266b0aad7a9SAruna Balakrishnaiah psinfo->bufsize - hsize); 267b0aad7a9SAruna Balakrishnaiah total_len = psinfo->bufsize; 268b0aad7a9SAruna Balakrishnaiah } else 269b0aad7a9SAruna Balakrishnaiah memcpy(psinfo->buf, big_oops_buf, total_len); 270b0aad7a9SAruna Balakrishnaiah 271b0aad7a9SAruna Balakrishnaiah return total_len; 272b0aad7a9SAruna Balakrishnaiah } 273b0aad7a9SAruna Balakrishnaiah 274ca01d6ddSTony Luck /* 275ca01d6ddSTony Luck * callback from kmsg_dump. (s2,l2) has the most recently 276ca01d6ddSTony Luck * written bytes, older bytes are in (s1,l1). Save as much 277ca01d6ddSTony Luck * as we can from the end of the buffer. 278ca01d6ddSTony Luck */ 279ca01d6ddSTony Luck static void pstore_dump(struct kmsg_dumper *dumper, 280e2ae715dSKay Sievers enum kmsg_dump_reason reason) 281ca01d6ddSTony Luck { 282e2ae715dSKay Sievers unsigned long total = 0; 283381b872cSSeiji Aguchi const char *why; 284ca01d6ddSTony Luck u64 id; 285b94fdd07SMatthew Garrett unsigned int part = 1; 286abd4d558SDon Zickus unsigned long flags = 0; 287*98e44fdaSNamhyung Kim int is_locked; 288e2ae715dSKay Sievers int ret; 289ca01d6ddSTony Luck 290381b872cSSeiji Aguchi why = get_reason_str(reason); 2919f6af27fSTony Luck 2929f244e9cSSeiji Aguchi if (pstore_cannot_block_path(reason)) { 2939f244e9cSSeiji Aguchi is_locked = spin_trylock_irqsave(&psinfo->buf_lock, flags); 2949f244e9cSSeiji Aguchi if (!is_locked) { 2959f244e9cSSeiji Aguchi pr_err("pstore dump routine blocked in %s path, may corrupt error record\n" 2969f244e9cSSeiji Aguchi , in_nmi() ? "NMI" : why); 2979f244e9cSSeiji Aguchi } 298*98e44fdaSNamhyung Kim } else { 299abd4d558SDon Zickus spin_lock_irqsave(&psinfo->buf_lock, flags); 300*98e44fdaSNamhyung Kim is_locked = 1; 301*98e44fdaSNamhyung Kim } 302ca01d6ddSTony Luck oopscount++; 303ca01d6ddSTony Luck while (total < kmsg_bytes) { 304e2ae715dSKay Sievers char *dst; 305e2ae715dSKay Sievers unsigned long size; 306e2ae715dSKay Sievers int hsize; 307b0aad7a9SAruna Balakrishnaiah int zipped_len = -1; 308e2ae715dSKay Sievers size_t len; 309b0aad7a9SAruna Balakrishnaiah bool compressed; 310b0aad7a9SAruna Balakrishnaiah size_t total_len; 311e2ae715dSKay Sievers 312f0e2efcfSKonstantin Khlebnikov if (big_oops_buf && is_locked) { 313b0aad7a9SAruna Balakrishnaiah dst = big_oops_buf; 314a6b8978cSalex chen hsize = sprintf(dst, "%s#%d Part%u\n", why, 315b0aad7a9SAruna Balakrishnaiah oopscount, part); 316b0aad7a9SAruna Balakrishnaiah size = big_oops_buf_sz - hsize; 317b0aad7a9SAruna Balakrishnaiah 318b0aad7a9SAruna Balakrishnaiah if (!kmsg_dump_get_buffer(dumper, true, dst + hsize, 319b0aad7a9SAruna Balakrishnaiah size, &len)) 320b0aad7a9SAruna Balakrishnaiah break; 321b0aad7a9SAruna Balakrishnaiah 322b0aad7a9SAruna Balakrishnaiah zipped_len = pstore_compress(dst, psinfo->buf, 323b0aad7a9SAruna Balakrishnaiah hsize + len, psinfo->bufsize); 324b0aad7a9SAruna Balakrishnaiah 325b0aad7a9SAruna Balakrishnaiah if (zipped_len > 0) { 326b0aad7a9SAruna Balakrishnaiah compressed = true; 327b0aad7a9SAruna Balakrishnaiah total_len = zipped_len; 328b0aad7a9SAruna Balakrishnaiah } else { 329b0aad7a9SAruna Balakrishnaiah compressed = false; 330b0aad7a9SAruna Balakrishnaiah total_len = copy_kmsg_to_buffer(hsize, len); 331b0aad7a9SAruna Balakrishnaiah } 332b0aad7a9SAruna Balakrishnaiah } else { 333ca01d6ddSTony Luck dst = psinfo->buf; 334a6b8978cSalex chen hsize = sprintf(dst, "%s#%d Part%u\n", why, oopscount, 335b0aad7a9SAruna Balakrishnaiah part); 336ca01d6ddSTony Luck size = psinfo->bufsize - hsize; 337ca01d6ddSTony Luck dst += hsize; 338ca01d6ddSTony Luck 339b0aad7a9SAruna Balakrishnaiah if (!kmsg_dump_get_buffer(dumper, true, dst, 340b0aad7a9SAruna Balakrishnaiah size, &len)) 341ca01d6ddSTony Luck break; 342ca01d6ddSTony Luck 343b0aad7a9SAruna Balakrishnaiah compressed = false; 344b0aad7a9SAruna Balakrishnaiah total_len = hsize + len; 345b0aad7a9SAruna Balakrishnaiah } 346b0aad7a9SAruna Balakrishnaiah 3473d6d8d20SKees Cook ret = psinfo->write(PSTORE_TYPE_DMESG, reason, &id, part, 348b0aad7a9SAruna Balakrishnaiah oopscount, compressed, total_len, psinfo); 349b238b8faSChen Gong if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted()) 3506dda9266SLuck, Tony pstore_new_entry = 1; 351e2ae715dSKay Sievers 352b0aad7a9SAruna Balakrishnaiah total += total_len; 35356280682SMatthew Garrett part++; 354ca01d6ddSTony Luck } 355abd4d558SDon Zickus if (is_locked) 3569f244e9cSSeiji Aguchi spin_unlock_irqrestore(&psinfo->buf_lock, flags); 357ca01d6ddSTony Luck } 358ca01d6ddSTony Luck 359ca01d6ddSTony Luck static struct kmsg_dumper pstore_dumper = { 360ca01d6ddSTony Luck .dump = pstore_dump, 361ca01d6ddSTony Luck }; 362ca01d6ddSTony Luck 363306e5c2aSGeliang Tang /* 364306e5c2aSGeliang Tang * Register with kmsg_dump to save last part of console log on panic. 365306e5c2aSGeliang Tang */ 36618730411SGeliang Tang static void pstore_register_kmsg(void) 36718730411SGeliang Tang { 36818730411SGeliang Tang kmsg_dump_register(&pstore_dumper); 36918730411SGeliang Tang } 37018730411SGeliang Tang 371ee1d2674SGeliang Tang static void pstore_unregister_kmsg(void) 372ee1d2674SGeliang Tang { 373ee1d2674SGeliang Tang kmsg_dump_unregister(&pstore_dumper); 374ee1d2674SGeliang Tang } 375ee1d2674SGeliang Tang 376f29e5956SAnton Vorontsov #ifdef CONFIG_PSTORE_CONSOLE 377f29e5956SAnton Vorontsov static void pstore_console_write(struct console *con, const char *s, unsigned c) 378f29e5956SAnton Vorontsov { 379f29e5956SAnton Vorontsov const char *e = s + c; 380f29e5956SAnton Vorontsov 381f29e5956SAnton Vorontsov while (s < e) { 382f29e5956SAnton Vorontsov unsigned long flags; 38370a6f46dSColin Ian King u64 id; 384f29e5956SAnton Vorontsov 385f29e5956SAnton Vorontsov if (c > psinfo->bufsize) 386f29e5956SAnton Vorontsov c = psinfo->bufsize; 38780c9d03cSChuansheng Liu 38880c9d03cSChuansheng Liu if (oops_in_progress) { 38980c9d03cSChuansheng Liu if (!spin_trylock_irqsave(&psinfo->buf_lock, flags)) 39080c9d03cSChuansheng Liu break; 39180c9d03cSChuansheng Liu } else { 392f29e5956SAnton Vorontsov spin_lock_irqsave(&psinfo->buf_lock, flags); 39380c9d03cSChuansheng Liu } 394f29e5956SAnton Vorontsov memcpy(psinfo->buf, s, c); 3956bbbca73SAruna Balakrishnaiah psinfo->write(PSTORE_TYPE_CONSOLE, 0, &id, 0, 0, 0, c, psinfo); 396f29e5956SAnton Vorontsov spin_unlock_irqrestore(&psinfo->buf_lock, flags); 397f29e5956SAnton Vorontsov s += c; 398f29e5956SAnton Vorontsov c = e - s; 399f29e5956SAnton Vorontsov } 400f29e5956SAnton Vorontsov } 401f29e5956SAnton Vorontsov 402f29e5956SAnton Vorontsov static struct console pstore_console = { 403f29e5956SAnton Vorontsov .name = "pstore", 404f29e5956SAnton Vorontsov .write = pstore_console_write, 405f29e5956SAnton Vorontsov .flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME, 406f29e5956SAnton Vorontsov .index = -1, 407f29e5956SAnton Vorontsov }; 408f29e5956SAnton Vorontsov 409f29e5956SAnton Vorontsov static void pstore_register_console(void) 410f29e5956SAnton Vorontsov { 411f29e5956SAnton Vorontsov register_console(&pstore_console); 412f29e5956SAnton Vorontsov } 413ee1d2674SGeliang Tang 414ee1d2674SGeliang Tang static void pstore_unregister_console(void) 415ee1d2674SGeliang Tang { 416ee1d2674SGeliang Tang unregister_console(&pstore_console); 417ee1d2674SGeliang Tang } 418f29e5956SAnton Vorontsov #else 419f29e5956SAnton Vorontsov static void pstore_register_console(void) {} 420ee1d2674SGeliang Tang static void pstore_unregister_console(void) {} 421f29e5956SAnton Vorontsov #endif 422f29e5956SAnton Vorontsov 423897dba02SAnton Vorontsov static int pstore_write_compat(enum pstore_type_id type, 424897dba02SAnton Vorontsov enum kmsg_dump_reason reason, 425755d4fe4SSeiji Aguchi u64 *id, unsigned int part, int count, 426b3b515bbSAruna Balakrishnaiah bool compressed, size_t size, 4276bbbca73SAruna Balakrishnaiah struct pstore_info *psi) 428897dba02SAnton Vorontsov { 429b3b515bbSAruna Balakrishnaiah return psi->write_buf(type, reason, id, part, psinfo->buf, compressed, 4306bbbca73SAruna Balakrishnaiah size, psi); 431897dba02SAnton Vorontsov } 432897dba02SAnton Vorontsov 433ca01d6ddSTony Luck /* 434ca01d6ddSTony Luck * platform specific persistent storage driver registers with 435ca01d6ddSTony Luck * us here. If pstore is already mounted, call the platform 436ca01d6ddSTony Luck * read function right away to populate the file system. If not 437ca01d6ddSTony Luck * then the pstore mount code will call us later to fill out 438ca01d6ddSTony Luck * the file system. 439ca01d6ddSTony Luck */ 440ca01d6ddSTony Luck int pstore_register(struct pstore_info *psi) 441ca01d6ddSTony Luck { 442ca01d6ddSTony Luck struct module *owner = psi->owner; 443ca01d6ddSTony Luck 4448e48b1a8SLenny Szubowicz if (backend && strcmp(backend, psi->name)) 4458e48b1a8SLenny Szubowicz return -EPERM; 4468e48b1a8SLenny Szubowicz 447ca01d6ddSTony Luck spin_lock(&pstore_lock); 448ca01d6ddSTony Luck if (psinfo) { 449ca01d6ddSTony Luck spin_unlock(&pstore_lock); 450ca01d6ddSTony Luck return -EBUSY; 451ca01d6ddSTony Luck } 452dee28e72SMatthew Garrett 453897dba02SAnton Vorontsov if (!psi->write) 454897dba02SAnton Vorontsov psi->write = pstore_write_compat; 455ca01d6ddSTony Luck psinfo = psi; 456f6f82851SKees Cook mutex_init(&psinfo->read_mutex); 457ca01d6ddSTony Luck spin_unlock(&pstore_lock); 458ca01d6ddSTony Luck 459ca01d6ddSTony Luck if (owner && !try_module_get(owner)) { 460ca01d6ddSTony Luck psinfo = NULL; 461ca01d6ddSTony Luck return -EINVAL; 462ca01d6ddSTony Luck } 463ca01d6ddSTony Luck 464b0aad7a9SAruna Balakrishnaiah allocate_buf_for_compression(); 465b0aad7a9SAruna Balakrishnaiah 466ca01d6ddSTony Luck if (pstore_is_mounted()) 4676dda9266SLuck, Tony pstore_get_records(0); 468ca01d6ddSTony Luck 46918730411SGeliang Tang pstore_register_kmsg(); 470df36ac1bSLuck, Tony 471df36ac1bSLuck, Tony if ((psi->flags & PSTORE_FLAGS_FRAGILE) == 0) { 472f29e5956SAnton Vorontsov pstore_register_console(); 47365f8c95eSAnton Vorontsov pstore_register_ftrace(); 4749d5438f4SMark Salyzyn pstore_register_pmsg(); 475df36ac1bSLuck, Tony } 476ca01d6ddSTony Luck 477a3f5f075SAnton Vorontsov if (pstore_update_ms >= 0) { 478a3f5f075SAnton Vorontsov pstore_timer.expires = jiffies + 479a3f5f075SAnton Vorontsov msecs_to_jiffies(pstore_update_ms); 4806dda9266SLuck, Tony add_timer(&pstore_timer); 481a3f5f075SAnton Vorontsov } 4826dda9266SLuck, Tony 48342222c2aSWang Long /* 48442222c2aSWang Long * Update the module parameter backend, so it is visible 48542222c2aSWang Long * through /sys/module/pstore/parameters/backend 48642222c2aSWang Long */ 48742222c2aSWang Long backend = psi->name; 48842222c2aSWang Long 489ee1d2674SGeliang Tang module_put(owner); 490ee1d2674SGeliang Tang 491ef748853SFabian Frederick pr_info("Registered %s as persistent store backend\n", psi->name); 4928e48b1a8SLenny Szubowicz 493ca01d6ddSTony Luck return 0; 494ca01d6ddSTony Luck } 495ca01d6ddSTony Luck EXPORT_SYMBOL_GPL(pstore_register); 496ca01d6ddSTony Luck 497ee1d2674SGeliang Tang void pstore_unregister(struct pstore_info *psi) 498ee1d2674SGeliang Tang { 499a1db8060SKees Cook if ((psi->flags & PSTORE_FLAGS_FRAGILE) == 0) { 500ee1d2674SGeliang Tang pstore_unregister_pmsg(); 501ee1d2674SGeliang Tang pstore_unregister_ftrace(); 502ee1d2674SGeliang Tang pstore_unregister_console(); 503a1db8060SKees Cook } 504ee1d2674SGeliang Tang pstore_unregister_kmsg(); 505ee1d2674SGeliang Tang 506ee1d2674SGeliang Tang free_buf_for_compression(); 507ee1d2674SGeliang Tang 508ee1d2674SGeliang Tang psinfo = NULL; 509ee1d2674SGeliang Tang backend = NULL; 510ee1d2674SGeliang Tang } 511ee1d2674SGeliang Tang EXPORT_SYMBOL_GPL(pstore_unregister); 512ee1d2674SGeliang Tang 513ca01d6ddSTony Luck /* 5146dda9266SLuck, Tony * Read all the records from the persistent store. Create 5156dda9266SLuck, Tony * files in our filesystem. Don't warn about -EEXIST errors 5166dda9266SLuck, Tony * when we are re-scanning the backing store looking to add new 5176dda9266SLuck, Tony * error records. 518ca01d6ddSTony Luck */ 5196dda9266SLuck, Tony void pstore_get_records(int quiet) 520ca01d6ddSTony Luck { 521ca01d6ddSTony Luck struct pstore_info *psi = psinfo; 522f6f82851SKees Cook char *buf = NULL; 5238d38d74bSChen Gong ssize_t size; 524ca01d6ddSTony Luck u64 id; 525755d4fe4SSeiji Aguchi int count; 526ca01d6ddSTony Luck enum pstore_type_id type; 527ca01d6ddSTony Luck struct timespec time; 52806cf91b4SChen Gong int failed = 0, rc; 5299a4e1398SAruna Balakrishnaiah bool compressed; 530adb42f5eSAruna Balakrishnaiah int unzipped_len = -1; 531ca01d6ddSTony Luck 532ca01d6ddSTony Luck if (!psi) 533ca01d6ddSTony Luck return; 534ca01d6ddSTony Luck 535f6f82851SKees Cook mutex_lock(&psi->read_mutex); 5362174f6dfSKees Cook if (psi->open && psi->open(psi)) 53706cf91b4SChen Gong goto out; 53806cf91b4SChen Gong 5399a4e1398SAruna Balakrishnaiah while ((size = psi->read(&id, &type, &count, &time, &buf, &compressed, 5409a4e1398SAruna Balakrishnaiah psi)) > 0) { 541adb42f5eSAruna Balakrishnaiah if (compressed && (type == PSTORE_TYPE_DMESG)) { 542adb42f5eSAruna Balakrishnaiah if (big_oops_buf) 543adb42f5eSAruna Balakrishnaiah unzipped_len = pstore_decompress(buf, 544adb42f5eSAruna Balakrishnaiah big_oops_buf, size, 545adb42f5eSAruna Balakrishnaiah big_oops_buf_sz); 546adb42f5eSAruna Balakrishnaiah 547adb42f5eSAruna Balakrishnaiah if (unzipped_len > 0) { 548e32634f5SLiu ShuoX kfree(buf); 549adb42f5eSAruna Balakrishnaiah buf = big_oops_buf; 550adb42f5eSAruna Balakrishnaiah size = unzipped_len; 5519ad2cbe0SAruna Balakrishnaiah compressed = false; 552adb42f5eSAruna Balakrishnaiah } else { 553ef748853SFabian Frederick pr_err("decompression failed;returned %d\n", 554ef748853SFabian Frederick unzipped_len); 5559ad2cbe0SAruna Balakrishnaiah compressed = true; 556adb42f5eSAruna Balakrishnaiah } 557adb42f5eSAruna Balakrishnaiah } 558755d4fe4SSeiji Aguchi rc = pstore_mkfile(type, psi->name, id, count, buf, 5599ad2cbe0SAruna Balakrishnaiah compressed, (size_t)size, time, psi); 560adb42f5eSAruna Balakrishnaiah if (unzipped_len < 0) { 561adb42f5eSAruna Balakrishnaiah /* Free buffer other than big oops */ 562f6f82851SKees Cook kfree(buf); 563f6f82851SKees Cook buf = NULL; 564adb42f5eSAruna Balakrishnaiah } else 565adb42f5eSAruna Balakrishnaiah unzipped_len = -1; 5666dda9266SLuck, Tony if (rc && (rc != -EEXIST || !quiet)) 567ca01d6ddSTony Luck failed++; 568ca01d6ddSTony Luck } 5692174f6dfSKees Cook if (psi->close) 57006cf91b4SChen Gong psi->close(psi); 57106cf91b4SChen Gong out: 572f6f82851SKees Cook mutex_unlock(&psi->read_mutex); 573ca01d6ddSTony Luck 574ca01d6ddSTony Luck if (failed) 575ef748853SFabian Frederick pr_warn("failed to load %d record(s) from '%s'\n", 576ca01d6ddSTony Luck failed, psi->name); 577ca01d6ddSTony Luck } 578ca01d6ddSTony Luck 5796dda9266SLuck, Tony static void pstore_dowork(struct work_struct *work) 5806dda9266SLuck, Tony { 5816dda9266SLuck, Tony pstore_get_records(1); 5826dda9266SLuck, Tony } 5836dda9266SLuck, Tony 5846dda9266SLuck, Tony static void pstore_timefunc(unsigned long dummy) 5856dda9266SLuck, Tony { 5866dda9266SLuck, Tony if (pstore_new_entry) { 5876dda9266SLuck, Tony pstore_new_entry = 0; 5886dda9266SLuck, Tony schedule_work(&pstore_work); 5896dda9266SLuck, Tony } 5906dda9266SLuck, Tony 591a3f5f075SAnton Vorontsov mod_timer(&pstore_timer, jiffies + msecs_to_jiffies(pstore_update_ms)); 5926dda9266SLuck, Tony } 5936dda9266SLuck, Tony 594dee28e72SMatthew Garrett module_param(backend, charp, 0444); 595dee28e72SMatthew Garrett MODULE_PARM_DESC(backend, "Pstore backend to use"); 596