platform.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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. #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
  30. #include <linux/lzo.h>
  31. #endif
  32. #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
  33. #include <linux/lz4.h>
  34. #endif
  35. #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
  36. #include <linux/zstd.h>
  37. #endif
  38. #include <linux/crypto.h>
  39. #include <linux/string.h>
  40. #include <linux/timer.h>
  41. #include <linux/slab.h>
  42. #include <linux/uaccess.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(struct timer_list *);
  59. static DEFINE_TIMER(pstore_timer, pstore_timefunc);
  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. static char *compress =
  70. #ifdef CONFIG_PSTORE_COMPRESS_DEFAULT
  71. CONFIG_PSTORE_COMPRESS_DEFAULT;
  72. #else
  73. NULL;
  74. #endif
  75. /* Compression parameters */
  76. static struct crypto_comp *tfm;
  77. struct pstore_zbackend {
  78. int (*zbufsize)(size_t size);
  79. const char *name;
  80. };
  81. static char *big_oops_buf;
  82. static size_t big_oops_buf_sz;
  83. /* How much of the console log to snapshot */
  84. unsigned long kmsg_bytes = PSTORE_DEFAULT_KMSG_BYTES;
  85. void pstore_set_kmsg_bytes(int bytes)
  86. {
  87. kmsg_bytes = bytes;
  88. }
  89. /* Tag each group of saved records with a sequence number */
  90. static int oopscount;
  91. static const char *get_reason_str(enum kmsg_dump_reason reason)
  92. {
  93. switch (reason) {
  94. case KMSG_DUMP_PANIC:
  95. return "Panic";
  96. case KMSG_DUMP_OOPS:
  97. return "Oops";
  98. case KMSG_DUMP_EMERG:
  99. return "Emergency";
  100. case KMSG_DUMP_RESTART:
  101. return "Restart";
  102. case KMSG_DUMP_HALT:
  103. return "Halt";
  104. case KMSG_DUMP_POWEROFF:
  105. return "Poweroff";
  106. default:
  107. return "Unknown";
  108. }
  109. }
  110. bool pstore_cannot_block_path(enum kmsg_dump_reason reason)
  111. {
  112. /*
  113. * In case of NMI path, pstore shouldn't be blocked
  114. * regardless of reason.
  115. */
  116. if (in_nmi())
  117. return true;
  118. switch (reason) {
  119. /* In panic case, other cpus are stopped by smp_send_stop(). */
  120. case KMSG_DUMP_PANIC:
  121. /* Emergency restart shouldn't be blocked by spin lock. */
  122. case KMSG_DUMP_EMERG:
  123. return true;
  124. default:
  125. return false;
  126. }
  127. }
  128. EXPORT_SYMBOL_GPL(pstore_cannot_block_path);
  129. #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
  130. static int zbufsize_deflate(size_t size)
  131. {
  132. size_t cmpr;
  133. switch (size) {
  134. /* buffer range for efivars */
  135. case 1000 ... 2000:
  136. cmpr = 56;
  137. break;
  138. case 2001 ... 3000:
  139. cmpr = 54;
  140. break;
  141. case 3001 ... 3999:
  142. cmpr = 52;
  143. break;
  144. /* buffer range for nvram, erst */
  145. case 4000 ... 10000:
  146. cmpr = 45;
  147. break;
  148. default:
  149. cmpr = 60;
  150. break;
  151. }
  152. return (size * 100) / cmpr;
  153. }
  154. #endif
  155. #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
  156. static int zbufsize_lzo(size_t size)
  157. {
  158. return lzo1x_worst_compress(size);
  159. }
  160. #endif
  161. #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
  162. static int zbufsize_lz4(size_t size)
  163. {
  164. return LZ4_compressBound(size);
  165. }
  166. #endif
  167. #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
  168. static int zbufsize_842(size_t size)
  169. {
  170. return size;
  171. }
  172. #endif
  173. #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
  174. static int zbufsize_zstd(size_t size)
  175. {
  176. return ZSTD_compressBound(size);
  177. }
  178. #endif
  179. static const struct pstore_zbackend *zbackend __ro_after_init;
  180. static const struct pstore_zbackend zbackends[] = {
  181. #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
  182. {
  183. .zbufsize = zbufsize_deflate,
  184. .name = "deflate",
  185. },
  186. #endif
  187. #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
  188. {
  189. .zbufsize = zbufsize_lzo,
  190. .name = "lzo",
  191. },
  192. #endif
  193. #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS)
  194. {
  195. .zbufsize = zbufsize_lz4,
  196. .name = "lz4",
  197. },
  198. #endif
  199. #if IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
  200. {
  201. .zbufsize = zbufsize_lz4,
  202. .name = "lz4hc",
  203. },
  204. #endif
  205. #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
  206. {
  207. .zbufsize = zbufsize_842,
  208. .name = "842",
  209. },
  210. #endif
  211. #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
  212. {
  213. .zbufsize = zbufsize_zstd,
  214. .name = "zstd",
  215. },
  216. #endif
  217. { }
  218. };
  219. static int pstore_compress(const void *in, void *out,
  220. unsigned int inlen, unsigned int outlen)
  221. {
  222. int ret;
  223. ret = crypto_comp_compress(tfm, in, inlen, out, &outlen);
  224. if (ret) {
  225. pr_err("crypto_comp_compress failed, ret = %d!\n", ret);
  226. return ret;
  227. }
  228. return outlen;
  229. }
  230. static int pstore_decompress(void *in, void *out,
  231. unsigned int inlen, unsigned int outlen)
  232. {
  233. int ret;
  234. ret = crypto_comp_decompress(tfm, in, inlen, out, &outlen);
  235. if (ret) {
  236. pr_err("crypto_comp_decompress failed, ret = %d!\n", ret);
  237. return ret;
  238. }
  239. return outlen;
  240. }
  241. static void allocate_buf_for_compression(void)
  242. {
  243. struct crypto_comp *ctx;
  244. int size;
  245. char *buf;
  246. /* Skip if not built-in or compression backend not selected yet. */
  247. if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !zbackend)
  248. return;
  249. /* Skip if no pstore backend yet or compression init already done. */
  250. if (!psinfo || tfm)
  251. return;
  252. if (!crypto_has_comp(zbackend->name, 0, 0)) {
  253. pr_err("Unknown compression: %s\n", zbackend->name);
  254. return;
  255. }
  256. size = zbackend->zbufsize(psinfo->bufsize);
  257. if (size <= 0) {
  258. pr_err("Invalid compression size for %s: %d\n",
  259. zbackend->name, size);
  260. return;
  261. }
  262. buf = kmalloc(size, GFP_KERNEL);
  263. if (!buf) {
  264. pr_err("Failed %d byte compression buffer allocation for: %s\n",
  265. size, zbackend->name);
  266. return;
  267. }
  268. ctx = crypto_alloc_comp(zbackend->name, 0, 0);
  269. if (IS_ERR_OR_NULL(ctx)) {
  270. kfree(buf);
  271. pr_err("crypto_alloc_comp('%s') failed: %ld\n", zbackend->name,
  272. PTR_ERR(ctx));
  273. return;
  274. }
  275. /* A non-NULL big_oops_buf indicates compression is available. */
  276. tfm = ctx;
  277. big_oops_buf_sz = size;
  278. big_oops_buf = buf;
  279. pr_info("Using compression: %s\n", zbackend->name);
  280. }
  281. static void free_buf_for_compression(void)
  282. {
  283. if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && tfm)
  284. crypto_free_comp(tfm);
  285. kfree(big_oops_buf);
  286. big_oops_buf = NULL;
  287. big_oops_buf_sz = 0;
  288. }
  289. /*
  290. * Called when compression fails, since the printk buffer
  291. * would be fetched for compression calling it again when
  292. * compression fails would have moved the iterator of
  293. * printk buffer which results in fetching old contents.
  294. * Copy the recent messages from big_oops_buf to psinfo->buf
  295. */
  296. static size_t copy_kmsg_to_buffer(int hsize, size_t len)
  297. {
  298. size_t total_len;
  299. size_t diff;
  300. total_len = hsize + len;
  301. if (total_len > psinfo->bufsize) {
  302. diff = total_len - psinfo->bufsize + hsize;
  303. memcpy(psinfo->buf, big_oops_buf, hsize);
  304. memcpy(psinfo->buf + hsize, big_oops_buf + diff,
  305. psinfo->bufsize - hsize);
  306. total_len = psinfo->bufsize;
  307. } else
  308. memcpy(psinfo->buf, big_oops_buf, total_len);
  309. return total_len;
  310. }
  311. void pstore_record_init(struct pstore_record *record,
  312. struct pstore_info *psinfo)
  313. {
  314. memset(record, 0, sizeof(*record));
  315. record->psi = psinfo;
  316. /* Report zeroed timestamp if called before timekeeping has resumed. */
  317. record->time = ns_to_timespec64(ktime_get_real_fast_ns());
  318. }
  319. /*
  320. * callback from kmsg_dump. (s2,l2) has the most recently
  321. * written bytes, older bytes are in (s1,l1). Save as much
  322. * as we can from the end of the buffer.
  323. */
  324. static void pstore_dump(struct kmsg_dumper *dumper,
  325. enum kmsg_dump_reason reason)
  326. {
  327. unsigned long total = 0;
  328. const char *why;
  329. unsigned int part = 1;
  330. unsigned long flags = 0;
  331. int is_locked;
  332. int ret;
  333. why = get_reason_str(reason);
  334. if (pstore_cannot_block_path(reason)) {
  335. is_locked = spin_trylock_irqsave(&psinfo->buf_lock, flags);
  336. if (!is_locked) {
  337. pr_err("pstore dump routine blocked in %s path, may corrupt error record\n"
  338. , in_nmi() ? "NMI" : why);
  339. return;
  340. }
  341. } else {
  342. spin_lock_irqsave(&psinfo->buf_lock, flags);
  343. is_locked = 1;
  344. }
  345. oopscount++;
  346. while (total < kmsg_bytes) {
  347. char *dst;
  348. size_t dst_size;
  349. int header_size;
  350. int zipped_len = -1;
  351. size_t dump_size;
  352. struct pstore_record record;
  353. pstore_record_init(&record, psinfo);
  354. record.type = PSTORE_TYPE_DMESG;
  355. record.count = oopscount;
  356. record.reason = reason;
  357. record.part = part;
  358. record.buf = psinfo->buf;
  359. if (big_oops_buf && is_locked) {
  360. dst = big_oops_buf;
  361. dst_size = big_oops_buf_sz;
  362. } else {
  363. dst = psinfo->buf;
  364. dst_size = psinfo->bufsize;
  365. }
  366. /* Write dump header. */
  367. header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why,
  368. oopscount, part);
  369. dst_size -= header_size;
  370. /* Write dump contents. */
  371. if (!kmsg_dump_get_buffer(dumper, true, dst + header_size,
  372. dst_size, &dump_size))
  373. break;
  374. if (big_oops_buf && is_locked) {
  375. zipped_len = pstore_compress(dst, psinfo->buf,
  376. header_size + dump_size,
  377. psinfo->bufsize);
  378. if (zipped_len > 0) {
  379. record.compressed = true;
  380. record.size = zipped_len;
  381. } else {
  382. record.size = copy_kmsg_to_buffer(header_size,
  383. dump_size);
  384. }
  385. } else {
  386. record.size = header_size + dump_size;
  387. }
  388. ret = psinfo->write(&record);
  389. if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted())
  390. pstore_new_entry = 1;
  391. total += record.size;
  392. part++;
  393. }
  394. if (is_locked)
  395. spin_unlock_irqrestore(&psinfo->buf_lock, flags);
  396. }
  397. static struct kmsg_dumper pstore_dumper = {
  398. .dump = pstore_dump,
  399. };
  400. /*
  401. * Register with kmsg_dump to save last part of console log on panic.
  402. */
  403. static void pstore_register_kmsg(void)
  404. {
  405. kmsg_dump_register(&pstore_dumper);
  406. }
  407. static void pstore_unregister_kmsg(void)
  408. {
  409. kmsg_dump_unregister(&pstore_dumper);
  410. }
  411. #ifdef CONFIG_PSTORE_CONSOLE
  412. static void pstore_console_write(struct console *con, const char *s, unsigned c)
  413. {
  414. const char *e = s + c;
  415. while (s < e) {
  416. struct pstore_record record;
  417. unsigned long flags;
  418. pstore_record_init(&record, psinfo);
  419. record.type = PSTORE_TYPE_CONSOLE;
  420. if (c > psinfo->bufsize)
  421. c = psinfo->bufsize;
  422. if (oops_in_progress) {
  423. if (!spin_trylock_irqsave(&psinfo->buf_lock, flags))
  424. break;
  425. } else {
  426. spin_lock_irqsave(&psinfo->buf_lock, flags);
  427. }
  428. record.buf = (char *)s;
  429. record.size = c;
  430. psinfo->write(&record);
  431. spin_unlock_irqrestore(&psinfo->buf_lock, flags);
  432. s += c;
  433. c = e - s;
  434. }
  435. }
  436. static struct console pstore_console = {
  437. .name = "pstore",
  438. .write = pstore_console_write,
  439. .flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME,
  440. .index = -1,
  441. };
  442. static void pstore_register_console(void)
  443. {
  444. register_console(&pstore_console);
  445. }
  446. static void pstore_unregister_console(void)
  447. {
  448. unregister_console(&pstore_console);
  449. }
  450. #else
  451. static void pstore_register_console(void) {}
  452. static void pstore_unregister_console(void) {}
  453. #endif
  454. static int pstore_write_user_compat(struct pstore_record *record,
  455. const char __user *buf)
  456. {
  457. int ret = 0;
  458. if (record->buf)
  459. return -EINVAL;
  460. record->buf = memdup_user(buf, record->size);
  461. if (IS_ERR(record->buf)) {
  462. ret = PTR_ERR(record->buf);
  463. goto out;
  464. }
  465. ret = record->psi->write(record);
  466. kfree(record->buf);
  467. out:
  468. record->buf = NULL;
  469. return unlikely(ret < 0) ? ret : record->size;
  470. }
  471. /*
  472. * platform specific persistent storage driver registers with
  473. * us here. If pstore is already mounted, call the platform
  474. * read function right away to populate the file system. If not
  475. * then the pstore mount code will call us later to fill out
  476. * the file system.
  477. */
  478. int pstore_register(struct pstore_info *psi)
  479. {
  480. struct module *owner = psi->owner;
  481. if (backend && strcmp(backend, psi->name)) {
  482. pr_warn("ignoring unexpected backend '%s'\n", psi->name);
  483. return -EPERM;
  484. }
  485. /* Sanity check flags. */
  486. if (!psi->flags) {
  487. pr_warn("backend '%s' must support at least one frontend\n",
  488. psi->name);
  489. return -EINVAL;
  490. }
  491. /* Check for required functions. */
  492. if (!psi->read || !psi->write) {
  493. pr_warn("backend '%s' must implement read() and write()\n",
  494. psi->name);
  495. return -EINVAL;
  496. }
  497. spin_lock(&pstore_lock);
  498. if (psinfo) {
  499. pr_warn("backend '%s' already loaded: ignoring '%s'\n",
  500. psinfo->name, psi->name);
  501. spin_unlock(&pstore_lock);
  502. return -EBUSY;
  503. }
  504. if (!psi->write_user)
  505. psi->write_user = pstore_write_user_compat;
  506. psinfo = psi;
  507. mutex_init(&psinfo->read_mutex);
  508. spin_unlock(&pstore_lock);
  509. if (owner && !try_module_get(owner)) {
  510. psinfo = NULL;
  511. return -EINVAL;
  512. }
  513. allocate_buf_for_compression();
  514. if (pstore_is_mounted())
  515. pstore_get_records(0);
  516. if (psi->flags & PSTORE_FLAGS_DMESG)
  517. pstore_register_kmsg();
  518. if (psi->flags & PSTORE_FLAGS_CONSOLE)
  519. pstore_register_console();
  520. if (psi->flags & PSTORE_FLAGS_FTRACE)
  521. pstore_register_ftrace();
  522. if (psi->flags & PSTORE_FLAGS_PMSG)
  523. pstore_register_pmsg();
  524. /* Start watching for new records, if desired. */
  525. if (pstore_update_ms >= 0) {
  526. pstore_timer.expires = jiffies +
  527. msecs_to_jiffies(pstore_update_ms);
  528. add_timer(&pstore_timer);
  529. }
  530. /*
  531. * Update the module parameter backend, so it is visible
  532. * through /sys/module/pstore/parameters/backend
  533. */
  534. backend = psi->name;
  535. pr_info("Registered %s as persistent store backend\n", psi->name);
  536. module_put(owner);
  537. return 0;
  538. }
  539. EXPORT_SYMBOL_GPL(pstore_register);
  540. void pstore_unregister(struct pstore_info *psi)
  541. {
  542. /* Stop timer and make sure all work has finished. */
  543. pstore_update_ms = -1;
  544. del_timer_sync(&pstore_timer);
  545. flush_work(&pstore_work);
  546. if (psi->flags & PSTORE_FLAGS_PMSG)
  547. pstore_unregister_pmsg();
  548. if (psi->flags & PSTORE_FLAGS_FTRACE)
  549. pstore_unregister_ftrace();
  550. if (psi->flags & PSTORE_FLAGS_CONSOLE)
  551. pstore_unregister_console();
  552. if (psi->flags & PSTORE_FLAGS_DMESG)
  553. pstore_unregister_kmsg();
  554. free_buf_for_compression();
  555. psinfo = NULL;
  556. backend = NULL;
  557. }
  558. EXPORT_SYMBOL_GPL(pstore_unregister);
  559. static void decompress_record(struct pstore_record *record)
  560. {
  561. int unzipped_len;
  562. char *decompressed;
  563. if (!record->compressed)
  564. return;
  565. /* Only PSTORE_TYPE_DMESG support compression. */
  566. if (record->type != PSTORE_TYPE_DMESG) {
  567. pr_warn("ignored compressed record type %d\n", record->type);
  568. return;
  569. }
  570. /* No compression method has created the common buffer. */
  571. if (!big_oops_buf) {
  572. pr_warn("no decompression buffer allocated\n");
  573. return;
  574. }
  575. unzipped_len = pstore_decompress(record->buf, big_oops_buf,
  576. record->size, big_oops_buf_sz);
  577. if (unzipped_len <= 0) {
  578. pr_err("decompression failed: %d\n", unzipped_len);
  579. return;
  580. }
  581. /* Build new buffer for decompressed contents. */
  582. decompressed = kmalloc(unzipped_len + record->ecc_notice_size,
  583. GFP_KERNEL);
  584. if (!decompressed) {
  585. pr_err("decompression ran out of memory\n");
  586. return;
  587. }
  588. memcpy(decompressed, big_oops_buf, unzipped_len);
  589. /* Append ECC notice to decompressed buffer. */
  590. memcpy(decompressed + unzipped_len, record->buf + record->size,
  591. record->ecc_notice_size);
  592. /* Swap out compresed contents with decompressed contents. */
  593. kfree(record->buf);
  594. record->buf = decompressed;
  595. record->size = unzipped_len;
  596. record->compressed = false;
  597. }
  598. /*
  599. * Read all the records from one persistent store backend. Create
  600. * files in our filesystem. Don't warn about -EEXIST errors
  601. * when we are re-scanning the backing store looking to add new
  602. * error records.
  603. */
  604. void pstore_get_backend_records(struct pstore_info *psi,
  605. struct dentry *root, int quiet)
  606. {
  607. int failed = 0;
  608. unsigned int stop_loop = 65536;
  609. if (!psi || !root)
  610. return;
  611. mutex_lock(&psi->read_mutex);
  612. if (psi->open && psi->open(psi))
  613. goto out;
  614. /*
  615. * Backend callback read() allocates record.buf. decompress_record()
  616. * may reallocate record.buf. On success, pstore_mkfile() will keep
  617. * the record.buf, so free it only on failure.
  618. */
  619. for (; stop_loop; stop_loop--) {
  620. struct pstore_record *record;
  621. int rc;
  622. record = kzalloc(sizeof(*record), GFP_KERNEL);
  623. if (!record) {
  624. pr_err("out of memory creating record\n");
  625. break;
  626. }
  627. pstore_record_init(record, psi);
  628. record->size = psi->read(record);
  629. /* No more records left in backend? */
  630. if (record->size <= 0) {
  631. kfree(record);
  632. break;
  633. }
  634. decompress_record(record);
  635. rc = pstore_mkfile(root, record);
  636. if (rc) {
  637. /* pstore_mkfile() did not take record, so free it. */
  638. kfree(record->buf);
  639. kfree(record);
  640. if (rc != -EEXIST || !quiet)
  641. failed++;
  642. }
  643. }
  644. if (psi->close)
  645. psi->close(psi);
  646. out:
  647. mutex_unlock(&psi->read_mutex);
  648. if (failed)
  649. pr_warn("failed to create %d record(s) from '%s'\n",
  650. failed, psi->name);
  651. if (!stop_loop)
  652. pr_err("looping? Too many records seen from '%s'\n",
  653. psi->name);
  654. }
  655. static void pstore_dowork(struct work_struct *work)
  656. {
  657. pstore_get_records(1);
  658. }
  659. static void pstore_timefunc(struct timer_list *unused)
  660. {
  661. if (pstore_new_entry) {
  662. pstore_new_entry = 0;
  663. schedule_work(&pstore_work);
  664. }
  665. if (pstore_update_ms >= 0)
  666. mod_timer(&pstore_timer,
  667. jiffies + msecs_to_jiffies(pstore_update_ms));
  668. }
  669. void __init pstore_choose_compression(void)
  670. {
  671. const struct pstore_zbackend *step;
  672. if (!compress)
  673. return;
  674. for (step = zbackends; step->name; step++) {
  675. if (!strcmp(compress, step->name)) {
  676. zbackend = step;
  677. return;
  678. }
  679. }
  680. }
  681. static int __init pstore_init(void)
  682. {
  683. int ret;
  684. pstore_choose_compression();
  685. /*
  686. * Check if any pstore backends registered earlier but did not
  687. * initialize compression because crypto was not ready. If so,
  688. * initialize compression now.
  689. */
  690. allocate_buf_for_compression();
  691. ret = pstore_init_fs();
  692. if (ret)
  693. return ret;
  694. return 0;
  695. }
  696. late_initcall(pstore_init);
  697. static void __exit pstore_exit(void)
  698. {
  699. pstore_exit_fs();
  700. }
  701. module_exit(pstore_exit)
  702. module_param(compress, charp, 0444);
  703. MODULE_PARM_DESC(compress, "Pstore compression to use");
  704. module_param(backend, charp, 0444);
  705. MODULE_PARM_DESC(backend, "Pstore backend to use");
  706. MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
  707. MODULE_LICENSE("GPL");