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 21ca01d6ddSTony Luck #include <linux/atomic.h> 22ca01d6ddSTony Luck #include <linux/types.h> 23ca01d6ddSTony Luck #include <linux/errno.h> 24ca01d6ddSTony Luck #include <linux/init.h> 25ca01d6ddSTony Luck #include <linux/kmsg_dump.h> 26f29e5956SAnton Vorontsov #include <linux/console.h> 27ca01d6ddSTony Luck #include <linux/module.h> 28ca01d6ddSTony Luck #include <linux/pstore.h> 29ca01d6ddSTony Luck #include <linux/string.h> 306dda9266SLuck, Tony #include <linux/timer.h> 31ca01d6ddSTony Luck #include <linux/slab.h> 32ca01d6ddSTony Luck #include <linux/uaccess.h> 33abd4d558SDon Zickus #include <linux/hardirq.h> 34*a3f5f075SAnton Vorontsov #include <linux/jiffies.h> 356dda9266SLuck, Tony #include <linux/workqueue.h> 36ca01d6ddSTony Luck 37ca01d6ddSTony Luck #include "internal.h" 38ca01d6ddSTony Luck 39ca01d6ddSTony Luck /* 406dda9266SLuck, Tony * We defer making "oops" entries appear in pstore - see 416dda9266SLuck, Tony * whether the system is actually still running well enough 426dda9266SLuck, Tony * to let someone see the entry 436dda9266SLuck, Tony */ 44*a3f5f075SAnton Vorontsov static int pstore_update_ms = 60000; 45*a3f5f075SAnton Vorontsov module_param_named(update_ms, pstore_update_ms, int, 0600); 46*a3f5f075SAnton Vorontsov MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content " 47*a3f5f075SAnton Vorontsov "(default is 60000; -1 means runtime updates are disabled)"); 486dda9266SLuck, Tony 496dda9266SLuck, Tony static int pstore_new_entry; 506dda9266SLuck, Tony 516dda9266SLuck, Tony static void pstore_timefunc(unsigned long); 526dda9266SLuck, Tony static DEFINE_TIMER(pstore_timer, pstore_timefunc, 0, 0); 536dda9266SLuck, Tony 546dda9266SLuck, Tony static void pstore_dowork(struct work_struct *); 556dda9266SLuck, Tony static DECLARE_WORK(pstore_work, pstore_dowork); 566dda9266SLuck, Tony 576dda9266SLuck, Tony /* 58ca01d6ddSTony Luck * pstore_lock just protects "psinfo" during 59ca01d6ddSTony Luck * calls to pstore_register() 60ca01d6ddSTony Luck */ 61ca01d6ddSTony Luck static DEFINE_SPINLOCK(pstore_lock); 62ca01d6ddSTony Luck static struct pstore_info *psinfo; 63ca01d6ddSTony Luck 64dee28e72SMatthew Garrett static char *backend; 65dee28e72SMatthew Garrett 66366f7e7aSLuck, Tony /* How much of the console log to snapshot */ 67ca01d6ddSTony Luck static unsigned long kmsg_bytes = 10240; 68ca01d6ddSTony Luck 69366f7e7aSLuck, Tony void pstore_set_kmsg_bytes(int bytes) 70ca01d6ddSTony Luck { 71366f7e7aSLuck, Tony kmsg_bytes = bytes; 72ca01d6ddSTony Luck } 73ca01d6ddSTony Luck 74ca01d6ddSTony Luck /* Tag each group of saved records with a sequence number */ 75ca01d6ddSTony Luck static int oopscount; 76ca01d6ddSTony Luck 77381b872cSSeiji Aguchi static const char *get_reason_str(enum kmsg_dump_reason reason) 78381b872cSSeiji Aguchi { 79381b872cSSeiji Aguchi switch (reason) { 80381b872cSSeiji Aguchi case KMSG_DUMP_PANIC: 81381b872cSSeiji Aguchi return "Panic"; 82381b872cSSeiji Aguchi case KMSG_DUMP_OOPS: 83381b872cSSeiji Aguchi return "Oops"; 84381b872cSSeiji Aguchi case KMSG_DUMP_EMERG: 85381b872cSSeiji Aguchi return "Emergency"; 86381b872cSSeiji Aguchi case KMSG_DUMP_RESTART: 87381b872cSSeiji Aguchi return "Restart"; 88381b872cSSeiji Aguchi case KMSG_DUMP_HALT: 89381b872cSSeiji Aguchi return "Halt"; 90381b872cSSeiji Aguchi case KMSG_DUMP_POWEROFF: 91381b872cSSeiji Aguchi return "Poweroff"; 92381b872cSSeiji Aguchi default: 93381b872cSSeiji Aguchi return "Unknown"; 94381b872cSSeiji Aguchi } 95381b872cSSeiji Aguchi } 969f6af27fSTony Luck 97ca01d6ddSTony Luck /* 98ca01d6ddSTony Luck * callback from kmsg_dump. (s2,l2) has the most recently 99ca01d6ddSTony Luck * written bytes, older bytes are in (s1,l1). Save as much 100ca01d6ddSTony Luck * as we can from the end of the buffer. 101ca01d6ddSTony Luck */ 102ca01d6ddSTony Luck static void pstore_dump(struct kmsg_dumper *dumper, 103ca01d6ddSTony Luck enum kmsg_dump_reason reason, 104ca01d6ddSTony Luck const char *s1, unsigned long l1, 105ca01d6ddSTony Luck const char *s2, unsigned long l2) 106ca01d6ddSTony Luck { 107ca01d6ddSTony Luck unsigned long s1_start, s2_start; 108ca01d6ddSTony Luck unsigned long l1_cpy, l2_cpy; 109ca01d6ddSTony Luck unsigned long size, total = 0; 110381b872cSSeiji Aguchi char *dst; 111381b872cSSeiji Aguchi const char *why; 112ca01d6ddSTony Luck u64 id; 113b238b8faSChen Gong int hsize, ret; 114b94fdd07SMatthew Garrett unsigned int part = 1; 115abd4d558SDon Zickus unsigned long flags = 0; 116abd4d558SDon Zickus int is_locked = 0; 117ca01d6ddSTony Luck 118381b872cSSeiji Aguchi why = get_reason_str(reason); 1199f6af27fSTony Luck 120abd4d558SDon Zickus if (in_nmi()) { 121abd4d558SDon Zickus is_locked = spin_trylock(&psinfo->buf_lock); 122abd4d558SDon Zickus if (!is_locked) 123abd4d558SDon Zickus pr_err("pstore dump routine blocked in NMI, may corrupt error record\n"); 124abd4d558SDon Zickus } else 125abd4d558SDon Zickus spin_lock_irqsave(&psinfo->buf_lock, flags); 126ca01d6ddSTony Luck oopscount++; 127ca01d6ddSTony Luck while (total < kmsg_bytes) { 128ca01d6ddSTony Luck dst = psinfo->buf; 12956280682SMatthew Garrett hsize = sprintf(dst, "%s#%d Part%d\n", why, oopscount, part); 130ca01d6ddSTony Luck size = psinfo->bufsize - hsize; 131ca01d6ddSTony Luck dst += hsize; 132ca01d6ddSTony Luck 133ca01d6ddSTony Luck l2_cpy = min(l2, size); 134ca01d6ddSTony Luck l1_cpy = min(l1, size - l2_cpy); 135ca01d6ddSTony Luck 136ca01d6ddSTony Luck if (l1_cpy + l2_cpy == 0) 137ca01d6ddSTony Luck break; 138ca01d6ddSTony Luck 139ca01d6ddSTony Luck s2_start = l2 - l2_cpy; 140ca01d6ddSTony Luck s1_start = l1 - l1_cpy; 141ca01d6ddSTony Luck 142ca01d6ddSTony Luck memcpy(dst, s1 + s1_start, l1_cpy); 143ca01d6ddSTony Luck memcpy(dst + l1_cpy, s2 + s2_start, l2_cpy); 144ca01d6ddSTony Luck 1453d6d8d20SKees Cook ret = psinfo->write(PSTORE_TYPE_DMESG, reason, &id, part, 14656280682SMatthew Garrett hsize + l1_cpy + l2_cpy, psinfo); 147b238b8faSChen Gong if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted()) 1486dda9266SLuck, Tony pstore_new_entry = 1; 149ca01d6ddSTony Luck l1 -= l1_cpy; 150ca01d6ddSTony Luck l2 -= l2_cpy; 151ca01d6ddSTony Luck total += l1_cpy + l2_cpy; 15256280682SMatthew Garrett part++; 153ca01d6ddSTony Luck } 154abd4d558SDon Zickus if (in_nmi()) { 155abd4d558SDon Zickus if (is_locked) 156abd4d558SDon Zickus spin_unlock(&psinfo->buf_lock); 157abd4d558SDon Zickus } else 158abd4d558SDon Zickus spin_unlock_irqrestore(&psinfo->buf_lock, flags); 159ca01d6ddSTony Luck } 160ca01d6ddSTony Luck 161ca01d6ddSTony Luck static struct kmsg_dumper pstore_dumper = { 162ca01d6ddSTony Luck .dump = pstore_dump, 163ca01d6ddSTony Luck }; 164ca01d6ddSTony Luck 165f29e5956SAnton Vorontsov #ifdef CONFIG_PSTORE_CONSOLE 166f29e5956SAnton Vorontsov static void pstore_console_write(struct console *con, const char *s, unsigned c) 167f29e5956SAnton Vorontsov { 168f29e5956SAnton Vorontsov const char *e = s + c; 169f29e5956SAnton Vorontsov 170f29e5956SAnton Vorontsov while (s < e) { 171f29e5956SAnton Vorontsov unsigned long flags; 172f29e5956SAnton Vorontsov 173f29e5956SAnton Vorontsov if (c > psinfo->bufsize) 174f29e5956SAnton Vorontsov c = psinfo->bufsize; 175f29e5956SAnton Vorontsov spin_lock_irqsave(&psinfo->buf_lock, flags); 176f29e5956SAnton Vorontsov memcpy(psinfo->buf, s, c); 177f29e5956SAnton Vorontsov psinfo->write(PSTORE_TYPE_CONSOLE, 0, NULL, 0, c, psinfo); 178f29e5956SAnton Vorontsov spin_unlock_irqrestore(&psinfo->buf_lock, flags); 179f29e5956SAnton Vorontsov s += c; 180f29e5956SAnton Vorontsov c = e - s; 181f29e5956SAnton Vorontsov } 182f29e5956SAnton Vorontsov } 183f29e5956SAnton Vorontsov 184f29e5956SAnton Vorontsov static struct console pstore_console = { 185f29e5956SAnton Vorontsov .name = "pstore", 186f29e5956SAnton Vorontsov .write = pstore_console_write, 187f29e5956SAnton Vorontsov .flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME, 188f29e5956SAnton Vorontsov .index = -1, 189f29e5956SAnton Vorontsov }; 190f29e5956SAnton Vorontsov 191f29e5956SAnton Vorontsov static void pstore_register_console(void) 192f29e5956SAnton Vorontsov { 193f29e5956SAnton Vorontsov register_console(&pstore_console); 194f29e5956SAnton Vorontsov } 195f29e5956SAnton Vorontsov #else 196f29e5956SAnton Vorontsov static void pstore_register_console(void) {} 197f29e5956SAnton Vorontsov #endif 198f29e5956SAnton Vorontsov 199ca01d6ddSTony Luck /* 200ca01d6ddSTony Luck * platform specific persistent storage driver registers with 201ca01d6ddSTony Luck * us here. If pstore is already mounted, call the platform 202ca01d6ddSTony Luck * read function right away to populate the file system. If not 203ca01d6ddSTony Luck * then the pstore mount code will call us later to fill out 204ca01d6ddSTony Luck * the file system. 205ca01d6ddSTony Luck * 206ca01d6ddSTony Luck * Register with kmsg_dump to save last part of console log on panic. 207ca01d6ddSTony Luck */ 208ca01d6ddSTony Luck int pstore_register(struct pstore_info *psi) 209ca01d6ddSTony Luck { 210ca01d6ddSTony Luck struct module *owner = psi->owner; 211ca01d6ddSTony Luck 212ca01d6ddSTony Luck spin_lock(&pstore_lock); 213ca01d6ddSTony Luck if (psinfo) { 214ca01d6ddSTony Luck spin_unlock(&pstore_lock); 215ca01d6ddSTony Luck return -EBUSY; 216ca01d6ddSTony Luck } 217dee28e72SMatthew Garrett 218dee28e72SMatthew Garrett if (backend && strcmp(backend, psi->name)) { 219dee28e72SMatthew Garrett spin_unlock(&pstore_lock); 220dee28e72SMatthew Garrett return -EINVAL; 221dee28e72SMatthew Garrett } 222dee28e72SMatthew Garrett 223ca01d6ddSTony Luck psinfo = psi; 224f6f82851SKees Cook mutex_init(&psinfo->read_mutex); 225ca01d6ddSTony Luck spin_unlock(&pstore_lock); 226ca01d6ddSTony Luck 227ca01d6ddSTony Luck if (owner && !try_module_get(owner)) { 228ca01d6ddSTony Luck psinfo = NULL; 229ca01d6ddSTony Luck return -EINVAL; 230ca01d6ddSTony Luck } 231ca01d6ddSTony Luck 232ca01d6ddSTony Luck if (pstore_is_mounted()) 2336dda9266SLuck, Tony pstore_get_records(0); 234ca01d6ddSTony Luck 235ca01d6ddSTony Luck kmsg_dump_register(&pstore_dumper); 236f29e5956SAnton Vorontsov pstore_register_console(); 237ca01d6ddSTony Luck 238*a3f5f075SAnton Vorontsov if (pstore_update_ms >= 0) { 239*a3f5f075SAnton Vorontsov pstore_timer.expires = jiffies + 240*a3f5f075SAnton Vorontsov msecs_to_jiffies(pstore_update_ms); 2416dda9266SLuck, Tony add_timer(&pstore_timer); 242*a3f5f075SAnton Vorontsov } 2436dda9266SLuck, Tony 244ca01d6ddSTony Luck return 0; 245ca01d6ddSTony Luck } 246ca01d6ddSTony Luck EXPORT_SYMBOL_GPL(pstore_register); 247ca01d6ddSTony Luck 248ca01d6ddSTony Luck /* 2496dda9266SLuck, Tony * Read all the records from the persistent store. Create 2506dda9266SLuck, Tony * files in our filesystem. Don't warn about -EEXIST errors 2516dda9266SLuck, Tony * when we are re-scanning the backing store looking to add new 2526dda9266SLuck, Tony * error records. 253ca01d6ddSTony Luck */ 2546dda9266SLuck, Tony void pstore_get_records(int quiet) 255ca01d6ddSTony Luck { 256ca01d6ddSTony Luck struct pstore_info *psi = psinfo; 257f6f82851SKees Cook char *buf = NULL; 2588d38d74bSChen Gong ssize_t size; 259ca01d6ddSTony Luck u64 id; 260ca01d6ddSTony Luck enum pstore_type_id type; 261ca01d6ddSTony Luck struct timespec time; 26206cf91b4SChen Gong int failed = 0, rc; 263ca01d6ddSTony Luck 264ca01d6ddSTony Luck if (!psi) 265ca01d6ddSTony Luck return; 266ca01d6ddSTony Luck 267f6f82851SKees Cook mutex_lock(&psi->read_mutex); 2682174f6dfSKees Cook if (psi->open && psi->open(psi)) 26906cf91b4SChen Gong goto out; 27006cf91b4SChen Gong 271f6f82851SKees Cook while ((size = psi->read(&id, &type, &time, &buf, psi)) > 0) { 272f6f82851SKees Cook rc = pstore_mkfile(type, psi->name, id, buf, (size_t)size, 2736dda9266SLuck, Tony time, psi); 274f6f82851SKees Cook kfree(buf); 275f6f82851SKees Cook buf = NULL; 2766dda9266SLuck, Tony if (rc && (rc != -EEXIST || !quiet)) 277ca01d6ddSTony Luck failed++; 278ca01d6ddSTony Luck } 2792174f6dfSKees Cook if (psi->close) 28006cf91b4SChen Gong psi->close(psi); 28106cf91b4SChen Gong out: 282f6f82851SKees Cook mutex_unlock(&psi->read_mutex); 283ca01d6ddSTony Luck 284ca01d6ddSTony Luck if (failed) 285ca01d6ddSTony Luck printk(KERN_WARNING "pstore: failed to load %d record(s) from '%s'\n", 286ca01d6ddSTony Luck failed, psi->name); 287ca01d6ddSTony Luck } 288ca01d6ddSTony Luck 2896dda9266SLuck, Tony static void pstore_dowork(struct work_struct *work) 2906dda9266SLuck, Tony { 2916dda9266SLuck, Tony pstore_get_records(1); 2926dda9266SLuck, Tony } 2936dda9266SLuck, Tony 2946dda9266SLuck, Tony static void pstore_timefunc(unsigned long dummy) 2956dda9266SLuck, Tony { 2966dda9266SLuck, Tony if (pstore_new_entry) { 2976dda9266SLuck, Tony pstore_new_entry = 0; 2986dda9266SLuck, Tony schedule_work(&pstore_work); 2996dda9266SLuck, Tony } 3006dda9266SLuck, Tony 301*a3f5f075SAnton Vorontsov mod_timer(&pstore_timer, jiffies + msecs_to_jiffies(pstore_update_ms)); 3026dda9266SLuck, Tony } 3036dda9266SLuck, Tony 304dee28e72SMatthew Garrett module_param(backend, charp, 0444); 305dee28e72SMatthew Garrett MODULE_PARM_DESC(backend, "Pstore backend to use"); 306