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); 64*060287b8SAnton 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; 167f29e5956SAnton Vorontsov spin_lock_irqsave(&psinfo->buf_lock, flags); 168f29e5956SAnton Vorontsov memcpy(psinfo->buf, s, c); 169f29e5956SAnton Vorontsov psinfo->write(PSTORE_TYPE_CONSOLE, 0, NULL, 0, c, psinfo); 170f29e5956SAnton Vorontsov spin_unlock_irqrestore(&psinfo->buf_lock, flags); 171f29e5956SAnton Vorontsov s += c; 172f29e5956SAnton Vorontsov c = e - s; 173f29e5956SAnton Vorontsov } 174f29e5956SAnton Vorontsov } 175f29e5956SAnton Vorontsov 176f29e5956SAnton Vorontsov static struct console pstore_console = { 177f29e5956SAnton Vorontsov .name = "pstore", 178f29e5956SAnton Vorontsov .write = pstore_console_write, 179f29e5956SAnton Vorontsov .flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME, 180f29e5956SAnton Vorontsov .index = -1, 181f29e5956SAnton Vorontsov }; 182f29e5956SAnton Vorontsov 183f29e5956SAnton Vorontsov static void pstore_register_console(void) 184f29e5956SAnton Vorontsov { 185f29e5956SAnton Vorontsov register_console(&pstore_console); 186f29e5956SAnton Vorontsov } 187f29e5956SAnton Vorontsov #else 188f29e5956SAnton Vorontsov static void pstore_register_console(void) {} 189f29e5956SAnton Vorontsov #endif 190f29e5956SAnton Vorontsov 191897dba02SAnton Vorontsov static int pstore_write_compat(enum pstore_type_id type, 192897dba02SAnton Vorontsov enum kmsg_dump_reason reason, 193897dba02SAnton Vorontsov u64 *id, unsigned int part, 194897dba02SAnton Vorontsov size_t size, struct pstore_info *psi) 195897dba02SAnton Vorontsov { 196897dba02SAnton Vorontsov return psi->write_buf(type, reason, id, part, psinfo->buf, size, psi); 197897dba02SAnton Vorontsov } 198897dba02SAnton 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 223897dba02SAnton Vorontsov if (!psi->write) 224897dba02SAnton Vorontsov psi->write = pstore_write_compat; 225ca01d6ddSTony Luck psinfo = psi; 226f6f82851SKees Cook mutex_init(&psinfo->read_mutex); 227ca01d6ddSTony Luck spin_unlock(&pstore_lock); 228ca01d6ddSTony Luck 229ca01d6ddSTony Luck if (owner && !try_module_get(owner)) { 230ca01d6ddSTony Luck psinfo = NULL; 231ca01d6ddSTony Luck return -EINVAL; 232ca01d6ddSTony Luck } 233ca01d6ddSTony Luck 234ca01d6ddSTony Luck if (pstore_is_mounted()) 2356dda9266SLuck, Tony pstore_get_records(0); 236ca01d6ddSTony Luck 237ca01d6ddSTony Luck kmsg_dump_register(&pstore_dumper); 238f29e5956SAnton Vorontsov pstore_register_console(); 239ca01d6ddSTony Luck 240a3f5f075SAnton Vorontsov if (pstore_update_ms >= 0) { 241a3f5f075SAnton Vorontsov pstore_timer.expires = jiffies + 242a3f5f075SAnton Vorontsov msecs_to_jiffies(pstore_update_ms); 2436dda9266SLuck, Tony add_timer(&pstore_timer); 244a3f5f075SAnton Vorontsov } 2456dda9266SLuck, Tony 246ca01d6ddSTony Luck return 0; 247ca01d6ddSTony Luck } 248ca01d6ddSTony Luck EXPORT_SYMBOL_GPL(pstore_register); 249ca01d6ddSTony Luck 250ca01d6ddSTony Luck /* 2516dda9266SLuck, Tony * Read all the records from the persistent store. Create 2526dda9266SLuck, Tony * files in our filesystem. Don't warn about -EEXIST errors 2536dda9266SLuck, Tony * when we are re-scanning the backing store looking to add new 2546dda9266SLuck, Tony * error records. 255ca01d6ddSTony Luck */ 2566dda9266SLuck, Tony void pstore_get_records(int quiet) 257ca01d6ddSTony Luck { 258ca01d6ddSTony Luck struct pstore_info *psi = psinfo; 259f6f82851SKees Cook char *buf = NULL; 2608d38d74bSChen Gong ssize_t size; 261ca01d6ddSTony Luck u64 id; 262ca01d6ddSTony Luck enum pstore_type_id type; 263ca01d6ddSTony Luck struct timespec time; 26406cf91b4SChen Gong int failed = 0, rc; 265ca01d6ddSTony Luck 266ca01d6ddSTony Luck if (!psi) 267ca01d6ddSTony Luck return; 268ca01d6ddSTony Luck 269f6f82851SKees Cook mutex_lock(&psi->read_mutex); 2702174f6dfSKees Cook if (psi->open && psi->open(psi)) 27106cf91b4SChen Gong goto out; 27206cf91b4SChen Gong 273f6f82851SKees Cook while ((size = psi->read(&id, &type, &time, &buf, psi)) > 0) { 274f6f82851SKees Cook rc = pstore_mkfile(type, psi->name, id, buf, (size_t)size, 2756dda9266SLuck, Tony time, psi); 276f6f82851SKees Cook kfree(buf); 277f6f82851SKees Cook buf = NULL; 2786dda9266SLuck, Tony if (rc && (rc != -EEXIST || !quiet)) 279ca01d6ddSTony Luck failed++; 280ca01d6ddSTony Luck } 2812174f6dfSKees Cook if (psi->close) 28206cf91b4SChen Gong psi->close(psi); 28306cf91b4SChen Gong out: 284f6f82851SKees Cook mutex_unlock(&psi->read_mutex); 285ca01d6ddSTony Luck 286ca01d6ddSTony Luck if (failed) 287ca01d6ddSTony Luck printk(KERN_WARNING "pstore: failed to load %d record(s) from '%s'\n", 288ca01d6ddSTony Luck failed, psi->name); 289ca01d6ddSTony Luck } 290ca01d6ddSTony Luck 2916dda9266SLuck, Tony static void pstore_dowork(struct work_struct *work) 2926dda9266SLuck, Tony { 2936dda9266SLuck, Tony pstore_get_records(1); 2946dda9266SLuck, Tony } 2956dda9266SLuck, Tony 2966dda9266SLuck, Tony static void pstore_timefunc(unsigned long dummy) 2976dda9266SLuck, Tony { 2986dda9266SLuck, Tony if (pstore_new_entry) { 2996dda9266SLuck, Tony pstore_new_entry = 0; 3006dda9266SLuck, Tony schedule_work(&pstore_work); 3016dda9266SLuck, Tony } 3026dda9266SLuck, Tony 303a3f5f075SAnton Vorontsov mod_timer(&pstore_timer, jiffies + msecs_to_jiffies(pstore_update_ms)); 3046dda9266SLuck, Tony } 3056dda9266SLuck, Tony 306dee28e72SMatthew Garrett module_param(backend, charp, 0444); 307dee28e72SMatthew Garrett MODULE_PARM_DESC(backend, "Pstore backend to use"); 308