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