platform.c 5.9 KB

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