platform.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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/slab.h>
  28. #include <linux/uaccess.h>
  29. #include "internal.h"
  30. /*
  31. * pstore_lock just protects "psinfo" during
  32. * calls to pstore_register()
  33. */
  34. static DEFINE_SPINLOCK(pstore_lock);
  35. static struct pstore_info *psinfo;
  36. static char *backend;
  37. /* How much of the console log to snapshot */
  38. static unsigned long kmsg_bytes = 10240;
  39. void pstore_set_kmsg_bytes(int bytes)
  40. {
  41. kmsg_bytes = bytes;
  42. }
  43. /* Tag each group of saved records with a sequence number */
  44. static int oopscount;
  45. static char *reason_str[] = {
  46. "Oops", "Panic", "Kexec", "Restart", "Halt", "Poweroff", "Emergency"
  47. };
  48. /*
  49. * callback from kmsg_dump. (s2,l2) has the most recently
  50. * written bytes, older bytes are in (s1,l1). Save as much
  51. * as we can from the end of the buffer.
  52. */
  53. static void pstore_dump(struct kmsg_dumper *dumper,
  54. enum kmsg_dump_reason reason,
  55. const char *s1, unsigned long l1,
  56. const char *s2, unsigned long l2)
  57. {
  58. unsigned long s1_start, s2_start;
  59. unsigned long l1_cpy, l2_cpy;
  60. unsigned long size, total = 0;
  61. char *dst, *why;
  62. u64 id;
  63. int hsize;
  64. unsigned int part = 1;
  65. if (reason < ARRAY_SIZE(reason_str))
  66. why = reason_str[reason];
  67. else
  68. why = "Unknown";
  69. mutex_lock(&psinfo->buf_mutex);
  70. oopscount++;
  71. while (total < kmsg_bytes) {
  72. dst = psinfo->buf;
  73. hsize = sprintf(dst, "%s#%d Part%d\n", why, oopscount, part);
  74. size = psinfo->bufsize - hsize;
  75. dst += hsize;
  76. l2_cpy = min(l2, size);
  77. l1_cpy = min(l1, size - l2_cpy);
  78. if (l1_cpy + l2_cpy == 0)
  79. break;
  80. s2_start = l2 - l2_cpy;
  81. s1_start = l1 - l1_cpy;
  82. memcpy(dst, s1 + s1_start, l1_cpy);
  83. memcpy(dst + l1_cpy, s2 + s2_start, l2_cpy);
  84. id = psinfo->write(PSTORE_TYPE_DMESG, part,
  85. hsize + l1_cpy + l2_cpy, psinfo);
  86. if (reason == KMSG_DUMP_OOPS && pstore_is_mounted())
  87. pstore_mkfile(PSTORE_TYPE_DMESG, psinfo->name, id,
  88. psinfo->buf, hsize + l1_cpy + l2_cpy,
  89. CURRENT_TIME, psinfo);
  90. l1 -= l1_cpy;
  91. l2 -= l2_cpy;
  92. total += l1_cpy + l2_cpy;
  93. part++;
  94. }
  95. mutex_unlock(&psinfo->buf_mutex);
  96. }
  97. static struct kmsg_dumper pstore_dumper = {
  98. .dump = pstore_dump,
  99. };
  100. /*
  101. * platform specific persistent storage driver registers with
  102. * us here. If pstore is already mounted, call the platform
  103. * read function right away to populate the file system. If not
  104. * then the pstore mount code will call us later to fill out
  105. * the file system.
  106. *
  107. * Register with kmsg_dump to save last part of console log on panic.
  108. */
  109. int pstore_register(struct pstore_info *psi)
  110. {
  111. struct module *owner = psi->owner;
  112. spin_lock(&pstore_lock);
  113. if (psinfo) {
  114. spin_unlock(&pstore_lock);
  115. return -EBUSY;
  116. }
  117. if (backend && strcmp(backend, psi->name)) {
  118. spin_unlock(&pstore_lock);
  119. return -EINVAL;
  120. }
  121. psinfo = psi;
  122. spin_unlock(&pstore_lock);
  123. if (owner && !try_module_get(owner)) {
  124. psinfo = NULL;
  125. return -EINVAL;
  126. }
  127. if (pstore_is_mounted())
  128. pstore_get_records();
  129. kmsg_dump_register(&pstore_dumper);
  130. return 0;
  131. }
  132. EXPORT_SYMBOL_GPL(pstore_register);
  133. /*
  134. * Read all the records from the persistent store. Create and
  135. * file files in our filesystem.
  136. */
  137. void pstore_get_records(void)
  138. {
  139. struct pstore_info *psi = psinfo;
  140. ssize_t size;
  141. u64 id;
  142. enum pstore_type_id type;
  143. struct timespec time;
  144. int failed = 0, rc;
  145. if (!psi)
  146. return;
  147. mutex_lock(&psinfo->buf_mutex);
  148. rc = psi->open(psi);
  149. if (rc)
  150. goto out;
  151. while ((size = psi->read(&id, &type, &time, psi)) > 0) {
  152. if (pstore_mkfile(type, psi->name, id, psi->buf, (size_t)size,
  153. time, psi))
  154. failed++;
  155. }
  156. psi->close(psi);
  157. out:
  158. mutex_unlock(&psinfo->buf_mutex);
  159. if (failed)
  160. printk(KERN_WARNING "pstore: failed to load %d record(s) from '%s'\n",
  161. failed, psi->name);
  162. }
  163. /*
  164. * Call platform driver to write a record to the
  165. * persistent store.
  166. */
  167. int pstore_write(enum pstore_type_id type, char *buf, size_t size)
  168. {
  169. u64 id;
  170. if (!psinfo)
  171. return -ENODEV;
  172. if (size > psinfo->bufsize)
  173. return -EFBIG;
  174. mutex_lock(&psinfo->buf_mutex);
  175. memcpy(psinfo->buf, buf, size);
  176. id = psinfo->write(type, 0, size, psinfo);
  177. if (pstore_is_mounted())
  178. pstore_mkfile(PSTORE_TYPE_DMESG, psinfo->name, id, psinfo->buf,
  179. size, CURRENT_TIME, psinfo);
  180. mutex_unlock(&psinfo->buf_mutex);
  181. return 0;
  182. }
  183. EXPORT_SYMBOL_GPL(pstore_write);
  184. module_param(backend, charp, 0444);
  185. MODULE_PARM_DESC(backend, "Pstore backend to use");