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> 34a3f5f075SAnton 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 */ 44521f7288SAnton Vorontsov static int pstore_update_ms = -1; 45a3f5f075SAnton Vorontsov module_param_named(update_ms, pstore_update_ms, int, 0600); 46a3f5f075SAnton Vorontsov MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content " 47521f7288SAnton Vorontsov "(default is -1, which means runtime updates are disabled; " 48521f7288SAnton Vorontsov "enabling this option is not safe, it may lead to further " 49521f7288SAnton Vorontsov "corruption on Oopses)"); 506dda9266SLuck, Tony 516dda9266SLuck, Tony static int pstore_new_entry; 526dda9266SLuck, Tony 536dda9266SLuck, Tony static void pstore_timefunc(unsigned long); 546dda9266SLuck, Tony static DEFINE_TIMER(pstore_timer, pstore_timefunc, 0, 0); 556dda9266SLuck, Tony 566dda9266SLuck, Tony static void pstore_dowork(struct work_struct *); 576dda9266SLuck, Tony static DECLARE_WORK(pstore_work, pstore_dowork); 586dda9266SLuck, Tony 596dda9266SLuck, Tony /* 60ca01d6ddSTony Luck * pstore_lock just protects "psinfo" during 61ca01d6ddSTony Luck * calls to pstore_register() 62ca01d6ddSTony Luck */ 63ca01d6ddSTony Luck static DEFINE_SPINLOCK(pstore_lock); 64060287b8SAnton Vorontsov struct pstore_info *psinfo; 65ca01d6ddSTony Luck 66dee28e72SMatthew Garrett static char *backend; 67dee28e72SMatthew Garrett 68366f7e7aSLuck, Tony /* How much of the console log to snapshot */ 69ca01d6ddSTony Luck static unsigned long kmsg_bytes = 10240; 70ca01d6ddSTony Luck 71366f7e7aSLuck, Tony void pstore_set_kmsg_bytes(int bytes) 72ca01d6ddSTony Luck { 73366f7e7aSLuck, Tony kmsg_bytes = bytes; 74ca01d6ddSTony Luck } 75ca01d6ddSTony Luck 76ca01d6ddSTony Luck /* Tag each group of saved records with a sequence number */ 77ca01d6ddSTony Luck static int oopscount; 78ca01d6ddSTony Luck 79381b872cSSeiji Aguchi static const char *get_reason_str(enum kmsg_dump_reason reason) 80381b872cSSeiji Aguchi { 81381b872cSSeiji Aguchi switch (reason) { 82381b872cSSeiji Aguchi case KMSG_DUMP_PANIC: 83381b872cSSeiji Aguchi return "Panic"; 84381b872cSSeiji Aguchi case KMSG_DUMP_OOPS: 85381b872cSSeiji Aguchi return "Oops"; 86381b872cSSeiji Aguchi case KMSG_DUMP_EMERG: 87381b872cSSeiji Aguchi return "Emergency"; 88381b872cSSeiji Aguchi case KMSG_DUMP_RESTART: 89381b872cSSeiji Aguchi return "Restart"; 90381b872cSSeiji Aguchi case KMSG_DUMP_HALT: 91381b872cSSeiji Aguchi return "Halt"; 92381b872cSSeiji Aguchi case KMSG_DUMP_POWEROFF: 93381b872cSSeiji Aguchi return "Poweroff"; 94381b872cSSeiji Aguchi default: 95381b872cSSeiji Aguchi return "Unknown"; 96381b872cSSeiji Aguchi } 97381b872cSSeiji Aguchi } 989f6af27fSTony Luck 99ca01d6ddSTony Luck /* 100ca01d6ddSTony Luck * callback from kmsg_dump. (s2,l2) has the most recently 101ca01d6ddSTony Luck * written bytes, older bytes are in (s1,l1). Save as much 102ca01d6ddSTony Luck * as we can from the end of the buffer. 103ca01d6ddSTony Luck */ 104ca01d6ddSTony Luck static void pstore_dump(struct kmsg_dumper *dumper, 105e2ae715dSKay Sievers enum kmsg_dump_reason reason) 106ca01d6ddSTony Luck { 107e2ae715dSKay Sievers unsigned long total = 0; 108381b872cSSeiji Aguchi const char *why; 109ca01d6ddSTony Luck u64 id; 110b94fdd07SMatthew Garrett unsigned int part = 1; 111abd4d558SDon Zickus unsigned long flags = 0; 112abd4d558SDon Zickus int is_locked = 0; 113e2ae715dSKay Sievers int ret; 114ca01d6ddSTony Luck 115381b872cSSeiji Aguchi why = get_reason_str(reason); 1169f6af27fSTony Luck 117abd4d558SDon Zickus if (in_nmi()) { 118abd4d558SDon Zickus is_locked = spin_trylock(&psinfo->buf_lock); 119abd4d558SDon Zickus if (!is_locked) 120abd4d558SDon Zickus pr_err("pstore dump routine blocked in NMI, may corrupt error record\n"); 121abd4d558SDon Zickus } else 122abd4d558SDon Zickus spin_lock_irqsave(&psinfo->buf_lock, flags); 123ca01d6ddSTony Luck oopscount++; 124ca01d6ddSTony Luck while (total < kmsg_bytes) { 125e2ae715dSKay Sievers char *dst; 126e2ae715dSKay Sievers unsigned long size; 127e2ae715dSKay Sievers int hsize; 128e2ae715dSKay Sievers size_t len; 129e2ae715dSKay Sievers 130ca01d6ddSTony Luck dst = psinfo->buf; 13156280682SMatthew Garrett hsize = sprintf(dst, "%s#%d Part%d\n", why, oopscount, part); 132ca01d6ddSTony Luck size = psinfo->bufsize - hsize; 133ca01d6ddSTony Luck dst += hsize; 134ca01d6ddSTony Luck 135e2ae715dSKay Sievers if (!kmsg_dump_get_buffer(dumper, true, dst, size, &len)) 136ca01d6ddSTony Luck break; 137ca01d6ddSTony Luck 1383d6d8d20SKees Cook ret = psinfo->write(PSTORE_TYPE_DMESG, reason, &id, part, 139e2ae715dSKay Sievers hsize + len, psinfo); 140b238b8faSChen Gong if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted()) 1416dda9266SLuck, Tony pstore_new_entry = 1; 142e2ae715dSKay Sievers 143e2ae715dSKay Sievers total += hsize + len; 14456280682SMatthew Garrett part++; 145ca01d6ddSTony Luck } 146abd4d558SDon Zickus if (in_nmi()) { 147abd4d558SDon Zickus if (is_locked) 148abd4d558SDon Zickus spin_unlock(&psinfo->buf_lock); 149abd4d558SDon Zickus } else 150abd4d558SDon Zickus spin_unlock_irqrestore(&psinfo->buf_lock, flags); 151ca01d6ddSTony Luck } 152ca01d6ddSTony Luck 153ca01d6ddSTony Luck static struct kmsg_dumper pstore_dumper = { 154ca01d6ddSTony Luck .dump = pstore_dump, 155ca01d6ddSTony Luck }; 156ca01d6ddSTony Luck 157f29e5956SAnton Vorontsov #ifdef CONFIG_PSTORE_CONSOLE 158f29e5956SAnton Vorontsov static void pstore_console_write(struct console *con, const char *s, unsigned c) 159f29e5956SAnton Vorontsov { 160f29e5956SAnton Vorontsov const char *e = s + c; 161f29e5956SAnton Vorontsov 162f29e5956SAnton Vorontsov while (s < e) { 163f29e5956SAnton Vorontsov unsigned long flags; 164f29e5956SAnton Vorontsov 165f29e5956SAnton Vorontsov if (c > psinfo->bufsize) 166f29e5956SAnton Vorontsov c = psinfo->bufsize; 167*80c9d03cSChuansheng Liu 168*80c9d03cSChuansheng Liu if (oops_in_progress) { 169*80c9d03cSChuansheng Liu if (!spin_trylock_irqsave(&psinfo->buf_lock, flags)) 170*80c9d03cSChuansheng Liu break; 171*80c9d03cSChuansheng Liu } else { 172f29e5956SAnton Vorontsov spin_lock_irqsave(&psinfo->buf_lock, flags); 173*80c9d03cSChuansheng Liu } 174f29e5956SAnton Vorontsov memcpy(psinfo->buf, s, c); 175f29e5956SAnton Vorontsov psinfo->write(PSTORE_TYPE_CONSOLE, 0, NULL, 0, c, psinfo); 176f29e5956SAnton Vorontsov spin_unlock_irqrestore(&psinfo->buf_lock, flags); 177f29e5956SAnton Vorontsov s += c; 178f29e5956SAnton Vorontsov c = e - s; 179f29e5956SAnton Vorontsov } 180f29e5956SAnton Vorontsov } 181f29e5956SAnton Vorontsov 182f29e5956SAnton Vorontsov static struct console pstore_console = { 183f29e5956SAnton Vorontsov .name = "pstore", 184f29e5956SAnton Vorontsov .write = pstore_console_write, 185f29e5956SAnton Vorontsov .flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME, 186f29e5956SAnton Vorontsov .index = -1, 187f29e5956SAnton Vorontsov }; 188f29e5956SAnton Vorontsov 189f29e5956SAnton Vorontsov static void pstore_register_console(void) 190f29e5956SAnton Vorontsov { 191f29e5956SAnton Vorontsov register_console(&pstore_console); 192f29e5956SAnton Vorontsov } 193f29e5956SAnton Vorontsov #else 194f29e5956SAnton Vorontsov static void pstore_register_console(void) {} 195f29e5956SAnton Vorontsov #endif 196f29e5956SAnton Vorontsov 197897dba02SAnton Vorontsov static int pstore_write_compat(enum pstore_type_id type, 198897dba02SAnton Vorontsov enum kmsg_dump_reason reason, 199897dba02SAnton Vorontsov u64 *id, unsigned int part, 200897dba02SAnton Vorontsov size_t size, struct pstore_info *psi) 201897dba02SAnton Vorontsov { 202897dba02SAnton Vorontsov return psi->write_buf(type, reason, id, part, psinfo->buf, size, psi); 203897dba02SAnton Vorontsov } 204897dba02SAnton Vorontsov 205ca01d6ddSTony Luck /* 206ca01d6ddSTony Luck * platform specific persistent storage driver registers with 207ca01d6ddSTony Luck * us here. If pstore is already mounted, call the platform 208ca01d6ddSTony Luck * read function right away to populate the file system. If not 209ca01d6ddSTony Luck * then the pstore mount code will call us later to fill out 210ca01d6ddSTony Luck * the file system. 211ca01d6ddSTony Luck * 212ca01d6ddSTony Luck * Register with kmsg_dump to save last part of console log on panic. 213ca01d6ddSTony Luck */ 214ca01d6ddSTony Luck int pstore_register(struct pstore_info *psi) 215ca01d6ddSTony Luck { 216ca01d6ddSTony Luck struct module *owner = psi->owner; 217ca01d6ddSTony Luck 218ca01d6ddSTony Luck spin_lock(&pstore_lock); 219ca01d6ddSTony Luck if (psinfo) { 220ca01d6ddSTony Luck spin_unlock(&pstore_lock); 221ca01d6ddSTony Luck return -EBUSY; 222ca01d6ddSTony Luck } 223dee28e72SMatthew Garrett 224dee28e72SMatthew Garrett if (backend && strcmp(backend, psi->name)) { 225dee28e72SMatthew Garrett spin_unlock(&pstore_lock); 226dee28e72SMatthew Garrett return -EINVAL; 227dee28e72SMatthew Garrett } 228dee28e72SMatthew Garrett 229897dba02SAnton Vorontsov if (!psi->write) 230897dba02SAnton Vorontsov psi->write = pstore_write_compat; 231ca01d6ddSTony Luck psinfo = psi; 232f6f82851SKees Cook mutex_init(&psinfo->read_mutex); 233ca01d6ddSTony Luck spin_unlock(&pstore_lock); 234ca01d6ddSTony Luck 235ca01d6ddSTony Luck if (owner && !try_module_get(owner)) { 236ca01d6ddSTony Luck psinfo = NULL; 237ca01d6ddSTony Luck return -EINVAL; 238ca01d6ddSTony Luck } 239ca01d6ddSTony Luck 240ca01d6ddSTony Luck if (pstore_is_mounted()) 2416dda9266SLuck, Tony pstore_get_records(0); 242ca01d6ddSTony Luck 243ca01d6ddSTony Luck kmsg_dump_register(&pstore_dumper); 244f29e5956SAnton Vorontsov pstore_register_console(); 24565f8c95eSAnton Vorontsov pstore_register_ftrace(); 246ca01d6ddSTony Luck 247a3f5f075SAnton Vorontsov if (pstore_update_ms >= 0) { 248a3f5f075SAnton Vorontsov pstore_timer.expires = jiffies + 249a3f5f075SAnton Vorontsov msecs_to_jiffies(pstore_update_ms); 2506dda9266SLuck, Tony add_timer(&pstore_timer); 251a3f5f075SAnton Vorontsov } 2526dda9266SLuck, Tony 253ca01d6ddSTony Luck return 0; 254ca01d6ddSTony Luck } 255ca01d6ddSTony Luck EXPORT_SYMBOL_GPL(pstore_register); 256ca01d6ddSTony Luck 257ca01d6ddSTony Luck /* 2586dda9266SLuck, Tony * Read all the records from the persistent store. Create 2596dda9266SLuck, Tony * files in our filesystem. Don't warn about -EEXIST errors 2606dda9266SLuck, Tony * when we are re-scanning the backing store looking to add new 2616dda9266SLuck, Tony * error records. 262ca01d6ddSTony Luck */ 2636dda9266SLuck, Tony void pstore_get_records(int quiet) 264ca01d6ddSTony Luck { 265ca01d6ddSTony Luck struct pstore_info *psi = psinfo; 266f6f82851SKees Cook char *buf = NULL; 2678d38d74bSChen Gong ssize_t size; 268ca01d6ddSTony Luck u64 id; 269ca01d6ddSTony Luck enum pstore_type_id type; 270ca01d6ddSTony Luck struct timespec time; 27106cf91b4SChen Gong int failed = 0, rc; 272ca01d6ddSTony Luck 273ca01d6ddSTony Luck if (!psi) 274ca01d6ddSTony Luck return; 275ca01d6ddSTony Luck 276f6f82851SKees Cook mutex_lock(&psi->read_mutex); 2772174f6dfSKees Cook if (psi->open && psi->open(psi)) 27806cf91b4SChen Gong goto out; 27906cf91b4SChen Gong 280f6f82851SKees Cook while ((size = psi->read(&id, &type, &time, &buf, psi)) > 0) { 281f6f82851SKees Cook rc = pstore_mkfile(type, psi->name, id, buf, (size_t)size, 2826dda9266SLuck, Tony time, psi); 283f6f82851SKees Cook kfree(buf); 284f6f82851SKees Cook buf = NULL; 2856dda9266SLuck, Tony if (rc && (rc != -EEXIST || !quiet)) 286ca01d6ddSTony Luck failed++; 287ca01d6ddSTony Luck } 2882174f6dfSKees Cook if (psi->close) 28906cf91b4SChen Gong psi->close(psi); 29006cf91b4SChen Gong out: 291f6f82851SKees Cook mutex_unlock(&psi->read_mutex); 292ca01d6ddSTony Luck 293ca01d6ddSTony Luck if (failed) 294ca01d6ddSTony Luck printk(KERN_WARNING "pstore: failed to load %d record(s) from '%s'\n", 295ca01d6ddSTony Luck failed, psi->name); 296ca01d6ddSTony Luck } 297ca01d6ddSTony Luck 2986dda9266SLuck, Tony static void pstore_dowork(struct work_struct *work) 2996dda9266SLuck, Tony { 3006dda9266SLuck, Tony pstore_get_records(1); 3016dda9266SLuck, Tony } 3026dda9266SLuck, Tony 3036dda9266SLuck, Tony static void pstore_timefunc(unsigned long dummy) 3046dda9266SLuck, Tony { 3056dda9266SLuck, Tony if (pstore_new_entry) { 3066dda9266SLuck, Tony pstore_new_entry = 0; 3076dda9266SLuck, Tony schedule_work(&pstore_work); 3086dda9266SLuck, Tony } 3096dda9266SLuck, Tony 310a3f5f075SAnton Vorontsov mod_timer(&pstore_timer, jiffies + msecs_to_jiffies(pstore_update_ms)); 3116dda9266SLuck, Tony } 3126dda9266SLuck, Tony 313dee28e72SMatthew Garrett module_param(backend, charp, 0444); 314dee28e72SMatthew Garrett MODULE_PARM_DESC(backend, "Pstore backend to use"); 315