platform.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 const char *get_reason_str(enum kmsg_dump_reason reason)
  60. {
  61. switch (reason) {
  62. case KMSG_DUMP_PANIC:
  63. return "Panic";
  64. case KMSG_DUMP_OOPS:
  65. return "Oops";
  66. case KMSG_DUMP_EMERG:
  67. return "Emergency";
  68. case KMSG_DUMP_RESTART:
  69. return "Restart";
  70. case KMSG_DUMP_HALT:
  71. return "Halt";
  72. case KMSG_DUMP_POWEROFF:
  73. return "Poweroff";
  74. default:
  75. return "Unknown";
  76. }
  77. }
  78. /*
  79. * callback from kmsg_dump. (s2,l2) has the most recently
  80. * written bytes, older bytes are in (s1,l1). Save as much
  81. * as we can from the end of the buffer.
  82. */
  83. static void pstore_dump(struct kmsg_dumper *dumper,
  84. enum kmsg_dump_reason reason,
  85. const char *s1, unsigned long l1,
  86. const char *s2, unsigned long l2)
  87. {
  88. unsigned long s1_start, s2_start;
  89. unsigned long l1_cpy, l2_cpy;
  90. unsigned long size, total = 0;
  91. char *dst;
  92. const char *why;
  93. u64 id;
  94. int hsize, ret;
  95. unsigned int part = 1;
  96. unsigned long flags = 0;
  97. int is_locked = 0;
  98. why = get_reason_str(reason);
  99. if (in_nmi()) {
  100. is_locked = spin_trylock(&psinfo->buf_lock);
  101. if (!is_locked)
  102. pr_err("pstore dump routine blocked in NMI, may corrupt error record\n");
  103. } else
  104. spin_lock_irqsave(&psinfo->buf_lock, flags);
  105. oopscount++;
  106. while (total < kmsg_bytes) {
  107. dst = psinfo->buf;
  108. hsize = sprintf(dst, "%s#%d Part%d\n", why, oopscount, part);
  109. size = psinfo->bufsize - hsize;
  110. dst += hsize;
  111. l2_cpy = min(l2, size);
  112. l1_cpy = min(l1, size - l2_cpy);
  113. if (l1_cpy + l2_cpy == 0)
  114. break;
  115. s2_start = l2 - l2_cpy;
  116. s1_start = l1 - l1_cpy;
  117. memcpy(dst, s1 + s1_start, l1_cpy);
  118. memcpy(dst + l1_cpy, s2 + s2_start, l2_cpy);
  119. ret = psinfo->write(PSTORE_TYPE_DMESG, reason, &id, part,
  120. hsize + l1_cpy + l2_cpy, psinfo);
  121. if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted())
  122. pstore_new_entry = 1;
  123. l1 -= l1_cpy;
  124. l2 -= l2_cpy;
  125. total += l1_cpy + l2_cpy;
  126. part++;
  127. }
  128. if (in_nmi()) {
  129. if (is_locked)
  130. spin_unlock(&psinfo->buf_lock);
  131. } else
  132. spin_unlock_irqrestore(&psinfo->buf_lock, flags);
  133. }
  134. static struct kmsg_dumper pstore_dumper = {
  135. .dump = pstore_dump,
  136. };
  137. /*
  138. * platform specific persistent storage driver registers with
  139. * us here. If pstore is already mounted, call the platform
  140. * read function right away to populate the file system. If not
  141. * then the pstore mount code will call us later to fill out
  142. * the file system.
  143. *
  144. * Register with kmsg_dump to save last part of console log on panic.
  145. */
  146. int pstore_register(struct pstore_info *psi)
  147. {
  148. struct module *owner = psi->owner;
  149. spin_lock(&pstore_lock);
  150. if (psinfo) {
  151. spin_unlock(&pstore_lock);
  152. return -EBUSY;
  153. }
  154. if (backend && strcmp(backend, psi->name)) {
  155. spin_unlock(&pstore_lock);
  156. return -EINVAL;
  157. }
  158. psinfo = psi;
  159. mutex_init(&psinfo->read_mutex);
  160. spin_unlock(&pstore_lock);
  161. if (owner && !try_module_get(owner)) {
  162. psinfo = NULL;
  163. return -EINVAL;
  164. }
  165. if (pstore_is_mounted())
  166. pstore_get_records(0);
  167. kmsg_dump_register(&pstore_dumper);
  168. pstore_timer.expires = jiffies + PSTORE_INTERVAL;
  169. add_timer(&pstore_timer);
  170. return 0;
  171. }
  172. EXPORT_SYMBOL_GPL(pstore_register);
  173. /*
  174. * Read all the records from the persistent store. Create
  175. * files in our filesystem. Don't warn about -EEXIST errors
  176. * when we are re-scanning the backing store looking to add new
  177. * error records.
  178. */
  179. void pstore_get_records(int quiet)
  180. {
  181. struct pstore_info *psi = psinfo;
  182. char *buf = NULL;
  183. ssize_t size;
  184. u64 id;
  185. enum pstore_type_id type;
  186. struct timespec time;
  187. int failed = 0, rc;
  188. if (!psi)
  189. return;
  190. mutex_lock(&psi->read_mutex);
  191. if (psi->open && psi->open(psi))
  192. goto out;
  193. while ((size = psi->read(&id, &type, &time, &buf, psi)) > 0) {
  194. rc = pstore_mkfile(type, psi->name, id, buf, (size_t)size,
  195. time, psi);
  196. kfree(buf);
  197. buf = NULL;
  198. if (rc && (rc != -EEXIST || !quiet))
  199. failed++;
  200. }
  201. if (psi->close)
  202. psi->close(psi);
  203. out:
  204. mutex_unlock(&psi->read_mutex);
  205. if (failed)
  206. printk(KERN_WARNING "pstore: failed to load %d record(s) from '%s'\n",
  207. failed, psi->name);
  208. }
  209. static void pstore_dowork(struct work_struct *work)
  210. {
  211. pstore_get_records(1);
  212. }
  213. static void pstore_timefunc(unsigned long dummy)
  214. {
  215. if (pstore_new_entry) {
  216. pstore_new_entry = 0;
  217. schedule_work(&pstore_work);
  218. }
  219. mod_timer(&pstore_timer, jiffies + PSTORE_INTERVAL);
  220. }
  221. module_param(backend, charp, 0444);
  222. MODULE_PARM_DESC(backend, "Pstore backend to use");