platform.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. #include <linux/atomic.h>
  20. #include <linux/types.h>
  21. #include <linux/errno.h>
  22. #include <linux/init.h>
  23. #include <linux/kmsg_dump.h>
  24. #include <linux/module.h>
  25. #include <linux/pstore.h>
  26. #include <linux/string.h>
  27. #include <linux/timer.h>
  28. #include <linux/slab.h>
  29. #include <linux/uaccess.h>
  30. #include <linux/hardirq.h>
  31. #include <linux/workqueue.h>
  32. #include "internal.h"
  33. /*
  34. * We defer making "oops" entries appear in pstore - see
  35. * whether the system is actually still running well enough
  36. * to let someone see the entry
  37. */
  38. #define PSTORE_INTERVAL (60 * HZ)
  39. static int pstore_new_entry;
  40. static void pstore_timefunc(unsigned long);
  41. static DEFINE_TIMER(pstore_timer, pstore_timefunc, 0, 0);
  42. static void pstore_dowork(struct work_struct *);
  43. static DECLARE_WORK(pstore_work, pstore_dowork);
  44. /*
  45. * pstore_lock just protects "psinfo" during
  46. * calls to pstore_register()
  47. */
  48. static DEFINE_SPINLOCK(pstore_lock);
  49. static struct pstore_info *psinfo;
  50. static char *backend;
  51. /* How much of the console log to snapshot */
  52. static unsigned long kmsg_bytes = 10240;
  53. void pstore_set_kmsg_bytes(int bytes)
  54. {
  55. kmsg_bytes = bytes;
  56. }
  57. /* Tag each group of saved records with a sequence number */
  58. static int oopscount;
  59. static char *reason_str[] = {
  60. "Oops", "Panic", "Kexec", "Restart", "Halt", "Poweroff", "Emergency"
  61. };
  62. /*
  63. * callback from kmsg_dump. (s2,l2) has the most recently
  64. * written bytes, older bytes are in (s1,l1). Save as much
  65. * as we can from the end of the buffer.
  66. */
  67. static void pstore_dump(struct kmsg_dumper *dumper,
  68. enum kmsg_dump_reason reason,
  69. const char *s1, unsigned long l1,
  70. const char *s2, unsigned long l2)
  71. {
  72. unsigned long s1_start, s2_start;
  73. unsigned long l1_cpy, l2_cpy;
  74. unsigned long size, total = 0;
  75. char *dst, *why;
  76. u64 id;
  77. int hsize, ret;
  78. unsigned int part = 1;
  79. unsigned long flags = 0;
  80. int is_locked = 0;
  81. if (reason < ARRAY_SIZE(reason_str))
  82. why = reason_str[reason];
  83. else
  84. why = "Unknown";
  85. if (in_nmi()) {
  86. is_locked = spin_trylock(&psinfo->buf_lock);
  87. if (!is_locked)
  88. pr_err("pstore dump routine blocked in NMI, may corrupt error record\n");
  89. } else
  90. spin_lock_irqsave(&psinfo->buf_lock, flags);
  91. oopscount++;
  92. while (total < kmsg_bytes) {
  93. dst = psinfo->buf;
  94. hsize = sprintf(dst, "%s#%d Part%d\n", why, oopscount, part);
  95. size = psinfo->bufsize - hsize;
  96. dst += hsize;
  97. l2_cpy = min(l2, size);
  98. l1_cpy = min(l1, size - l2_cpy);
  99. if (l1_cpy + l2_cpy == 0)
  100. break;
  101. s2_start = l2 - l2_cpy;
  102. s1_start = l1 - l1_cpy;
  103. memcpy(dst, s1 + s1_start, l1_cpy);
  104. memcpy(dst + l1_cpy, s2 + s2_start, l2_cpy);
  105. ret = psinfo->write(PSTORE_TYPE_DMESG, &id, part,
  106. hsize + l1_cpy + l2_cpy, psinfo);
  107. if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted())
  108. pstore_new_entry = 1;
  109. l1 -= l1_cpy;
  110. l2 -= l2_cpy;
  111. total += l1_cpy + l2_cpy;
  112. part++;
  113. }
  114. if (in_nmi()) {
  115. if (is_locked)
  116. spin_unlock(&psinfo->buf_lock);
  117. } else
  118. spin_unlock_irqrestore(&psinfo->buf_lock, flags);
  119. }
  120. static struct kmsg_dumper pstore_dumper = {
  121. .dump = pstore_dump,
  122. };
  123. /*
  124. * platform specific persistent storage driver registers with
  125. * us here. If pstore is already mounted, call the platform
  126. * read function right away to populate the file system. If not
  127. * then the pstore mount code will call us later to fill out
  128. * the file system.
  129. *
  130. * Register with kmsg_dump to save last part of console log on panic.
  131. */
  132. int pstore_register(struct pstore_info *psi)
  133. {
  134. struct module *owner = psi->owner;
  135. spin_lock(&pstore_lock);
  136. if (psinfo) {
  137. spin_unlock(&pstore_lock);
  138. return -EBUSY;
  139. }
  140. if (backend && strcmp(backend, psi->name)) {
  141. spin_unlock(&pstore_lock);
  142. return -EINVAL;
  143. }
  144. psinfo = psi;
  145. spin_unlock(&pstore_lock);
  146. if (owner && !try_module_get(owner)) {
  147. psinfo = NULL;
  148. return -EINVAL;
  149. }
  150. if (pstore_is_mounted())
  151. pstore_get_records(0);
  152. kmsg_dump_register(&pstore_dumper);
  153. pstore_timer.expires = jiffies + PSTORE_INTERVAL;
  154. add_timer(&pstore_timer);
  155. return 0;
  156. }
  157. EXPORT_SYMBOL_GPL(pstore_register);
  158. /*
  159. * Read all the records from the persistent store. Create
  160. * files in our filesystem. Don't warn about -EEXIST errors
  161. * when we are re-scanning the backing store looking to add new
  162. * error records.
  163. */
  164. void pstore_get_records(int quiet)
  165. {
  166. struct pstore_info *psi = psinfo;
  167. ssize_t size;
  168. u64 id;
  169. enum pstore_type_id type;
  170. struct timespec time;
  171. int failed = 0, rc;
  172. unsigned long flags;
  173. if (!psi)
  174. return;
  175. spin_lock_irqsave(&psinfo->buf_lock, flags);
  176. rc = psi->open(psi);
  177. if (rc)
  178. goto out;
  179. while ((size = psi->read(&id, &type, &time, psi)) > 0) {
  180. rc = pstore_mkfile(type, psi->name, id, psi->buf, (size_t)size,
  181. time, psi);
  182. if (rc && (rc != -EEXIST || !quiet))
  183. failed++;
  184. }
  185. psi->close(psi);
  186. out:
  187. spin_unlock_irqrestore(&psinfo->buf_lock, flags);
  188. if (failed)
  189. printk(KERN_WARNING "pstore: failed to load %d record(s) from '%s'\n",
  190. failed, psi->name);
  191. }
  192. static void pstore_dowork(struct work_struct *work)
  193. {
  194. pstore_get_records(1);
  195. }
  196. static void pstore_timefunc(unsigned long dummy)
  197. {
  198. if (pstore_new_entry) {
  199. pstore_new_entry = 0;
  200. schedule_work(&pstore_work);
  201. }
  202. mod_timer(&pstore_timer, jiffies + PSTORE_INTERVAL);
  203. }
  204. /*
  205. * Call platform driver to write a record to the
  206. * persistent store.
  207. */
  208. int pstore_write(enum pstore_type_id type, char *buf, size_t size)
  209. {
  210. u64 id;
  211. int ret;
  212. unsigned long flags;
  213. if (!psinfo)
  214. return -ENODEV;
  215. if (size > psinfo->bufsize)
  216. return -EFBIG;
  217. spin_lock_irqsave(&psinfo->buf_lock, flags);
  218. memcpy(psinfo->buf, buf, size);
  219. ret = psinfo->write(type, &id, 0, size, psinfo);
  220. if (ret == 0 && pstore_is_mounted())
  221. pstore_mkfile(PSTORE_TYPE_DMESG, psinfo->name, id, psinfo->buf,
  222. size, CURRENT_TIME, psinfo);
  223. spin_unlock_irqrestore(&psinfo->buf_lock, flags);
  224. return 0;
  225. }
  226. EXPORT_SYMBOL_GPL(pstore_write);
  227. module_param(backend, charp, 0444);
  228. MODULE_PARM_DESC(backend, "Pstore backend to use");