platform.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Persistent Storage - platform driver interface parts.
  4. *
  5. * Copyright (C) 2007-2008 Google, Inc.
  6. * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com>
  7. */
  8. #define pr_fmt(fmt) "pstore: " fmt
  9. #include <linux/atomic.h>
  10. #include <linux/types.h>
  11. #include <linux/errno.h>
  12. #include <linux/init.h>
  13. #include <linux/kmsg_dump.h>
  14. #include <linux/console.h>
  15. #include <linux/mm.h>
  16. #include <linux/module.h>
  17. #include <linux/pstore.h>
  18. #include <linux/string.h>
  19. #include <linux/timer.h>
  20. #include <linux/slab.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/jiffies.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/zlib.h>
  26. #include "internal.h"
  27. /*
  28. * We defer making "oops" entries appear in pstore - see
  29. * whether the system is actually still running well enough
  30. * to let someone see the entry
  31. */
  32. static int pstore_update_ms = -1;
  33. module_param_named(update_ms, pstore_update_ms, int, 0600);
  34. MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content "
  35. "(default is -1, which means runtime updates are disabled; "
  36. "enabling this option may not be safe; it may lead to further "
  37. "corruption on Oopses)");
  38. /* Names should be in the same order as the enum pstore_type_id */
  39. static const char * const pstore_type_names[] = {
  40. "dmesg",
  41. "mce",
  42. "console",
  43. "ftrace",
  44. "rtas",
  45. "powerpc-ofw",
  46. "powerpc-common",
  47. "pmsg",
  48. "powerpc-opal",
  49. };
  50. static int pstore_new_entry;
  51. static void pstore_timefunc(struct timer_list *);
  52. static DEFINE_TIMER(pstore_timer, pstore_timefunc);
  53. static void pstore_dowork(struct work_struct *);
  54. static DECLARE_WORK(pstore_work, pstore_dowork);
  55. /*
  56. * psinfo_lock protects "psinfo" during calls to
  57. * pstore_register(), pstore_unregister(), and
  58. * the filesystem mount/unmount routines.
  59. */
  60. static DEFINE_MUTEX(psinfo_lock);
  61. struct pstore_info *psinfo;
  62. static char *backend;
  63. module_param(backend, charp, 0444);
  64. MODULE_PARM_DESC(backend, "specific backend to use");
  65. /*
  66. * pstore no longer implements compression via the crypto API, and only
  67. * supports zlib deflate compression implemented using the zlib library
  68. * interface. This removes additional complexity which is hard to justify for a
  69. * diagnostic facility that has to operate in conditions where the system may
  70. * have become unstable. Zlib deflate is comparatively small in terms of code
  71. * size, and compresses ASCII text comparatively well. In terms of compression
  72. * speed, deflate is not the best performer but for recording the log output on
  73. * a kernel panic, this is not considered critical.
  74. *
  75. * The only remaining arguments supported by the compress= module parameter are
  76. * 'deflate' and 'none'. To retain compatibility with existing installations,
  77. * all other values are logged and replaced with 'deflate'.
  78. */
  79. static char *compress = "deflate";
  80. module_param(compress, charp, 0444);
  81. MODULE_PARM_DESC(compress, "compression to use");
  82. /* How much of the kernel log to snapshot */
  83. unsigned int kmsg_bytes = CONFIG_PSTORE_DEFAULT_KMSG_BYTES;
  84. module_param(kmsg_bytes, uint, 0444);
  85. MODULE_PARM_DESC(kmsg_bytes, "amount of kernel log to snapshot (in bytes)");
  86. static void *compress_workspace;
  87. /*
  88. * Compression is only used for dmesg output, which consists of low-entropy
  89. * ASCII text, and so we can assume worst-case 60%.
  90. */
  91. #define DMESG_COMP_PERCENT 60
  92. static char *big_oops_buf;
  93. static size_t max_compressed_size;
  94. void pstore_set_kmsg_bytes(unsigned int bytes)
  95. {
  96. WRITE_ONCE(kmsg_bytes, bytes);
  97. }
  98. /* Tag each group of saved records with a sequence number */
  99. static int oopscount;
  100. const char *pstore_type_to_name(enum pstore_type_id type)
  101. {
  102. BUILD_BUG_ON(ARRAY_SIZE(pstore_type_names) != PSTORE_TYPE_MAX);
  103. if (WARN_ON_ONCE(type >= PSTORE_TYPE_MAX))
  104. return "unknown";
  105. return pstore_type_names[type];
  106. }
  107. EXPORT_SYMBOL_GPL(pstore_type_to_name);
  108. enum pstore_type_id pstore_name_to_type(const char *name)
  109. {
  110. int i;
  111. for (i = 0; i < PSTORE_TYPE_MAX; i++) {
  112. if (!strcmp(pstore_type_names[i], name))
  113. return i;
  114. }
  115. return PSTORE_TYPE_MAX;
  116. }
  117. EXPORT_SYMBOL_GPL(pstore_name_to_type);
  118. static void pstore_timer_kick(void)
  119. {
  120. if (pstore_update_ms < 0)
  121. return;
  122. mod_timer(&pstore_timer, jiffies + msecs_to_jiffies(pstore_update_ms));
  123. }
  124. static bool pstore_cannot_block_path(enum kmsg_dump_reason reason)
  125. {
  126. /*
  127. * In case of NMI path, pstore shouldn't be blocked
  128. * regardless of reason.
  129. */
  130. if (in_nmi())
  131. return true;
  132. switch (reason) {
  133. /* In panic case, other cpus are stopped by smp_send_stop(). */
  134. case KMSG_DUMP_PANIC:
  135. /*
  136. * Emergency restart shouldn't be blocked by spinning on
  137. * pstore_info::buf_lock.
  138. */
  139. case KMSG_DUMP_EMERG:
  140. return true;
  141. default:
  142. return false;
  143. }
  144. }
  145. static int pstore_compress(const void *in, void *out,
  146. unsigned int inlen, unsigned int outlen)
  147. {
  148. struct z_stream_s zstream = {
  149. .next_in = in,
  150. .avail_in = inlen,
  151. .next_out = out,
  152. .avail_out = outlen,
  153. .workspace = compress_workspace,
  154. };
  155. int ret;
  156. if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS))
  157. return -EINVAL;
  158. ret = zlib_deflateInit2(&zstream, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
  159. -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
  160. if (ret != Z_OK)
  161. return -EINVAL;
  162. ret = zlib_deflate(&zstream, Z_FINISH);
  163. if (ret != Z_STREAM_END)
  164. return -EINVAL;
  165. ret = zlib_deflateEnd(&zstream);
  166. if (ret != Z_OK)
  167. pr_warn_once("zlib_deflateEnd() failed: %d\n", ret);
  168. return zstream.total_out;
  169. }
  170. static void allocate_buf_for_compression(void)
  171. {
  172. size_t compressed_size;
  173. char *buf;
  174. /* Skip if not built-in or compression disabled. */
  175. if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !compress ||
  176. !strcmp(compress, "none")) {
  177. compress = NULL;
  178. return;
  179. }
  180. if (strcmp(compress, "deflate")) {
  181. pr_err("Unsupported compression '%s', falling back to deflate\n",
  182. compress);
  183. compress = "deflate";
  184. }
  185. /*
  186. * The compression buffer only needs to be as large as the maximum
  187. * uncompressed record size, since any record that would be expanded by
  188. * compression is just stored uncompressed.
  189. */
  190. compressed_size = (psinfo->bufsize * 100) / DMESG_COMP_PERCENT;
  191. buf = kvzalloc(compressed_size, GFP_KERNEL);
  192. if (!buf) {
  193. pr_err("Failed %zu byte compression buffer allocation for: %s\n",
  194. psinfo->bufsize, compress);
  195. return;
  196. }
  197. compress_workspace =
  198. vmalloc(zlib_deflate_workspacesize(MAX_WBITS, DEF_MEM_LEVEL));
  199. if (!compress_workspace) {
  200. pr_err("Failed to allocate zlib deflate workspace\n");
  201. kvfree(buf);
  202. return;
  203. }
  204. /* A non-NULL big_oops_buf indicates compression is available. */
  205. big_oops_buf = buf;
  206. max_compressed_size = compressed_size;
  207. pr_info("Using crash dump compression: %s\n", compress);
  208. }
  209. static void free_buf_for_compression(void)
  210. {
  211. if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && compress_workspace) {
  212. vfree(compress_workspace);
  213. compress_workspace = NULL;
  214. }
  215. kvfree(big_oops_buf);
  216. big_oops_buf = NULL;
  217. max_compressed_size = 0;
  218. }
  219. void pstore_record_init(struct pstore_record *record,
  220. struct pstore_info *psinfo)
  221. {
  222. memset(record, 0, sizeof(*record));
  223. record->psi = psinfo;
  224. /* Report zeroed timestamp if called before timekeeping has resumed. */
  225. record->time = ns_to_timespec64(ktime_get_real_fast_ns());
  226. }
  227. /*
  228. * callback from kmsg_dump. Save as much as we can (up to kmsg_bytes) from the
  229. * end of the buffer.
  230. */
  231. static void pstore_dump(struct kmsg_dumper *dumper,
  232. struct kmsg_dump_detail *detail)
  233. {
  234. struct kmsg_dump_iter iter;
  235. unsigned int remaining = READ_ONCE(kmsg_bytes);
  236. unsigned long total = 0;
  237. const char *why;
  238. unsigned int part = 1;
  239. unsigned long flags = 0;
  240. int saved_ret = 0;
  241. int ret;
  242. why = kmsg_dump_reason_str(detail->reason);
  243. if (pstore_cannot_block_path(detail->reason)) {
  244. if (!raw_spin_trylock_irqsave(&psinfo->buf_lock, flags)) {
  245. pr_err("dump skipped in %s path because of concurrent dump\n",
  246. in_nmi() ? "NMI" : why);
  247. return;
  248. }
  249. } else {
  250. raw_spin_lock_irqsave(&psinfo->buf_lock, flags);
  251. }
  252. kmsg_dump_rewind(&iter);
  253. oopscount++;
  254. while (total < remaining) {
  255. char *dst;
  256. size_t dst_size;
  257. int header_size;
  258. int zipped_len = -1;
  259. size_t dump_size;
  260. struct pstore_record record;
  261. pstore_record_init(&record, psinfo);
  262. record.type = PSTORE_TYPE_DMESG;
  263. record.count = oopscount;
  264. record.reason = detail->reason;
  265. record.part = part;
  266. record.buf = psinfo->buf;
  267. dst = big_oops_buf ?: psinfo->buf;
  268. dst_size = max_compressed_size ?: psinfo->bufsize;
  269. /* Write dump header. */
  270. header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why,
  271. oopscount, part);
  272. dst_size -= header_size;
  273. /* Write dump contents. */
  274. if (!kmsg_dump_get_buffer(&iter, true, dst + header_size,
  275. dst_size, &dump_size))
  276. break;
  277. if (big_oops_buf) {
  278. zipped_len = pstore_compress(dst, psinfo->buf,
  279. header_size + dump_size,
  280. psinfo->bufsize);
  281. if (zipped_len > 0) {
  282. record.compressed = true;
  283. record.size = zipped_len;
  284. } else {
  285. /*
  286. * Compression failed, so the buffer is most
  287. * likely filled with binary data that does not
  288. * compress as well as ASCII text. Copy as much
  289. * of the uncompressed data as possible into
  290. * the pstore record, and discard the rest.
  291. */
  292. record.size = psinfo->bufsize;
  293. memcpy(psinfo->buf, dst, psinfo->bufsize);
  294. }
  295. } else {
  296. record.size = header_size + dump_size;
  297. }
  298. ret = psinfo->write(&record);
  299. if (ret == 0 && detail->reason == KMSG_DUMP_OOPS) {
  300. pstore_new_entry = 1;
  301. pstore_timer_kick();
  302. } else {
  303. /* Preserve only the first non-zero returned value. */
  304. if (!saved_ret)
  305. saved_ret = ret;
  306. }
  307. total += record.size;
  308. part++;
  309. }
  310. raw_spin_unlock_irqrestore(&psinfo->buf_lock, flags);
  311. if (saved_ret) {
  312. pr_err_once("backend (%s) writing error (%d)\n", psinfo->name,
  313. saved_ret);
  314. }
  315. }
  316. static struct kmsg_dumper pstore_dumper = {
  317. .dump = pstore_dump,
  318. };
  319. /*
  320. * Register with kmsg_dump to save last part of console log on panic.
  321. */
  322. static void pstore_register_kmsg(void)
  323. {
  324. kmsg_dump_register(&pstore_dumper);
  325. }
  326. static void pstore_unregister_kmsg(void)
  327. {
  328. kmsg_dump_unregister(&pstore_dumper);
  329. }
  330. #ifdef CONFIG_PSTORE_CONSOLE
  331. static void pstore_console_write(struct console *con, const char *s, unsigned c)
  332. {
  333. struct pstore_record record;
  334. if (!c)
  335. return;
  336. pstore_record_init(&record, psinfo);
  337. record.type = PSTORE_TYPE_CONSOLE;
  338. record.buf = (char *)s;
  339. record.size = c;
  340. psinfo->write(&record);
  341. }
  342. static struct console pstore_console = {
  343. .write = pstore_console_write,
  344. .index = -1,
  345. };
  346. static void pstore_register_console(void)
  347. {
  348. /* Show which backend is going to get console writes. */
  349. strscpy(pstore_console.name, psinfo->name,
  350. sizeof(pstore_console.name));
  351. /*
  352. * Always initialize flags here since prior unregister_console()
  353. * calls may have changed settings (specifically CON_ENABLED).
  354. */
  355. pstore_console.flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME;
  356. register_console(&pstore_console);
  357. }
  358. static void pstore_unregister_console(void)
  359. {
  360. unregister_console(&pstore_console);
  361. }
  362. #else
  363. static void pstore_register_console(void) {}
  364. static void pstore_unregister_console(void) {}
  365. #endif
  366. static int pstore_write_user_compat(struct pstore_record *record,
  367. const char __user *buf)
  368. {
  369. int ret = 0;
  370. if (record->buf)
  371. return -EINVAL;
  372. record->buf = vmemdup_user(buf, record->size);
  373. if (IS_ERR(record->buf)) {
  374. ret = PTR_ERR(record->buf);
  375. goto out;
  376. }
  377. ret = record->psi->write(record);
  378. kvfree(record->buf);
  379. out:
  380. record->buf = NULL;
  381. return unlikely(ret < 0) ? ret : record->size;
  382. }
  383. /*
  384. * platform specific persistent storage driver registers with
  385. * us here. If pstore is already mounted, call the platform
  386. * read function right away to populate the file system. If not
  387. * then the pstore mount code will call us later to fill out
  388. * the file system.
  389. */
  390. int pstore_register(struct pstore_info *psi)
  391. {
  392. char *new_backend;
  393. if (backend && strcmp(backend, psi->name)) {
  394. pr_warn("backend '%s' already in use: ignoring '%s'\n",
  395. backend, psi->name);
  396. return -EBUSY;
  397. }
  398. /* Sanity check flags. */
  399. if (!psi->flags) {
  400. pr_warn("backend '%s' must support at least one frontend\n",
  401. psi->name);
  402. return -EINVAL;
  403. }
  404. /* Check for required functions. */
  405. if (!psi->read || !psi->write) {
  406. pr_warn("backend '%s' must implement read() and write()\n",
  407. psi->name);
  408. return -EINVAL;
  409. }
  410. new_backend = kstrdup(psi->name, GFP_KERNEL);
  411. if (!new_backend)
  412. return -ENOMEM;
  413. mutex_lock(&psinfo_lock);
  414. if (psinfo) {
  415. pr_warn("backend '%s' already loaded: ignoring '%s'\n",
  416. psinfo->name, psi->name);
  417. mutex_unlock(&psinfo_lock);
  418. kfree(new_backend);
  419. return -EBUSY;
  420. }
  421. if (!psi->write_user)
  422. psi->write_user = pstore_write_user_compat;
  423. psinfo = psi;
  424. mutex_init(&psinfo->read_mutex);
  425. raw_spin_lock_init(&psinfo->buf_lock);
  426. if (psi->flags & PSTORE_FLAGS_DMESG)
  427. allocate_buf_for_compression();
  428. pstore_get_records(0);
  429. if (psi->flags & PSTORE_FLAGS_DMESG) {
  430. pstore_dumper.max_reason = psinfo->max_reason;
  431. pstore_register_kmsg();
  432. }
  433. if (psi->flags & PSTORE_FLAGS_CONSOLE)
  434. pstore_register_console();
  435. if (psi->flags & PSTORE_FLAGS_FTRACE)
  436. pstore_register_ftrace();
  437. if (psi->flags & PSTORE_FLAGS_PMSG)
  438. pstore_register_pmsg();
  439. /* Start watching for new records, if desired. */
  440. pstore_timer_kick();
  441. /*
  442. * Update the module parameter backend, so it is visible
  443. * through /sys/module/pstore/parameters/backend
  444. */
  445. backend = new_backend;
  446. pr_info("Registered %s as persistent store backend\n", psi->name);
  447. mutex_unlock(&psinfo_lock);
  448. return 0;
  449. }
  450. EXPORT_SYMBOL_GPL(pstore_register);
  451. void pstore_unregister(struct pstore_info *psi)
  452. {
  453. /* It's okay to unregister nothing. */
  454. if (!psi)
  455. return;
  456. mutex_lock(&psinfo_lock);
  457. /* Only one backend can be registered at a time. */
  458. if (WARN_ON(psi != psinfo)) {
  459. mutex_unlock(&psinfo_lock);
  460. return;
  461. }
  462. /* Unregister all callbacks. */
  463. if (psi->flags & PSTORE_FLAGS_PMSG)
  464. pstore_unregister_pmsg();
  465. if (psi->flags & PSTORE_FLAGS_FTRACE)
  466. pstore_unregister_ftrace();
  467. if (psi->flags & PSTORE_FLAGS_CONSOLE)
  468. pstore_unregister_console();
  469. if (psi->flags & PSTORE_FLAGS_DMESG)
  470. pstore_unregister_kmsg();
  471. /* Stop timer and make sure all work has finished. */
  472. timer_delete_sync(&pstore_timer);
  473. flush_work(&pstore_work);
  474. /* Remove all backend records from filesystem tree. */
  475. pstore_put_backend_records(psi);
  476. free_buf_for_compression();
  477. psinfo = NULL;
  478. kfree(backend);
  479. backend = NULL;
  480. pr_info("Unregistered %s as persistent store backend\n", psi->name);
  481. mutex_unlock(&psinfo_lock);
  482. }
  483. EXPORT_SYMBOL_GPL(pstore_unregister);
  484. static void decompress_record(struct pstore_record *record,
  485. struct z_stream_s *zstream)
  486. {
  487. int ret;
  488. int unzipped_len;
  489. char *unzipped, *workspace;
  490. size_t max_uncompressed_size;
  491. if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !record->compressed)
  492. return;
  493. /* Only PSTORE_TYPE_DMESG support compression. */
  494. if (record->type != PSTORE_TYPE_DMESG) {
  495. pr_warn("ignored compressed record type %d\n", record->type);
  496. return;
  497. }
  498. /* Missing compression buffer means compression was not initialized. */
  499. if (!zstream->workspace) {
  500. pr_warn("no decompression method initialized!\n");
  501. return;
  502. }
  503. ret = zlib_inflateReset(zstream);
  504. if (ret != Z_OK) {
  505. pr_err("zlib_inflateReset() failed, ret = %d!\n", ret);
  506. return;
  507. }
  508. /* Allocate enough space to hold max decompression and ECC. */
  509. max_uncompressed_size = 3 * psinfo->bufsize;
  510. workspace = kvzalloc(max_uncompressed_size + record->ecc_notice_size,
  511. GFP_KERNEL);
  512. if (!workspace)
  513. return;
  514. zstream->next_in = record->buf;
  515. zstream->avail_in = record->size;
  516. zstream->next_out = workspace;
  517. zstream->avail_out = max_uncompressed_size;
  518. ret = zlib_inflate(zstream, Z_FINISH);
  519. if (ret != Z_STREAM_END) {
  520. pr_err_ratelimited("zlib_inflate() failed, ret = %d!\n", ret);
  521. kvfree(workspace);
  522. return;
  523. }
  524. unzipped_len = zstream->total_out;
  525. /* Append ECC notice to decompressed buffer. */
  526. memcpy(workspace + unzipped_len, record->buf + record->size,
  527. record->ecc_notice_size);
  528. /* Copy decompressed contents into an minimum-sized allocation. */
  529. unzipped = kvmemdup(workspace, unzipped_len + record->ecc_notice_size,
  530. GFP_KERNEL);
  531. kvfree(workspace);
  532. if (!unzipped)
  533. return;
  534. /* Swap out compressed contents with decompressed contents. */
  535. kvfree(record->buf);
  536. record->buf = unzipped;
  537. record->size = unzipped_len;
  538. record->compressed = false;
  539. }
  540. /*
  541. * Read all the records from one persistent store backend. Create
  542. * files in our filesystem. Don't warn about -EEXIST errors
  543. * when we are re-scanning the backing store looking to add new
  544. * error records.
  545. */
  546. void pstore_get_backend_records(struct pstore_info *psi,
  547. struct dentry *root, int quiet)
  548. {
  549. int failed = 0;
  550. unsigned int stop_loop = 65536;
  551. struct z_stream_s zstream = {};
  552. if (!psi || !root)
  553. return;
  554. if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && compress) {
  555. zstream.workspace = kvmalloc(zlib_inflate_workspacesize(),
  556. GFP_KERNEL);
  557. zlib_inflateInit2(&zstream, -DEF_WBITS);
  558. }
  559. mutex_lock(&psi->read_mutex);
  560. if (psi->open && psi->open(psi))
  561. goto out;
  562. /*
  563. * Backend callback read() allocates record.buf. decompress_record()
  564. * may reallocate record.buf. On success, pstore_mkfile() will keep
  565. * the record.buf, so free it only on failure.
  566. */
  567. for (; stop_loop; stop_loop--) {
  568. struct pstore_record *record;
  569. int rc;
  570. record = kzalloc_obj(*record);
  571. if (!record) {
  572. pr_err("out of memory creating record\n");
  573. break;
  574. }
  575. pstore_record_init(record, psi);
  576. record->size = psi->read(record);
  577. /* No more records left in backend? */
  578. if (record->size <= 0) {
  579. kfree(record);
  580. break;
  581. }
  582. decompress_record(record, &zstream);
  583. rc = pstore_mkfile(root, record);
  584. if (rc) {
  585. /* pstore_mkfile() did not take record, so free it. */
  586. kvfree(record->buf);
  587. kfree(record->priv);
  588. kfree(record);
  589. if (rc != -EEXIST || !quiet)
  590. failed++;
  591. }
  592. }
  593. if (psi->close)
  594. psi->close(psi);
  595. out:
  596. mutex_unlock(&psi->read_mutex);
  597. if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && compress) {
  598. if (zlib_inflateEnd(&zstream) != Z_OK)
  599. pr_warn("zlib_inflateEnd() failed\n");
  600. kvfree(zstream.workspace);
  601. }
  602. if (failed)
  603. pr_warn("failed to create %d record(s) from '%s'\n",
  604. failed, psi->name);
  605. if (!stop_loop)
  606. pr_err("looping? Too many records seen from '%s'\n",
  607. psi->name);
  608. }
  609. static void pstore_dowork(struct work_struct *work)
  610. {
  611. pstore_get_records(1);
  612. }
  613. static void pstore_timefunc(struct timer_list *unused)
  614. {
  615. if (pstore_new_entry) {
  616. pstore_new_entry = 0;
  617. schedule_work(&pstore_work);
  618. }
  619. pstore_timer_kick();
  620. }
  621. static int __init pstore_init(void)
  622. {
  623. int ret;
  624. ret = pstore_init_fs();
  625. if (ret)
  626. free_buf_for_compression();
  627. return ret;
  628. }
  629. late_initcall(pstore_init);
  630. static void __exit pstore_exit(void)
  631. {
  632. pstore_exit_fs();
  633. }
  634. module_exit(pstore_exit)
  635. MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
  636. MODULE_DESCRIPTION("Persistent Storage - platform driver interface");
  637. MODULE_LICENSE("GPL");