platform.c 5.0 KB

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