platform.c 5.0 KB

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