Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Persistent Storage - platform driver interface parts. |
| 3 | * |
Anton Vorontsov | f29e595 | 2012-05-26 06:20:19 -0700 | [diff] [blame] | 4 | * Copyright (C) 2007-2008 Google, Inc. |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 5 | * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | |
| 21 | #include <linux/atomic.h> |
| 22 | #include <linux/types.h> |
| 23 | #include <linux/errno.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/kmsg_dump.h> |
Anton Vorontsov | f29e595 | 2012-05-26 06:20:19 -0700 | [diff] [blame] | 26 | #include <linux/console.h> |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 27 | #include <linux/module.h> |
| 28 | #include <linux/pstore.h> |
| 29 | #include <linux/string.h> |
Luck, Tony | 6dda926 | 2011-08-11 15:14:39 -0700 | [diff] [blame] | 30 | #include <linux/timer.h> |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 31 | #include <linux/slab.h> |
| 32 | #include <linux/uaccess.h> |
Don Zickus | abd4d55 | 2011-08-12 10:54:51 -0700 | [diff] [blame] | 33 | #include <linux/hardirq.h> |
Anton Vorontsov | a3f5f07 | 2012-05-26 06:20:28 -0700 | [diff] [blame^] | 34 | #include <linux/jiffies.h> |
Luck, Tony | 6dda926 | 2011-08-11 15:14:39 -0700 | [diff] [blame] | 35 | #include <linux/workqueue.h> |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 36 | |
| 37 | #include "internal.h" |
| 38 | |
| 39 | /* |
Luck, Tony | 6dda926 | 2011-08-11 15:14:39 -0700 | [diff] [blame] | 40 | * We defer making "oops" entries appear in pstore - see |
| 41 | * whether the system is actually still running well enough |
| 42 | * to let someone see the entry |
| 43 | */ |
Anton Vorontsov | a3f5f07 | 2012-05-26 06:20:28 -0700 | [diff] [blame^] | 44 | static int pstore_update_ms = 60000; |
| 45 | module_param_named(update_ms, pstore_update_ms, int, 0600); |
| 46 | MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content " |
| 47 | "(default is 60000; -1 means runtime updates are disabled)"); |
Luck, Tony | 6dda926 | 2011-08-11 15:14:39 -0700 | [diff] [blame] | 48 | |
| 49 | static int pstore_new_entry; |
| 50 | |
| 51 | static void pstore_timefunc(unsigned long); |
| 52 | static DEFINE_TIMER(pstore_timer, pstore_timefunc, 0, 0); |
| 53 | |
| 54 | static void pstore_dowork(struct work_struct *); |
| 55 | static DECLARE_WORK(pstore_work, pstore_dowork); |
| 56 | |
| 57 | /* |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 58 | * pstore_lock just protects "psinfo" during |
| 59 | * calls to pstore_register() |
| 60 | */ |
| 61 | static DEFINE_SPINLOCK(pstore_lock); |
| 62 | static struct pstore_info *psinfo; |
| 63 | |
Matthew Garrett | dee28e7 | 2011-07-21 16:57:55 -0400 | [diff] [blame] | 64 | static char *backend; |
| 65 | |
Luck, Tony | 366f7e7 | 2011-03-18 15:33:43 -0700 | [diff] [blame] | 66 | /* How much of the console log to snapshot */ |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 67 | static unsigned long kmsg_bytes = 10240; |
| 68 | |
Luck, Tony | 366f7e7 | 2011-03-18 15:33:43 -0700 | [diff] [blame] | 69 | void pstore_set_kmsg_bytes(int bytes) |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 70 | { |
Luck, Tony | 366f7e7 | 2011-03-18 15:33:43 -0700 | [diff] [blame] | 71 | kmsg_bytes = bytes; |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 74 | /* Tag each group of saved records with a sequence number */ |
| 75 | static int oopscount; |
| 76 | |
Seiji Aguchi | 381b872 | 2012-03-16 15:36:59 -0700 | [diff] [blame] | 77 | static const char *get_reason_str(enum kmsg_dump_reason reason) |
| 78 | { |
| 79 | switch (reason) { |
| 80 | case KMSG_DUMP_PANIC: |
| 81 | return "Panic"; |
| 82 | case KMSG_DUMP_OOPS: |
| 83 | return "Oops"; |
| 84 | case KMSG_DUMP_EMERG: |
| 85 | return "Emergency"; |
| 86 | case KMSG_DUMP_RESTART: |
| 87 | return "Restart"; |
| 88 | case KMSG_DUMP_HALT: |
| 89 | return "Halt"; |
| 90 | case KMSG_DUMP_POWEROFF: |
| 91 | return "Poweroff"; |
| 92 | default: |
| 93 | return "Unknown"; |
| 94 | } |
| 95 | } |
Tony Luck | 9f6af27 | 2011-03-22 16:01:49 -0700 | [diff] [blame] | 96 | |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 97 | /* |
| 98 | * callback from kmsg_dump. (s2,l2) has the most recently |
| 99 | * written bytes, older bytes are in (s1,l1). Save as much |
| 100 | * as we can from the end of the buffer. |
| 101 | */ |
| 102 | static void pstore_dump(struct kmsg_dumper *dumper, |
| 103 | enum kmsg_dump_reason reason, |
| 104 | const char *s1, unsigned long l1, |
| 105 | const char *s2, unsigned long l2) |
| 106 | { |
| 107 | unsigned long s1_start, s2_start; |
| 108 | unsigned long l1_cpy, l2_cpy; |
| 109 | unsigned long size, total = 0; |
Seiji Aguchi | 381b872 | 2012-03-16 15:36:59 -0700 | [diff] [blame] | 110 | char *dst; |
| 111 | const char *why; |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 112 | u64 id; |
Chen Gong | b238b8f | 2011-10-12 09:17:24 -0700 | [diff] [blame] | 113 | int hsize, ret; |
Matthew Garrett | b94fdd0 | 2011-07-21 16:57:54 -0400 | [diff] [blame] | 114 | unsigned int part = 1; |
Don Zickus | abd4d55 | 2011-08-12 10:54:51 -0700 | [diff] [blame] | 115 | unsigned long flags = 0; |
| 116 | int is_locked = 0; |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 117 | |
Seiji Aguchi | 381b872 | 2012-03-16 15:36:59 -0700 | [diff] [blame] | 118 | why = get_reason_str(reason); |
Tony Luck | 9f6af27 | 2011-03-22 16:01:49 -0700 | [diff] [blame] | 119 | |
Don Zickus | abd4d55 | 2011-08-12 10:54:51 -0700 | [diff] [blame] | 120 | if (in_nmi()) { |
| 121 | is_locked = spin_trylock(&psinfo->buf_lock); |
| 122 | if (!is_locked) |
| 123 | pr_err("pstore dump routine blocked in NMI, may corrupt error record\n"); |
| 124 | } else |
| 125 | spin_lock_irqsave(&psinfo->buf_lock, flags); |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 126 | oopscount++; |
| 127 | while (total < kmsg_bytes) { |
| 128 | dst = psinfo->buf; |
Matthew Garrett | 5628068 | 2011-07-21 16:57:53 -0400 | [diff] [blame] | 129 | hsize = sprintf(dst, "%s#%d Part%d\n", why, oopscount, part); |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 130 | size = psinfo->bufsize - hsize; |
| 131 | dst += hsize; |
| 132 | |
| 133 | l2_cpy = min(l2, size); |
| 134 | l1_cpy = min(l1, size - l2_cpy); |
| 135 | |
| 136 | if (l1_cpy + l2_cpy == 0) |
| 137 | break; |
| 138 | |
| 139 | s2_start = l2 - l2_cpy; |
| 140 | s1_start = l1 - l1_cpy; |
| 141 | |
| 142 | memcpy(dst, s1 + s1_start, l1_cpy); |
| 143 | memcpy(dst + l1_cpy, s2 + s2_start, l2_cpy); |
| 144 | |
Kees Cook | 3d6d8d2 | 2011-11-17 13:13:29 -0800 | [diff] [blame] | 145 | ret = psinfo->write(PSTORE_TYPE_DMESG, reason, &id, part, |
Matthew Garrett | 5628068 | 2011-07-21 16:57:53 -0400 | [diff] [blame] | 146 | hsize + l1_cpy + l2_cpy, psinfo); |
Chen Gong | b238b8f | 2011-10-12 09:17:24 -0700 | [diff] [blame] | 147 | if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted()) |
Luck, Tony | 6dda926 | 2011-08-11 15:14:39 -0700 | [diff] [blame] | 148 | pstore_new_entry = 1; |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 149 | l1 -= l1_cpy; |
| 150 | l2 -= l2_cpy; |
| 151 | total += l1_cpy + l2_cpy; |
Matthew Garrett | 5628068 | 2011-07-21 16:57:53 -0400 | [diff] [blame] | 152 | part++; |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 153 | } |
Don Zickus | abd4d55 | 2011-08-12 10:54:51 -0700 | [diff] [blame] | 154 | if (in_nmi()) { |
| 155 | if (is_locked) |
| 156 | spin_unlock(&psinfo->buf_lock); |
| 157 | } else |
| 158 | spin_unlock_irqrestore(&psinfo->buf_lock, flags); |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | static struct kmsg_dumper pstore_dumper = { |
| 162 | .dump = pstore_dump, |
| 163 | }; |
| 164 | |
Anton Vorontsov | f29e595 | 2012-05-26 06:20:19 -0700 | [diff] [blame] | 165 | #ifdef CONFIG_PSTORE_CONSOLE |
| 166 | static void pstore_console_write(struct console *con, const char *s, unsigned c) |
| 167 | { |
| 168 | const char *e = s + c; |
| 169 | |
| 170 | while (s < e) { |
| 171 | unsigned long flags; |
| 172 | |
| 173 | if (c > psinfo->bufsize) |
| 174 | c = psinfo->bufsize; |
| 175 | spin_lock_irqsave(&psinfo->buf_lock, flags); |
| 176 | memcpy(psinfo->buf, s, c); |
| 177 | psinfo->write(PSTORE_TYPE_CONSOLE, 0, NULL, 0, c, psinfo); |
| 178 | spin_unlock_irqrestore(&psinfo->buf_lock, flags); |
| 179 | s += c; |
| 180 | c = e - s; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | static struct console pstore_console = { |
| 185 | .name = "pstore", |
| 186 | .write = pstore_console_write, |
| 187 | .flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME, |
| 188 | .index = -1, |
| 189 | }; |
| 190 | |
| 191 | static void pstore_register_console(void) |
| 192 | { |
| 193 | register_console(&pstore_console); |
| 194 | } |
| 195 | #else |
| 196 | static void pstore_register_console(void) {} |
| 197 | #endif |
| 198 | |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 199 | /* |
| 200 | * platform specific persistent storage driver registers with |
| 201 | * us here. If pstore is already mounted, call the platform |
| 202 | * read function right away to populate the file system. If not |
| 203 | * then the pstore mount code will call us later to fill out |
| 204 | * the file system. |
| 205 | * |
| 206 | * Register with kmsg_dump to save last part of console log on panic. |
| 207 | */ |
| 208 | int pstore_register(struct pstore_info *psi) |
| 209 | { |
| 210 | struct module *owner = psi->owner; |
| 211 | |
| 212 | spin_lock(&pstore_lock); |
| 213 | if (psinfo) { |
| 214 | spin_unlock(&pstore_lock); |
| 215 | return -EBUSY; |
| 216 | } |
Matthew Garrett | dee28e7 | 2011-07-21 16:57:55 -0400 | [diff] [blame] | 217 | |
| 218 | if (backend && strcmp(backend, psi->name)) { |
| 219 | spin_unlock(&pstore_lock); |
| 220 | return -EINVAL; |
| 221 | } |
| 222 | |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 223 | psinfo = psi; |
Kees Cook | f6f8285 | 2011-11-17 12:58:07 -0800 | [diff] [blame] | 224 | mutex_init(&psinfo->read_mutex); |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 225 | spin_unlock(&pstore_lock); |
| 226 | |
| 227 | if (owner && !try_module_get(owner)) { |
| 228 | psinfo = NULL; |
| 229 | return -EINVAL; |
| 230 | } |
| 231 | |
| 232 | if (pstore_is_mounted()) |
Luck, Tony | 6dda926 | 2011-08-11 15:14:39 -0700 | [diff] [blame] | 233 | pstore_get_records(0); |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 234 | |
| 235 | kmsg_dump_register(&pstore_dumper); |
Anton Vorontsov | f29e595 | 2012-05-26 06:20:19 -0700 | [diff] [blame] | 236 | pstore_register_console(); |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 237 | |
Anton Vorontsov | a3f5f07 | 2012-05-26 06:20:28 -0700 | [diff] [blame^] | 238 | if (pstore_update_ms >= 0) { |
| 239 | pstore_timer.expires = jiffies + |
| 240 | msecs_to_jiffies(pstore_update_ms); |
| 241 | add_timer(&pstore_timer); |
| 242 | } |
Luck, Tony | 6dda926 | 2011-08-11 15:14:39 -0700 | [diff] [blame] | 243 | |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 244 | return 0; |
| 245 | } |
| 246 | EXPORT_SYMBOL_GPL(pstore_register); |
| 247 | |
| 248 | /* |
Luck, Tony | 6dda926 | 2011-08-11 15:14:39 -0700 | [diff] [blame] | 249 | * Read all the records from the persistent store. Create |
| 250 | * files in our filesystem. Don't warn about -EEXIST errors |
| 251 | * when we are re-scanning the backing store looking to add new |
| 252 | * error records. |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 253 | */ |
Luck, Tony | 6dda926 | 2011-08-11 15:14:39 -0700 | [diff] [blame] | 254 | void pstore_get_records(int quiet) |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 255 | { |
| 256 | struct pstore_info *psi = psinfo; |
Kees Cook | f6f8285 | 2011-11-17 12:58:07 -0800 | [diff] [blame] | 257 | char *buf = NULL; |
Chen Gong | 8d38d74 | 2011-05-16 10:58:57 -0700 | [diff] [blame] | 258 | ssize_t size; |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 259 | u64 id; |
| 260 | enum pstore_type_id type; |
| 261 | struct timespec time; |
Chen Gong | 06cf91b | 2011-05-16 11:00:27 -0700 | [diff] [blame] | 262 | int failed = 0, rc; |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 263 | |
| 264 | if (!psi) |
| 265 | return; |
| 266 | |
Kees Cook | f6f8285 | 2011-11-17 12:58:07 -0800 | [diff] [blame] | 267 | mutex_lock(&psi->read_mutex); |
Kees Cook | 2174f6d | 2011-11-18 13:49:00 -0800 | [diff] [blame] | 268 | if (psi->open && psi->open(psi)) |
Chen Gong | 06cf91b | 2011-05-16 11:00:27 -0700 | [diff] [blame] | 269 | goto out; |
| 270 | |
Kees Cook | f6f8285 | 2011-11-17 12:58:07 -0800 | [diff] [blame] | 271 | while ((size = psi->read(&id, &type, &time, &buf, psi)) > 0) { |
| 272 | rc = pstore_mkfile(type, psi->name, id, buf, (size_t)size, |
Luck, Tony | 6dda926 | 2011-08-11 15:14:39 -0700 | [diff] [blame] | 273 | time, psi); |
Kees Cook | f6f8285 | 2011-11-17 12:58:07 -0800 | [diff] [blame] | 274 | kfree(buf); |
| 275 | buf = NULL; |
Luck, Tony | 6dda926 | 2011-08-11 15:14:39 -0700 | [diff] [blame] | 276 | if (rc && (rc != -EEXIST || !quiet)) |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 277 | failed++; |
| 278 | } |
Kees Cook | 2174f6d | 2011-11-18 13:49:00 -0800 | [diff] [blame] | 279 | if (psi->close) |
| 280 | psi->close(psi); |
Chen Gong | 06cf91b | 2011-05-16 11:00:27 -0700 | [diff] [blame] | 281 | out: |
Kees Cook | f6f8285 | 2011-11-17 12:58:07 -0800 | [diff] [blame] | 282 | mutex_unlock(&psi->read_mutex); |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 283 | |
| 284 | if (failed) |
| 285 | printk(KERN_WARNING "pstore: failed to load %d record(s) from '%s'\n", |
| 286 | failed, psi->name); |
| 287 | } |
| 288 | |
Luck, Tony | 6dda926 | 2011-08-11 15:14:39 -0700 | [diff] [blame] | 289 | static void pstore_dowork(struct work_struct *work) |
| 290 | { |
| 291 | pstore_get_records(1); |
| 292 | } |
| 293 | |
| 294 | static void pstore_timefunc(unsigned long dummy) |
| 295 | { |
| 296 | if (pstore_new_entry) { |
| 297 | pstore_new_entry = 0; |
| 298 | schedule_work(&pstore_work); |
| 299 | } |
| 300 | |
Anton Vorontsov | a3f5f07 | 2012-05-26 06:20:28 -0700 | [diff] [blame^] | 301 | mod_timer(&pstore_timer, jiffies + msecs_to_jiffies(pstore_update_ms)); |
Luck, Tony | 6dda926 | 2011-08-11 15:14:39 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Matthew Garrett | dee28e7 | 2011-07-21 16:57:55 -0400 | [diff] [blame] | 304 | module_param(backend, charp, 0444); |
| 305 | MODULE_PARM_DESC(backend, "Pstore backend to use"); |