platform.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. /*
  2. * Persistent Storage - platform driver interface parts.
  3. *
  4. * Copyright (C) 2007-2008 Google, Inc.
  5. * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #define pr_fmt(fmt) "pstore: " fmt
  21. #include <linux/atomic.h>
  22. #include <linux/types.h>
  23. #include <linux/errno.h>
  24. #include <linux/init.h>
  25. #include <linux/kmsg_dump.h>
  26. #include <linux/console.h>
  27. #include <linux/module.h>
  28. #include <linux/pstore.h>
  29. #ifdef CONFIG_PSTORE_ZLIB_COMPRESS
  30. #include <linux/zlib.h>
  31. #endif
  32. #ifdef CONFIG_PSTORE_LZO_COMPRESS
  33. #include <linux/lzo.h>
  34. #endif
  35. #ifdef CONFIG_PSTORE_LZ4_COMPRESS
  36. #include <linux/lz4.h>
  37. #endif
  38. #include <linux/string.h>
  39. #include <linux/timer.h>
  40. #include <linux/slab.h>
  41. #include <linux/uaccess.h>
  42. #include <linux/hardirq.h>
  43. #include <linux/jiffies.h>
  44. #include <linux/workqueue.h>
  45. #include "internal.h"
  46. /*
  47. * We defer making "oops" entries appear in pstore - see
  48. * whether the system is actually still running well enough
  49. * to let someone see the entry
  50. */
  51. static int pstore_update_ms = -1;
  52. module_param_named(update_ms, pstore_update_ms, int, 0600);
  53. MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content "
  54. "(default is -1, which means runtime updates are disabled; "
  55. "enabling this option is not safe, it may lead to further "
  56. "corruption on Oopses)");
  57. static int pstore_new_entry;
  58. static void pstore_timefunc(unsigned long);
  59. static DEFINE_TIMER(pstore_timer, pstore_timefunc, 0, 0);
  60. static void pstore_dowork(struct work_struct *);
  61. static DECLARE_WORK(pstore_work, pstore_dowork);
  62. /*
  63. * pstore_lock just protects "psinfo" during
  64. * calls to pstore_register()
  65. */
  66. static DEFINE_SPINLOCK(pstore_lock);
  67. struct pstore_info *psinfo;
  68. static char *backend;
  69. /* Compression parameters */
  70. #ifdef CONFIG_PSTORE_ZLIB_COMPRESS
  71. #define COMPR_LEVEL 6
  72. #define WINDOW_BITS 12
  73. #define MEM_LEVEL 4
  74. static struct z_stream_s stream;
  75. #else
  76. static unsigned char *workspace;
  77. #endif
  78. struct pstore_zbackend {
  79. int (*compress)(const void *in, void *out, size_t inlen, size_t outlen);
  80. int (*decompress)(void *in, void *out, size_t inlen, size_t outlen);
  81. void (*allocate)(void);
  82. void (*free)(void);
  83. const char *name;
  84. };
  85. static char *big_oops_buf;
  86. static size_t big_oops_buf_sz;
  87. /* How much of the console log to snapshot */
  88. static unsigned long kmsg_bytes = 10240;
  89. void pstore_set_kmsg_bytes(int bytes)
  90. {
  91. kmsg_bytes = bytes;
  92. }
  93. /* Tag each group of saved records with a sequence number */
  94. static int oopscount;
  95. static const char *get_reason_str(enum kmsg_dump_reason reason)
  96. {
  97. switch (reason) {
  98. case KMSG_DUMP_PANIC:
  99. return "Panic";
  100. case KMSG_DUMP_OOPS:
  101. return "Oops";
  102. case KMSG_DUMP_EMERG:
  103. return "Emergency";
  104. case KMSG_DUMP_RESTART:
  105. return "Restart";
  106. case KMSG_DUMP_HALT:
  107. return "Halt";
  108. case KMSG_DUMP_POWEROFF:
  109. return "Poweroff";
  110. default:
  111. return "Unknown";
  112. }
  113. }
  114. bool pstore_cannot_block_path(enum kmsg_dump_reason reason)
  115. {
  116. /*
  117. * In case of NMI path, pstore shouldn't be blocked
  118. * regardless of reason.
  119. */
  120. if (in_nmi())
  121. return true;
  122. switch (reason) {
  123. /* In panic case, other cpus are stopped by smp_send_stop(). */
  124. case KMSG_DUMP_PANIC:
  125. /* Emergency restart shouldn't be blocked by spin lock. */
  126. case KMSG_DUMP_EMERG:
  127. return true;
  128. default:
  129. return false;
  130. }
  131. }
  132. EXPORT_SYMBOL_GPL(pstore_cannot_block_path);
  133. #ifdef CONFIG_PSTORE_ZLIB_COMPRESS
  134. /* Derived from logfs_compress() */
  135. static int compress_zlib(const void *in, void *out, size_t inlen, size_t outlen)
  136. {
  137. int err, ret;
  138. ret = -EIO;
  139. err = zlib_deflateInit2(&stream, COMPR_LEVEL, Z_DEFLATED, WINDOW_BITS,
  140. MEM_LEVEL, Z_DEFAULT_STRATEGY);
  141. if (err != Z_OK)
  142. goto error;
  143. stream.next_in = in;
  144. stream.avail_in = inlen;
  145. stream.total_in = 0;
  146. stream.next_out = out;
  147. stream.avail_out = outlen;
  148. stream.total_out = 0;
  149. err = zlib_deflate(&stream, Z_FINISH);
  150. if (err != Z_STREAM_END)
  151. goto error;
  152. err = zlib_deflateEnd(&stream);
  153. if (err != Z_OK)
  154. goto error;
  155. if (stream.total_out >= stream.total_in)
  156. goto error;
  157. ret = stream.total_out;
  158. error:
  159. return ret;
  160. }
  161. /* Derived from logfs_uncompress */
  162. static int decompress_zlib(void *in, void *out, size_t inlen, size_t outlen)
  163. {
  164. int err, ret;
  165. ret = -EIO;
  166. err = zlib_inflateInit2(&stream, WINDOW_BITS);
  167. if (err != Z_OK)
  168. goto error;
  169. stream.next_in = in;
  170. stream.avail_in = inlen;
  171. stream.total_in = 0;
  172. stream.next_out = out;
  173. stream.avail_out = outlen;
  174. stream.total_out = 0;
  175. err = zlib_inflate(&stream, Z_FINISH);
  176. if (err != Z_STREAM_END)
  177. goto error;
  178. err = zlib_inflateEnd(&stream);
  179. if (err != Z_OK)
  180. goto error;
  181. ret = stream.total_out;
  182. error:
  183. return ret;
  184. }
  185. static void allocate_zlib(void)
  186. {
  187. size_t size;
  188. size_t cmpr;
  189. switch (psinfo->bufsize) {
  190. /* buffer range for efivars */
  191. case 1000 ... 2000:
  192. cmpr = 56;
  193. break;
  194. case 2001 ... 3000:
  195. cmpr = 54;
  196. break;
  197. case 3001 ... 3999:
  198. cmpr = 52;
  199. break;
  200. /* buffer range for nvram, erst */
  201. case 4000 ... 10000:
  202. cmpr = 45;
  203. break;
  204. default:
  205. cmpr = 60;
  206. break;
  207. }
  208. big_oops_buf_sz = (psinfo->bufsize * 100) / cmpr;
  209. big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
  210. if (big_oops_buf) {
  211. size = max(zlib_deflate_workspacesize(WINDOW_BITS, MEM_LEVEL),
  212. zlib_inflate_workspacesize());
  213. stream.workspace = kmalloc(size, GFP_KERNEL);
  214. if (!stream.workspace) {
  215. pr_err("No memory for compression workspace; skipping compression\n");
  216. kfree(big_oops_buf);
  217. big_oops_buf = NULL;
  218. }
  219. } else {
  220. pr_err("No memory for uncompressed data; skipping compression\n");
  221. stream.workspace = NULL;
  222. }
  223. }
  224. static void free_zlib(void)
  225. {
  226. kfree(stream.workspace);
  227. stream.workspace = NULL;
  228. kfree(big_oops_buf);
  229. big_oops_buf = NULL;
  230. big_oops_buf_sz = 0;
  231. }
  232. static struct pstore_zbackend backend_zlib = {
  233. .compress = compress_zlib,
  234. .decompress = decompress_zlib,
  235. .allocate = allocate_zlib,
  236. .free = free_zlib,
  237. .name = "zlib",
  238. };
  239. #endif
  240. #ifdef CONFIG_PSTORE_LZO_COMPRESS
  241. static int compress_lzo(const void *in, void *out, size_t inlen, size_t outlen)
  242. {
  243. int ret;
  244. ret = lzo1x_1_compress(in, inlen, out, &outlen, workspace);
  245. if (ret != LZO_E_OK) {
  246. pr_err("lzo_compress error, ret = %d!\n", ret);
  247. return -EIO;
  248. }
  249. return outlen;
  250. }
  251. static int decompress_lzo(void *in, void *out, size_t inlen, size_t outlen)
  252. {
  253. int ret;
  254. ret = lzo1x_decompress_safe(in, inlen, out, &outlen);
  255. if (ret != LZO_E_OK) {
  256. pr_err("lzo_decompress error, ret = %d!\n", ret);
  257. return -EIO;
  258. }
  259. return outlen;
  260. }
  261. static void allocate_lzo(void)
  262. {
  263. big_oops_buf_sz = lzo1x_worst_compress(psinfo->bufsize);
  264. big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
  265. if (big_oops_buf) {
  266. workspace = kmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
  267. if (!workspace) {
  268. pr_err("No memory for compression workspace; skipping compression\n");
  269. kfree(big_oops_buf);
  270. big_oops_buf = NULL;
  271. }
  272. } else {
  273. pr_err("No memory for uncompressed data; skipping compression\n");
  274. workspace = NULL;
  275. }
  276. }
  277. static void free_lzo(void)
  278. {
  279. kfree(workspace);
  280. kfree(big_oops_buf);
  281. big_oops_buf = NULL;
  282. big_oops_buf_sz = 0;
  283. }
  284. static struct pstore_zbackend backend_lzo = {
  285. .compress = compress_lzo,
  286. .decompress = decompress_lzo,
  287. .allocate = allocate_lzo,
  288. .free = free_lzo,
  289. .name = "lzo",
  290. };
  291. #endif
  292. #ifdef CONFIG_PSTORE_LZ4_COMPRESS
  293. static int compress_lz4(const void *in, void *out, size_t inlen, size_t outlen)
  294. {
  295. int ret;
  296. ret = lz4_compress(in, inlen, out, &outlen, workspace);
  297. if (ret) {
  298. pr_err("lz4_compress error, ret = %d!\n", ret);
  299. return -EIO;
  300. }
  301. return outlen;
  302. }
  303. static int decompress_lz4(void *in, void *out, size_t inlen, size_t outlen)
  304. {
  305. int ret;
  306. ret = lz4_decompress_unknownoutputsize(in, inlen, out, &outlen);
  307. if (ret) {
  308. pr_err("lz4_decompress error, ret = %d!\n", ret);
  309. return -EIO;
  310. }
  311. return outlen;
  312. }
  313. static void allocate_lz4(void)
  314. {
  315. big_oops_buf_sz = lz4_compressbound(psinfo->bufsize);
  316. big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
  317. if (big_oops_buf) {
  318. workspace = kmalloc(LZ4_MEM_COMPRESS, GFP_KERNEL);
  319. if (!workspace) {
  320. pr_err("No memory for compression workspace; skipping compression\n");
  321. kfree(big_oops_buf);
  322. big_oops_buf = NULL;
  323. }
  324. } else {
  325. pr_err("No memory for uncompressed data; skipping compression\n");
  326. workspace = NULL;
  327. }
  328. }
  329. static void free_lz4(void)
  330. {
  331. kfree(workspace);
  332. kfree(big_oops_buf);
  333. big_oops_buf = NULL;
  334. big_oops_buf_sz = 0;
  335. }
  336. static struct pstore_zbackend backend_lz4 = {
  337. .compress = compress_lz4,
  338. .decompress = decompress_lz4,
  339. .allocate = allocate_lz4,
  340. .free = free_lz4,
  341. .name = "lz4",
  342. };
  343. #endif
  344. static struct pstore_zbackend *zbackend =
  345. #if defined(CONFIG_PSTORE_ZLIB_COMPRESS)
  346. &backend_zlib;
  347. #elif defined(CONFIG_PSTORE_LZO_COMPRESS)
  348. &backend_lzo;
  349. #elif defined(CONFIG_PSTORE_LZ4_COMPRESS)
  350. &backend_lz4;
  351. #else
  352. NULL;
  353. #endif
  354. static int pstore_compress(const void *in, void *out,
  355. size_t inlen, size_t outlen)
  356. {
  357. if (zbackend)
  358. return zbackend->compress(in, out, inlen, outlen);
  359. else
  360. return -EIO;
  361. }
  362. static int pstore_decompress(void *in, void *out, size_t inlen, size_t outlen)
  363. {
  364. if (zbackend)
  365. return zbackend->decompress(in, out, inlen, outlen);
  366. else
  367. return -EIO;
  368. }
  369. static void allocate_buf_for_compression(void)
  370. {
  371. if (zbackend) {
  372. pr_info("using %s compression\n", zbackend->name);
  373. zbackend->allocate();
  374. } else {
  375. pr_err("allocate compression buffer error!\n");
  376. }
  377. }
  378. static void free_buf_for_compression(void)
  379. {
  380. if (zbackend)
  381. zbackend->free();
  382. else
  383. pr_err("free compression buffer error!\n");
  384. }
  385. /*
  386. * Called when compression fails, since the printk buffer
  387. * would be fetched for compression calling it again when
  388. * compression fails would have moved the iterator of
  389. * printk buffer which results in fetching old contents.
  390. * Copy the recent messages from big_oops_buf to psinfo->buf
  391. */
  392. static size_t copy_kmsg_to_buffer(int hsize, size_t len)
  393. {
  394. size_t total_len;
  395. size_t diff;
  396. total_len = hsize + len;
  397. if (total_len > psinfo->bufsize) {
  398. diff = total_len - psinfo->bufsize + hsize;
  399. memcpy(psinfo->buf, big_oops_buf, hsize);
  400. memcpy(psinfo->buf + hsize, big_oops_buf + diff,
  401. psinfo->bufsize - hsize);
  402. total_len = psinfo->bufsize;
  403. } else
  404. memcpy(psinfo->buf, big_oops_buf, total_len);
  405. return total_len;
  406. }
  407. /*
  408. * callback from kmsg_dump. (s2,l2) has the most recently
  409. * written bytes, older bytes are in (s1,l1). Save as much
  410. * as we can from the end of the buffer.
  411. */
  412. static void pstore_dump(struct kmsg_dumper *dumper,
  413. enum kmsg_dump_reason reason)
  414. {
  415. unsigned long total = 0;
  416. const char *why;
  417. u64 id;
  418. unsigned int part = 1;
  419. unsigned long flags = 0;
  420. int is_locked;
  421. int ret;
  422. why = get_reason_str(reason);
  423. if (pstore_cannot_block_path(reason)) {
  424. is_locked = spin_trylock_irqsave(&psinfo->buf_lock, flags);
  425. if (!is_locked) {
  426. pr_err("pstore dump routine blocked in %s path, may corrupt error record\n"
  427. , in_nmi() ? "NMI" : why);
  428. }
  429. } else {
  430. spin_lock_irqsave(&psinfo->buf_lock, flags);
  431. is_locked = 1;
  432. }
  433. oopscount++;
  434. while (total < kmsg_bytes) {
  435. char *dst;
  436. unsigned long size;
  437. int hsize;
  438. int zipped_len = -1;
  439. size_t len;
  440. bool compressed = false;
  441. size_t total_len;
  442. if (big_oops_buf && is_locked) {
  443. dst = big_oops_buf;
  444. size = big_oops_buf_sz;
  445. } else {
  446. dst = psinfo->buf;
  447. size = psinfo->bufsize;
  448. }
  449. hsize = sprintf(dst, "%s#%d Part%u\n", why, oopscount, part);
  450. size -= hsize;
  451. if (!kmsg_dump_get_buffer(dumper, true, dst + hsize,
  452. size, &len))
  453. break;
  454. if (big_oops_buf && is_locked) {
  455. zipped_len = pstore_compress(dst, psinfo->buf,
  456. hsize + len, psinfo->bufsize);
  457. if (zipped_len > 0) {
  458. compressed = true;
  459. total_len = zipped_len;
  460. } else {
  461. total_len = copy_kmsg_to_buffer(hsize, len);
  462. }
  463. } else {
  464. total_len = hsize + len;
  465. }
  466. ret = psinfo->write(PSTORE_TYPE_DMESG, reason, &id, part,
  467. oopscount, compressed, total_len, psinfo);
  468. if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted())
  469. pstore_new_entry = 1;
  470. total += total_len;
  471. part++;
  472. }
  473. if (is_locked)
  474. spin_unlock_irqrestore(&psinfo->buf_lock, flags);
  475. }
  476. static struct kmsg_dumper pstore_dumper = {
  477. .dump = pstore_dump,
  478. };
  479. /*
  480. * Register with kmsg_dump to save last part of console log on panic.
  481. */
  482. static void pstore_register_kmsg(void)
  483. {
  484. kmsg_dump_register(&pstore_dumper);
  485. }
  486. static void pstore_unregister_kmsg(void)
  487. {
  488. kmsg_dump_unregister(&pstore_dumper);
  489. }
  490. #ifdef CONFIG_PSTORE_CONSOLE
  491. static void pstore_console_write(struct console *con, const char *s, unsigned c)
  492. {
  493. const char *e = s + c;
  494. while (s < e) {
  495. unsigned long flags;
  496. u64 id;
  497. if (c > psinfo->bufsize)
  498. c = psinfo->bufsize;
  499. if (oops_in_progress) {
  500. if (!spin_trylock_irqsave(&psinfo->buf_lock, flags))
  501. break;
  502. } else {
  503. spin_lock_irqsave(&psinfo->buf_lock, flags);
  504. }
  505. memcpy(psinfo->buf, s, c);
  506. psinfo->write(PSTORE_TYPE_CONSOLE, 0, &id, 0, 0, 0, c, psinfo);
  507. spin_unlock_irqrestore(&psinfo->buf_lock, flags);
  508. s += c;
  509. c = e - s;
  510. }
  511. }
  512. static struct console pstore_console = {
  513. .name = "pstore",
  514. .write = pstore_console_write,
  515. .flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME,
  516. .index = -1,
  517. };
  518. static void pstore_register_console(void)
  519. {
  520. register_console(&pstore_console);
  521. }
  522. static void pstore_unregister_console(void)
  523. {
  524. unregister_console(&pstore_console);
  525. }
  526. #else
  527. static void pstore_register_console(void) {}
  528. static void pstore_unregister_console(void) {}
  529. #endif
  530. static int pstore_write_compat(enum pstore_type_id type,
  531. enum kmsg_dump_reason reason,
  532. u64 *id, unsigned int part, int count,
  533. bool compressed, size_t size,
  534. struct pstore_info *psi)
  535. {
  536. return psi->write_buf(type, reason, id, part, psinfo->buf, compressed,
  537. size, psi);
  538. }
  539. static int pstore_write_buf_user_compat(enum pstore_type_id type,
  540. enum kmsg_dump_reason reason,
  541. u64 *id, unsigned int part,
  542. const char __user *buf,
  543. bool compressed, size_t size,
  544. struct pstore_info *psi)
  545. {
  546. unsigned long flags = 0;
  547. size_t i, bufsize = size;
  548. long ret = 0;
  549. if (unlikely(!access_ok(VERIFY_READ, buf, size)))
  550. return -EFAULT;
  551. if (bufsize > psinfo->bufsize)
  552. bufsize = psinfo->bufsize;
  553. spin_lock_irqsave(&psinfo->buf_lock, flags);
  554. for (i = 0; i < size; ) {
  555. size_t c = min(size - i, bufsize);
  556. ret = __copy_from_user(psinfo->buf, buf + i, c);
  557. if (unlikely(ret != 0)) {
  558. ret = -EFAULT;
  559. break;
  560. }
  561. ret = psi->write_buf(type, reason, id, part, psinfo->buf,
  562. compressed, c, psi);
  563. if (unlikely(ret < 0))
  564. break;
  565. i += c;
  566. }
  567. spin_unlock_irqrestore(&psinfo->buf_lock, flags);
  568. return unlikely(ret < 0) ? ret : size;
  569. }
  570. /*
  571. * platform specific persistent storage driver registers with
  572. * us here. If pstore is already mounted, call the platform
  573. * read function right away to populate the file system. If not
  574. * then the pstore mount code will call us later to fill out
  575. * the file system.
  576. */
  577. int pstore_register(struct pstore_info *psi)
  578. {
  579. struct module *owner = psi->owner;
  580. if (backend && strcmp(backend, psi->name))
  581. return -EPERM;
  582. spin_lock(&pstore_lock);
  583. if (psinfo) {
  584. spin_unlock(&pstore_lock);
  585. return -EBUSY;
  586. }
  587. if (!psi->write)
  588. psi->write = pstore_write_compat;
  589. if (!psi->write_buf_user)
  590. psi->write_buf_user = pstore_write_buf_user_compat;
  591. psinfo = psi;
  592. mutex_init(&psinfo->read_mutex);
  593. spin_unlock(&pstore_lock);
  594. if (owner && !try_module_get(owner)) {
  595. psinfo = NULL;
  596. return -EINVAL;
  597. }
  598. allocate_buf_for_compression();
  599. if (pstore_is_mounted())
  600. pstore_get_records(0);
  601. if (psi->flags & PSTORE_FLAGS_DMESG)
  602. pstore_register_kmsg();
  603. if (psi->flags & PSTORE_FLAGS_CONSOLE)
  604. pstore_register_console();
  605. if (psi->flags & PSTORE_FLAGS_FTRACE)
  606. pstore_register_ftrace();
  607. if (psi->flags & PSTORE_FLAGS_PMSG)
  608. pstore_register_pmsg();
  609. /* Start watching for new records, if desired. */
  610. if (pstore_update_ms >= 0) {
  611. pstore_timer.expires = jiffies +
  612. msecs_to_jiffies(pstore_update_ms);
  613. add_timer(&pstore_timer);
  614. }
  615. /*
  616. * Update the module parameter backend, so it is visible
  617. * through /sys/module/pstore/parameters/backend
  618. */
  619. backend = psi->name;
  620. module_put(owner);
  621. pr_info("Registered %s as persistent store backend\n", psi->name);
  622. return 0;
  623. }
  624. EXPORT_SYMBOL_GPL(pstore_register);
  625. void pstore_unregister(struct pstore_info *psi)
  626. {
  627. /* Stop timer and make sure all work has finished. */
  628. pstore_update_ms = -1;
  629. del_timer_sync(&pstore_timer);
  630. flush_work(&pstore_work);
  631. if (psi->flags & PSTORE_FLAGS_PMSG)
  632. pstore_unregister_pmsg();
  633. if (psi->flags & PSTORE_FLAGS_FTRACE)
  634. pstore_unregister_ftrace();
  635. if (psi->flags & PSTORE_FLAGS_CONSOLE)
  636. pstore_unregister_console();
  637. if (psi->flags & PSTORE_FLAGS_DMESG)
  638. pstore_unregister_kmsg();
  639. free_buf_for_compression();
  640. psinfo = NULL;
  641. backend = NULL;
  642. }
  643. EXPORT_SYMBOL_GPL(pstore_unregister);
  644. /*
  645. * Read all the records from the persistent store. Create
  646. * files in our filesystem. Don't warn about -EEXIST errors
  647. * when we are re-scanning the backing store looking to add new
  648. * error records.
  649. */
  650. void pstore_get_records(int quiet)
  651. {
  652. struct pstore_info *psi = psinfo;
  653. char *buf = NULL;
  654. ssize_t size;
  655. u64 id;
  656. int count;
  657. enum pstore_type_id type;
  658. struct timespec time;
  659. int failed = 0, rc;
  660. bool compressed;
  661. int unzipped_len = -1;
  662. ssize_t ecc_notice_size = 0;
  663. if (!psi)
  664. return;
  665. mutex_lock(&psi->read_mutex);
  666. if (psi->open && psi->open(psi))
  667. goto out;
  668. while ((size = psi->read(&id, &type, &count, &time, &buf, &compressed,
  669. &ecc_notice_size, psi)) > 0) {
  670. if (compressed && (type == PSTORE_TYPE_DMESG)) {
  671. if (big_oops_buf)
  672. unzipped_len = pstore_decompress(buf,
  673. big_oops_buf, size,
  674. big_oops_buf_sz);
  675. if (unzipped_len > 0) {
  676. if (ecc_notice_size)
  677. memcpy(big_oops_buf + unzipped_len,
  678. buf + size, ecc_notice_size);
  679. kfree(buf);
  680. buf = big_oops_buf;
  681. size = unzipped_len;
  682. compressed = false;
  683. } else {
  684. pr_err("decompression failed;returned %d\n",
  685. unzipped_len);
  686. compressed = true;
  687. }
  688. }
  689. rc = pstore_mkfile(type, psi->name, id, count, buf,
  690. compressed, size + ecc_notice_size,
  691. time, psi);
  692. if (unzipped_len < 0) {
  693. /* Free buffer other than big oops */
  694. kfree(buf);
  695. buf = NULL;
  696. } else
  697. unzipped_len = -1;
  698. if (rc && (rc != -EEXIST || !quiet))
  699. failed++;
  700. }
  701. if (psi->close)
  702. psi->close(psi);
  703. out:
  704. mutex_unlock(&psi->read_mutex);
  705. if (failed)
  706. pr_warn("failed to load %d record(s) from '%s'\n",
  707. failed, psi->name);
  708. }
  709. static void pstore_dowork(struct work_struct *work)
  710. {
  711. pstore_get_records(1);
  712. }
  713. static void pstore_timefunc(unsigned long dummy)
  714. {
  715. if (pstore_new_entry) {
  716. pstore_new_entry = 0;
  717. schedule_work(&pstore_work);
  718. }
  719. if (pstore_update_ms >= 0)
  720. mod_timer(&pstore_timer,
  721. jiffies + msecs_to_jiffies(pstore_update_ms));
  722. }
  723. module_param(backend, charp, 0444);
  724. MODULE_PARM_DESC(backend, "Pstore backend to use");