platform.c 18 KB

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