platform.c 4.7 KB

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