platform.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Persistent Storage - platform driver interface parts.
  3. *
  4. * Copyright (C) 2007-2008 Google, Inc.
  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. #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/console.h>
  26. #include <linux/module.h>
  27. #include <linux/pstore.h>
  28. #include <linux/string.h>
  29. #include <linux/timer.h>
  30. #include <linux/slab.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/hardirq.h>
  33. #include <linux/jiffies.h>
  34. #include <linux/workqueue.h>
  35. #include "internal.h"
  36. /*
  37. * We defer making "oops" entries appear in pstore - see
  38. * whether the system is actually still running well enough
  39. * to let someone see the entry
  40. */
  41. static int pstore_update_ms = -1;
  42. module_param_named(update_ms, pstore_update_ms, int, 0600);
  43. MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content "
  44. "(default is -1, which means runtime updates are disabled; "
  45. "enabling this option is not safe, it may lead to further "
  46. "corruption on Oopses)");
  47. static int pstore_new_entry;
  48. static void pstore_timefunc(unsigned long);
  49. static DEFINE_TIMER(pstore_timer, pstore_timefunc, 0, 0);
  50. static void pstore_dowork(struct work_struct *);
  51. static DECLARE_WORK(pstore_work, pstore_dowork);
  52. /*
  53. * pstore_lock just protects "psinfo" during
  54. * calls to pstore_register()
  55. */
  56. static DEFINE_SPINLOCK(pstore_lock);
  57. struct pstore_info *psinfo;
  58. static char *backend;
  59. /* How much of the console log to snapshot */
  60. static unsigned long kmsg_bytes = 10240;
  61. void pstore_set_kmsg_bytes(int bytes)
  62. {
  63. kmsg_bytes = bytes;
  64. }
  65. /* Tag each group of saved records with a sequence number */
  66. static int oopscount;
  67. static const char *get_reason_str(enum kmsg_dump_reason reason)
  68. {
  69. switch (reason) {
  70. case KMSG_DUMP_PANIC:
  71. return "Panic";
  72. case KMSG_DUMP_OOPS:
  73. return "Oops";
  74. case KMSG_DUMP_EMERG:
  75. return "Emergency";
  76. case KMSG_DUMP_RESTART:
  77. return "Restart";
  78. case KMSG_DUMP_HALT:
  79. return "Halt";
  80. case KMSG_DUMP_POWEROFF:
  81. return "Poweroff";
  82. default:
  83. return "Unknown";
  84. }
  85. }
  86. /*
  87. * callback from kmsg_dump. (s2,l2) has the most recently
  88. * written bytes, older bytes are in (s1,l1). Save as much
  89. * as we can from the end of the buffer.
  90. */
  91. static void pstore_dump(struct kmsg_dumper *dumper,
  92. enum kmsg_dump_reason reason)
  93. {
  94. unsigned long total = 0;
  95. const char *why;
  96. u64 id;
  97. unsigned int part = 1;
  98. unsigned long flags = 0;
  99. int is_locked = 0;
  100. int ret;
  101. why = get_reason_str(reason);
  102. if (in_nmi()) {
  103. is_locked = spin_trylock(&psinfo->buf_lock);
  104. if (!is_locked)
  105. pr_err("pstore dump routine blocked in NMI, may corrupt error record\n");
  106. } else
  107. spin_lock_irqsave(&psinfo->buf_lock, flags);
  108. oopscount++;
  109. while (total < kmsg_bytes) {
  110. char *dst;
  111. unsigned long size;
  112. int hsize;
  113. size_t len;
  114. dst = psinfo->buf;
  115. hsize = sprintf(dst, "%s#%d Part%d\n", why, oopscount, part);
  116. size = psinfo->bufsize - hsize;
  117. dst += hsize;
  118. if (!kmsg_dump_get_buffer(dumper, true, dst, size, &len))
  119. break;
  120. ret = psinfo->write(PSTORE_TYPE_DMESG, reason, &id, part,
  121. hsize + len, psinfo);
  122. if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted())
  123. pstore_new_entry = 1;
  124. total += hsize + len;
  125. part++;
  126. }
  127. if (in_nmi()) {
  128. if (is_locked)
  129. spin_unlock(&psinfo->buf_lock);
  130. } else
  131. spin_unlock_irqrestore(&psinfo->buf_lock, flags);
  132. }
  133. static struct kmsg_dumper pstore_dumper = {
  134. .dump = pstore_dump,
  135. };
  136. #ifdef CONFIG_PSTORE_CONSOLE
  137. static void pstore_console_write(struct console *con, const char *s, unsigned c)
  138. {
  139. const char *e = s + c;
  140. while (s < e) {
  141. unsigned long flags;
  142. if (c > psinfo->bufsize)
  143. c = psinfo->bufsize;
  144. spin_lock_irqsave(&psinfo->buf_lock, flags);
  145. memcpy(psinfo->buf, s, c);
  146. psinfo->write(PSTORE_TYPE_CONSOLE, 0, NULL, 0, c, psinfo);
  147. spin_unlock_irqrestore(&psinfo->buf_lock, flags);
  148. s += c;
  149. c = e - s;
  150. }
  151. }
  152. static struct console pstore_console = {
  153. .name = "pstore",
  154. .write = pstore_console_write,
  155. .flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME,
  156. .index = -1,
  157. };
  158. static void pstore_register_console(void)
  159. {
  160. register_console(&pstore_console);
  161. }
  162. #else
  163. static void pstore_register_console(void) {}
  164. #endif
  165. static int pstore_write_compat(enum pstore_type_id type,
  166. enum kmsg_dump_reason reason,
  167. u64 *id, unsigned int part,
  168. size_t size, struct pstore_info *psi)
  169. {
  170. return psi->write_buf(type, reason, id, part, psinfo->buf, size, psi);
  171. }
  172. /*
  173. * platform specific persistent storage driver registers with
  174. * us here. If pstore is already mounted, call the platform
  175. * read function right away to populate the file system. If not
  176. * then the pstore mount code will call us later to fill out
  177. * the file system.
  178. *
  179. * Register with kmsg_dump to save last part of console log on panic.
  180. */
  181. int pstore_register(struct pstore_info *psi)
  182. {
  183. struct module *owner = psi->owner;
  184. spin_lock(&pstore_lock);
  185. if (psinfo) {
  186. spin_unlock(&pstore_lock);
  187. return -EBUSY;
  188. }
  189. if (backend && strcmp(backend, psi->name)) {
  190. spin_unlock(&pstore_lock);
  191. return -EINVAL;
  192. }
  193. if (!psi->write)
  194. psi->write = pstore_write_compat;
  195. psinfo = psi;
  196. mutex_init(&psinfo->read_mutex);
  197. spin_unlock(&pstore_lock);
  198. if (owner && !try_module_get(owner)) {
  199. psinfo = NULL;
  200. return -EINVAL;
  201. }
  202. if (pstore_is_mounted())
  203. pstore_get_records(0);
  204. kmsg_dump_register(&pstore_dumper);
  205. pstore_register_console();
  206. if (pstore_update_ms >= 0) {
  207. pstore_timer.expires = jiffies +
  208. msecs_to_jiffies(pstore_update_ms);
  209. add_timer(&pstore_timer);
  210. }
  211. return 0;
  212. }
  213. EXPORT_SYMBOL_GPL(pstore_register);
  214. /*
  215. * Read all the records from the persistent store. Create
  216. * files in our filesystem. Don't warn about -EEXIST errors
  217. * when we are re-scanning the backing store looking to add new
  218. * error records.
  219. */
  220. void pstore_get_records(int quiet)
  221. {
  222. struct pstore_info *psi = psinfo;
  223. char *buf = NULL;
  224. ssize_t size;
  225. u64 id;
  226. enum pstore_type_id type;
  227. struct timespec time;
  228. int failed = 0, rc;
  229. if (!psi)
  230. return;
  231. mutex_lock(&psi->read_mutex);
  232. if (psi->open && psi->open(psi))
  233. goto out;
  234. while ((size = psi->read(&id, &type, &time, &buf, psi)) > 0) {
  235. rc = pstore_mkfile(type, psi->name, id, buf, (size_t)size,
  236. time, psi);
  237. kfree(buf);
  238. buf = NULL;
  239. if (rc && (rc != -EEXIST || !quiet))
  240. failed++;
  241. }
  242. if (psi->close)
  243. psi->close(psi);
  244. out:
  245. mutex_unlock(&psi->read_mutex);
  246. if (failed)
  247. printk(KERN_WARNING "pstore: failed to load %d record(s) from '%s'\n",
  248. failed, psi->name);
  249. }
  250. static void pstore_dowork(struct work_struct *work)
  251. {
  252. pstore_get_records(1);
  253. }
  254. static void pstore_timefunc(unsigned long dummy)
  255. {
  256. if (pstore_new_entry) {
  257. pstore_new_entry = 0;
  258. schedule_work(&pstore_work);
  259. }
  260. mod_timer(&pstore_timer, jiffies + msecs_to_jiffies(pstore_update_ms));
  261. }
  262. module_param(backend, charp, 0444);
  263. MODULE_PARM_DESC(backend, "Pstore backend to use");