platform.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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, hsize + l1_cpy + l2_cpy);
  83. if (reason == KMSG_DUMP_OOPS && pstore_is_mounted())
  84. pstore_mkfile(PSTORE_TYPE_DMESG, psinfo->name, id,
  85. psinfo->buf, hsize + l1_cpy + l2_cpy,
  86. CURRENT_TIME, psinfo->erase);
  87. l1 -= l1_cpy;
  88. l2 -= l2_cpy;
  89. total += l1_cpy + l2_cpy;
  90. }
  91. mutex_unlock(&psinfo->buf_mutex);
  92. }
  93. static struct kmsg_dumper pstore_dumper = {
  94. .dump = pstore_dump,
  95. };
  96. /*
  97. * platform specific persistent storage driver registers with
  98. * us here. If pstore is already mounted, call the platform
  99. * read function right away to populate the file system. If not
  100. * then the pstore mount code will call us later to fill out
  101. * the file system.
  102. *
  103. * Register with kmsg_dump to save last part of console log on panic.
  104. */
  105. int pstore_register(struct pstore_info *psi)
  106. {
  107. struct module *owner = psi->owner;
  108. spin_lock(&pstore_lock);
  109. if (psinfo) {
  110. spin_unlock(&pstore_lock);
  111. return -EBUSY;
  112. }
  113. psinfo = psi;
  114. spin_unlock(&pstore_lock);
  115. if (owner && !try_module_get(owner)) {
  116. psinfo = NULL;
  117. return -EINVAL;
  118. }
  119. if (pstore_is_mounted())
  120. pstore_get_records();
  121. kmsg_dump_register(&pstore_dumper);
  122. return 0;
  123. }
  124. EXPORT_SYMBOL_GPL(pstore_register);
  125. /*
  126. * Read all the records from the persistent store. Create and
  127. * file files in our filesystem.
  128. */
  129. void pstore_get_records(void)
  130. {
  131. struct pstore_info *psi = psinfo;
  132. ssize_t size;
  133. u64 id;
  134. enum pstore_type_id type;
  135. struct timespec time;
  136. int failed = 0, rc;
  137. if (!psi)
  138. return;
  139. mutex_lock(&psinfo->buf_mutex);
  140. rc = psi->open(psi);
  141. if (rc)
  142. goto out;
  143. while ((size = psi->read(&id, &type, &time)) > 0) {
  144. if (pstore_mkfile(type, psi->name, id, psi->buf, (size_t)size,
  145. time, psi->erase))
  146. failed++;
  147. }
  148. psi->close(psi);
  149. out:
  150. mutex_unlock(&psinfo->buf_mutex);
  151. if (failed)
  152. printk(KERN_WARNING "pstore: failed to load %d record(s) from '%s'\n",
  153. failed, psi->name);
  154. }
  155. /*
  156. * Call platform driver to write a record to the
  157. * persistent store.
  158. */
  159. int pstore_write(enum pstore_type_id type, char *buf, size_t size)
  160. {
  161. u64 id;
  162. if (!psinfo)
  163. return -ENODEV;
  164. if (size > psinfo->bufsize)
  165. return -EFBIG;
  166. mutex_lock(&psinfo->buf_mutex);
  167. memcpy(psinfo->buf, buf, size);
  168. id = psinfo->write(type, size);
  169. if (pstore_is_mounted())
  170. pstore_mkfile(PSTORE_TYPE_DMESG, psinfo->name, id, psinfo->buf,
  171. size, CURRENT_TIME, psinfo->erase);
  172. mutex_unlock(&psinfo->buf_mutex);
  173. return 0;
  174. }
  175. EXPORT_SYMBOL_GPL(pstore_write);