platform.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 = 60000;
  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 60000; -1 means runtime updates are disabled)");
  45. static int pstore_new_entry;
  46. static void pstore_timefunc(unsigned long);
  47. static DEFINE_TIMER(pstore_timer, pstore_timefunc, 0, 0);
  48. static void pstore_dowork(struct work_struct *);
  49. static DECLARE_WORK(pstore_work, pstore_dowork);
  50. /*
  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. static char *backend;
  57. /* How much of the console log to snapshot */
  58. static unsigned long kmsg_bytes = 10240;
  59. void pstore_set_kmsg_bytes(int bytes)
  60. {
  61. kmsg_bytes = bytes;
  62. }
  63. /* Tag each group of saved records with a sequence number */
  64. static int oopscount;
  65. static const char *get_reason_str(enum kmsg_dump_reason reason)
  66. {
  67. switch (reason) {
  68. case KMSG_DUMP_PANIC:
  69. return "Panic";
  70. case KMSG_DUMP_OOPS:
  71. return "Oops";
  72. case KMSG_DUMP_EMERG:
  73. return "Emergency";
  74. case KMSG_DUMP_RESTART:
  75. return "Restart";
  76. case KMSG_DUMP_HALT:
  77. return "Halt";
  78. case KMSG_DUMP_POWEROFF:
  79. return "Poweroff";
  80. default:
  81. return "Unknown";
  82. }
  83. }
  84. /*
  85. * callback from kmsg_dump. (s2,l2) has the most recently
  86. * written bytes, older bytes are in (s1,l1). Save as much
  87. * as we can from the end of the buffer.
  88. */
  89. static void pstore_dump(struct kmsg_dumper *dumper,
  90. enum kmsg_dump_reason reason,
  91. const char *s1, unsigned long l1,
  92. const char *s2, unsigned long l2)
  93. {
  94. unsigned long s1_start, s2_start;
  95. unsigned long l1_cpy, l2_cpy;
  96. unsigned long size, total = 0;
  97. char *dst;
  98. const char *why;
  99. u64 id;
  100. int hsize, ret;
  101. unsigned int part = 1;
  102. unsigned long flags = 0;
  103. int is_locked = 0;
  104. why = get_reason_str(reason);
  105. if (in_nmi()) {
  106. is_locked = spin_trylock(&psinfo->buf_lock);
  107. if (!is_locked)
  108. pr_err("pstore dump routine blocked in NMI, may corrupt error record\n");
  109. } else
  110. spin_lock_irqsave(&psinfo->buf_lock, flags);
  111. oopscount++;
  112. while (total < kmsg_bytes) {
  113. dst = psinfo->buf;
  114. hsize = sprintf(dst, "%s#%d Part%d\n", why, oopscount, part);
  115. size = psinfo->bufsize - hsize;
  116. dst += hsize;
  117. l2_cpy = min(l2, size);
  118. l1_cpy = min(l1, size - l2_cpy);
  119. if (l1_cpy + l2_cpy == 0)
  120. break;
  121. s2_start = l2 - l2_cpy;
  122. s1_start = l1 - l1_cpy;
  123. memcpy(dst, s1 + s1_start, l1_cpy);
  124. memcpy(dst + l1_cpy, s2 + s2_start, l2_cpy);
  125. ret = psinfo->write(PSTORE_TYPE_DMESG, reason, &id, part,
  126. hsize + l1_cpy + l2_cpy, psinfo);
  127. if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted())
  128. pstore_new_entry = 1;
  129. l1 -= l1_cpy;
  130. l2 -= l2_cpy;
  131. total += l1_cpy + l2_cpy;
  132. part++;
  133. }
  134. if (in_nmi()) {
  135. if (is_locked)
  136. spin_unlock(&psinfo->buf_lock);
  137. } else
  138. spin_unlock_irqrestore(&psinfo->buf_lock, flags);
  139. }
  140. static struct kmsg_dumper pstore_dumper = {
  141. .dump = pstore_dump,
  142. };
  143. #ifdef CONFIG_PSTORE_CONSOLE
  144. static void pstore_console_write(struct console *con, const char *s, unsigned c)
  145. {
  146. const char *e = s + c;
  147. while (s < e) {
  148. unsigned long flags;
  149. if (c > psinfo->bufsize)
  150. c = psinfo->bufsize;
  151. spin_lock_irqsave(&psinfo->buf_lock, flags);
  152. memcpy(psinfo->buf, s, c);
  153. psinfo->write(PSTORE_TYPE_CONSOLE, 0, NULL, 0, c, psinfo);
  154. spin_unlock_irqrestore(&psinfo->buf_lock, flags);
  155. s += c;
  156. c = e - s;
  157. }
  158. }
  159. static struct console pstore_console = {
  160. .name = "pstore",
  161. .write = pstore_console_write,
  162. .flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME,
  163. .index = -1,
  164. };
  165. static void pstore_register_console(void)
  166. {
  167. register_console(&pstore_console);
  168. }
  169. #else
  170. static void pstore_register_console(void) {}
  171. #endif
  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. psinfo = psi;
  194. mutex_init(&psinfo->read_mutex);
  195. spin_unlock(&pstore_lock);
  196. if (owner && !try_module_get(owner)) {
  197. psinfo = NULL;
  198. return -EINVAL;
  199. }
  200. if (pstore_is_mounted())
  201. pstore_get_records(0);
  202. kmsg_dump_register(&pstore_dumper);
  203. pstore_register_console();
  204. if (pstore_update_ms >= 0) {
  205. pstore_timer.expires = jiffies +
  206. msecs_to_jiffies(pstore_update_ms);
  207. add_timer(&pstore_timer);
  208. }
  209. return 0;
  210. }
  211. EXPORT_SYMBOL_GPL(pstore_register);
  212. /*
  213. * Read all the records from the persistent store. Create
  214. * files in our filesystem. Don't warn about -EEXIST errors
  215. * when we are re-scanning the backing store looking to add new
  216. * error records.
  217. */
  218. void pstore_get_records(int quiet)
  219. {
  220. struct pstore_info *psi = psinfo;
  221. char *buf = NULL;
  222. ssize_t size;
  223. u64 id;
  224. enum pstore_type_id type;
  225. struct timespec time;
  226. int failed = 0, rc;
  227. if (!psi)
  228. return;
  229. mutex_lock(&psi->read_mutex);
  230. if (psi->open && psi->open(psi))
  231. goto out;
  232. while ((size = psi->read(&id, &type, &time, &buf, psi)) > 0) {
  233. rc = pstore_mkfile(type, psi->name, id, buf, (size_t)size,
  234. time, psi);
  235. kfree(buf);
  236. buf = NULL;
  237. if (rc && (rc != -EEXIST || !quiet))
  238. failed++;
  239. }
  240. if (psi->close)
  241. psi->close(psi);
  242. out:
  243. mutex_unlock(&psi->read_mutex);
  244. if (failed)
  245. printk(KERN_WARNING "pstore: failed to load %d record(s) from '%s'\n",
  246. failed, psi->name);
  247. }
  248. static void pstore_dowork(struct work_struct *work)
  249. {
  250. pstore_get_records(1);
  251. }
  252. static void pstore_timefunc(unsigned long dummy)
  253. {
  254. if (pstore_new_entry) {
  255. pstore_new_entry = 0;
  256. schedule_work(&pstore_work);
  257. }
  258. mod_timer(&pstore_timer, jiffies + msecs_to_jiffies(pstore_update_ms));
  259. }
  260. module_param(backend, charp, 0444);
  261. MODULE_PARM_DESC(backend, "Pstore backend to use");