145051539SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2ca01d6ddSTony Luck /* 3ca01d6ddSTony Luck * Persistent Storage - platform driver interface parts. 4ca01d6ddSTony Luck * 5f29e5956SAnton Vorontsov * Copyright (C) 2007-2008 Google, Inc. 6ca01d6ddSTony Luck * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com> 7ca01d6ddSTony Luck */ 8ca01d6ddSTony Luck 9ef748853SFabian Frederick #define pr_fmt(fmt) "pstore: " fmt 10ef748853SFabian Frederick 11ca01d6ddSTony Luck #include <linux/atomic.h> 12ca01d6ddSTony Luck #include <linux/types.h> 13ca01d6ddSTony Luck #include <linux/errno.h> 14ca01d6ddSTony Luck #include <linux/init.h> 15ca01d6ddSTony Luck #include <linux/kmsg_dump.h> 16f29e5956SAnton Vorontsov #include <linux/console.h> 17ca01d6ddSTony Luck #include <linux/module.h> 18ca01d6ddSTony Luck #include <linux/pstore.h> 1958eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS) 208cfc8ddcSGeliang Tang #include <linux/lzo.h> 218cfc8ddcSGeliang Tang #endif 2258eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS) 238cfc8ddcSGeliang Tang #include <linux/lz4.h> 248cfc8ddcSGeliang Tang #endif 251021bcf4SGeliang Tang #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS) 261021bcf4SGeliang Tang #include <linux/zstd.h> 271021bcf4SGeliang Tang #endif 28cb3bee03SGeliang Tang #include <linux/crypto.h> 29ca01d6ddSTony Luck #include <linux/string.h> 306dda9266SLuck, Tony #include <linux/timer.h> 31ca01d6ddSTony Luck #include <linux/slab.h> 32ca01d6ddSTony Luck #include <linux/uaccess.h> 33a3f5f075SAnton Vorontsov #include <linux/jiffies.h> 346dda9266SLuck, Tony #include <linux/workqueue.h> 35ca01d6ddSTony Luck 36ca01d6ddSTony Luck #include "internal.h" 37ca01d6ddSTony Luck 38ca01d6ddSTony Luck /* 396dda9266SLuck, Tony * We defer making "oops" entries appear in pstore - see 406dda9266SLuck, Tony * whether the system is actually still running well enough 416dda9266SLuck, Tony * to let someone see the entry 426dda9266SLuck, Tony */ 43521f7288SAnton Vorontsov static int pstore_update_ms = -1; 44a3f5f075SAnton Vorontsov module_param_named(update_ms, pstore_update_ms, int, 0600); 45a3f5f075SAnton Vorontsov MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content " 46521f7288SAnton Vorontsov "(default is -1, which means runtime updates are disabled; " 4778c83c82SKees Cook "enabling this option may not be safe; it may lead to further " 48521f7288SAnton Vorontsov "corruption on Oopses)"); 496dda9266SLuck, Tony 50f0f23e54SJoel Fernandes (Google) /* Names should be in the same order as the enum pstore_type_id */ 51f0f23e54SJoel Fernandes (Google) static const char * const pstore_type_names[] = { 52f0f23e54SJoel Fernandes (Google) "dmesg", 53f0f23e54SJoel Fernandes (Google) "mce", 54f0f23e54SJoel Fernandes (Google) "console", 55f0f23e54SJoel Fernandes (Google) "ftrace", 56f0f23e54SJoel Fernandes (Google) "rtas", 57f0f23e54SJoel Fernandes (Google) "powerpc-ofw", 58f0f23e54SJoel Fernandes (Google) "powerpc-common", 59f0f23e54SJoel Fernandes (Google) "pmsg", 60f0f23e54SJoel Fernandes (Google) "powerpc-opal", 61f0f23e54SJoel Fernandes (Google) }; 62f0f23e54SJoel Fernandes (Google) 636dda9266SLuck, Tony static int pstore_new_entry; 646dda9266SLuck, Tony 6524ed960aSKees Cook static void pstore_timefunc(struct timer_list *); 661d27e3e2SKees Cook static DEFINE_TIMER(pstore_timer, pstore_timefunc); 676dda9266SLuck, Tony 686dda9266SLuck, Tony static void pstore_dowork(struct work_struct *); 696dda9266SLuck, Tony static DECLARE_WORK(pstore_work, pstore_dowork); 706dda9266SLuck, Tony 716dda9266SLuck, Tony /* 726248a066SKees Cook * psinfo_lock protects "psinfo" during calls to 736248a066SKees Cook * pstore_register(), pstore_unregister(), and 746248a066SKees Cook * the filesystem mount/unmount routines. 75ca01d6ddSTony Luck */ 76cab12fd0SKees Cook static DEFINE_MUTEX(psinfo_lock); 77060287b8SAnton Vorontsov struct pstore_info *psinfo; 78ca01d6ddSTony Luck 79dee28e72SMatthew Garrett static char *backend; 80d973f7d8SKees Cook module_param(backend, charp, 0444); 81d973f7d8SKees Cook MODULE_PARM_DESC(backend, "specific backend to use"); 82d973f7d8SKees Cook 83fe1d4758SKees Cook static char *compress = 84fe1d4758SKees Cook #ifdef CONFIG_PSTORE_COMPRESS_DEFAULT 85fe1d4758SKees Cook CONFIG_PSTORE_COMPRESS_DEFAULT; 86fe1d4758SKees Cook #else 87fe1d4758SKees Cook NULL; 88fe1d4758SKees Cook #endif 89d973f7d8SKees Cook module_param(compress, charp, 0444); 90d973f7d8SKees Cook MODULE_PARM_DESC(compress, "compression to use"); 91dee28e72SMatthew Garrett 928f5de3fdSGuilherme G. Piccoli /* How much of the kernel log to snapshot */ 938f5de3fdSGuilherme G. Piccoli unsigned long kmsg_bytes = CONFIG_PSTORE_DEFAULT_KMSG_BYTES; 948f5de3fdSGuilherme G. Piccoli module_param(kmsg_bytes, ulong, 0444); 958f5de3fdSGuilherme G. Piccoli MODULE_PARM_DESC(kmsg_bytes, "amount of kernel log to snapshot (in bytes)"); 968f5de3fdSGuilherme G. Piccoli 97b0aad7a9SAruna Balakrishnaiah /* Compression parameters */ 9840158dbfSGuilherme G. Piccoli static struct crypto_comp *tfm; 998cfc8ddcSGeliang Tang 1008cfc8ddcSGeliang Tang struct pstore_zbackend { 101cb3bee03SGeliang Tang int (*zbufsize)(size_t size); 1028cfc8ddcSGeliang Tang const char *name; 1038cfc8ddcSGeliang Tang }; 104b0aad7a9SAruna Balakrishnaiah 105b0aad7a9SAruna Balakrishnaiah static char *big_oops_buf; 106b0aad7a9SAruna Balakrishnaiah static size_t big_oops_buf_sz; 107b0aad7a9SAruna Balakrishnaiah 108366f7e7aSLuck, Tony void pstore_set_kmsg_bytes(int bytes) 109ca01d6ddSTony Luck { 110366f7e7aSLuck, Tony kmsg_bytes = bytes; 111ca01d6ddSTony Luck } 112ca01d6ddSTony Luck 113ca01d6ddSTony Luck /* Tag each group of saved records with a sequence number */ 114ca01d6ddSTony Luck static int oopscount; 115ca01d6ddSTony Luck 116f0f23e54SJoel Fernandes (Google) const char *pstore_type_to_name(enum pstore_type_id type) 117f0f23e54SJoel Fernandes (Google) { 118f0f23e54SJoel Fernandes (Google) BUILD_BUG_ON(ARRAY_SIZE(pstore_type_names) != PSTORE_TYPE_MAX); 119f0f23e54SJoel Fernandes (Google) 120f0f23e54SJoel Fernandes (Google) if (WARN_ON_ONCE(type >= PSTORE_TYPE_MAX)) 121f0f23e54SJoel Fernandes (Google) return "unknown"; 122f0f23e54SJoel Fernandes (Google) 123f0f23e54SJoel Fernandes (Google) return pstore_type_names[type]; 124f0f23e54SJoel Fernandes (Google) } 125f0f23e54SJoel Fernandes (Google) EXPORT_SYMBOL_GPL(pstore_type_to_name); 126f0f23e54SJoel Fernandes (Google) 127f0f23e54SJoel Fernandes (Google) enum pstore_type_id pstore_name_to_type(const char *name) 128f0f23e54SJoel Fernandes (Google) { 129f0f23e54SJoel Fernandes (Google) int i; 130f0f23e54SJoel Fernandes (Google) 131f0f23e54SJoel Fernandes (Google) for (i = 0; i < PSTORE_TYPE_MAX; i++) { 132f0f23e54SJoel Fernandes (Google) if (!strcmp(pstore_type_names[i], name)) 133f0f23e54SJoel Fernandes (Google) return i; 134f0f23e54SJoel Fernandes (Google) } 135f0f23e54SJoel Fernandes (Google) 136f0f23e54SJoel Fernandes (Google) return PSTORE_TYPE_MAX; 137f0f23e54SJoel Fernandes (Google) } 138f0f23e54SJoel Fernandes (Google) EXPORT_SYMBOL_GPL(pstore_name_to_type); 139f0f23e54SJoel Fernandes (Google) 14078c83c82SKees Cook static void pstore_timer_kick(void) 14178c83c82SKees Cook { 14278c83c82SKees Cook if (pstore_update_ms < 0) 14378c83c82SKees Cook return; 14478c83c82SKees Cook 14578c83c82SKees Cook mod_timer(&pstore_timer, jiffies + msecs_to_jiffies(pstore_update_ms)); 14678c83c82SKees Cook } 14778c83c82SKees Cook 1488126b1c7SJann Horn static bool pstore_cannot_block_path(enum kmsg_dump_reason reason) 149ea84b580SKees Cook { 1508126b1c7SJann Horn /* 1518126b1c7SJann Horn * In case of NMI path, pstore shouldn't be blocked 1528126b1c7SJann Horn * regardless of reason. 1538126b1c7SJann Horn */ 1549f244e9cSSeiji Aguchi if (in_nmi()) 1559f244e9cSSeiji Aguchi return true; 1569f244e9cSSeiji Aguchi 1579f244e9cSSeiji Aguchi switch (reason) { 1589f244e9cSSeiji Aguchi /* In panic case, other cpus are stopped by smp_send_stop(). */ 1599f244e9cSSeiji Aguchi case KMSG_DUMP_PANIC: 1608126b1c7SJann Horn /* 1618126b1c7SJann Horn * Emergency restart shouldn't be blocked by spinning on 1628126b1c7SJann Horn * pstore_info::buf_lock. 1638126b1c7SJann Horn */ 1649f244e9cSSeiji Aguchi case KMSG_DUMP_EMERG: 1659f244e9cSSeiji Aguchi return true; 1669f244e9cSSeiji Aguchi default: 1679f244e9cSSeiji Aguchi return false; 1689f244e9cSSeiji Aguchi } 1699f244e9cSSeiji Aguchi } 1709f244e9cSSeiji Aguchi 17158eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS) 172cb3bee03SGeliang Tang static int zbufsize_deflate(size_t size) 173b0aad7a9SAruna Balakrishnaiah { 1747de8fe2fSAruna Balakrishnaiah size_t cmpr; 175b0aad7a9SAruna Balakrishnaiah 176cb3bee03SGeliang Tang switch (size) { 1777de8fe2fSAruna Balakrishnaiah /* buffer range for efivars */ 1787de8fe2fSAruna Balakrishnaiah case 1000 ... 2000: 1797de8fe2fSAruna Balakrishnaiah cmpr = 56; 1807de8fe2fSAruna Balakrishnaiah break; 1817de8fe2fSAruna Balakrishnaiah case 2001 ... 3000: 1827de8fe2fSAruna Balakrishnaiah cmpr = 54; 1837de8fe2fSAruna Balakrishnaiah break; 1847de8fe2fSAruna Balakrishnaiah case 3001 ... 3999: 1857de8fe2fSAruna Balakrishnaiah cmpr = 52; 1867de8fe2fSAruna Balakrishnaiah break; 1877de8fe2fSAruna Balakrishnaiah /* buffer range for nvram, erst */ 1887de8fe2fSAruna Balakrishnaiah case 4000 ... 10000: 1897de8fe2fSAruna Balakrishnaiah cmpr = 45; 1907de8fe2fSAruna Balakrishnaiah break; 1917de8fe2fSAruna Balakrishnaiah default: 1927de8fe2fSAruna Balakrishnaiah cmpr = 60; 1937de8fe2fSAruna Balakrishnaiah break; 1947de8fe2fSAruna Balakrishnaiah } 1957de8fe2fSAruna Balakrishnaiah 196cb3bee03SGeliang Tang return (size * 100) / cmpr; 1978cfc8ddcSGeliang Tang } 1988cfc8ddcSGeliang Tang #endif 1998cfc8ddcSGeliang Tang 20058eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS) 201cb3bee03SGeliang Tang static int zbufsize_lzo(size_t size) 2028cfc8ddcSGeliang Tang { 203cb3bee03SGeliang Tang return lzo1x_worst_compress(size); 2048cfc8ddcSGeliang Tang } 2058cfc8ddcSGeliang Tang #endif 2068cfc8ddcSGeliang Tang 20758eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS) 208cb3bee03SGeliang Tang static int zbufsize_lz4(size_t size) 2098cfc8ddcSGeliang Tang { 210cb3bee03SGeliang Tang return LZ4_compressBound(size); 211239b7161SGeliang Tang } 212239b7161SGeliang Tang #endif 213239b7161SGeliang Tang 21458eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS) 215cb3bee03SGeliang Tang static int zbufsize_842(size_t size) 216239b7161SGeliang Tang { 21755597406SKees Cook return size; 218239b7161SGeliang Tang } 219fe1d4758SKees Cook #endif 2208cfc8ddcSGeliang Tang 2211021bcf4SGeliang Tang #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS) 2221021bcf4SGeliang Tang static int zbufsize_zstd(size_t size) 2231021bcf4SGeliang Tang { 224cf30f6a5SNick Terrell return zstd_compress_bound(size); 2251021bcf4SGeliang Tang } 2261021bcf4SGeliang Tang #endif 2271021bcf4SGeliang Tang 228fe1d4758SKees Cook static const struct pstore_zbackend *zbackend __ro_after_init; 229fe1d4758SKees Cook 230fe1d4758SKees Cook static const struct pstore_zbackend zbackends[] = { 23158eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS) 232fe1d4758SKees Cook { 233cb3bee03SGeliang Tang .zbufsize = zbufsize_deflate, 234cb3bee03SGeliang Tang .name = "deflate", 235fe1d4758SKees Cook }, 236fe1d4758SKees Cook #endif 23758eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS) 238fe1d4758SKees Cook { 239cb3bee03SGeliang Tang .zbufsize = zbufsize_lzo, 240fe1d4758SKees Cook .name = "lzo", 241fe1d4758SKees Cook }, 242fe1d4758SKees Cook #endif 24358eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) 244fe1d4758SKees Cook { 245cb3bee03SGeliang Tang .zbufsize = zbufsize_lz4, 246fe1d4758SKees Cook .name = "lz4", 247fe1d4758SKees Cook }, 248fe1d4758SKees Cook #endif 24958eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS) 250fe1d4758SKees Cook { 251cb3bee03SGeliang Tang .zbufsize = zbufsize_lz4, 252fe1d4758SKees Cook .name = "lz4hc", 253fe1d4758SKees Cook }, 254fe1d4758SKees Cook #endif 25558eb5b67SArnd Bergmann #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS) 256fe1d4758SKees Cook { 257cb3bee03SGeliang Tang .zbufsize = zbufsize_842, 258239b7161SGeliang Tang .name = "842", 259fe1d4758SKees Cook }, 260fe1d4758SKees Cook #endif 2611021bcf4SGeliang Tang #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS) 2621021bcf4SGeliang Tang { 2631021bcf4SGeliang Tang .zbufsize = zbufsize_zstd, 2641021bcf4SGeliang Tang .name = "zstd", 2651021bcf4SGeliang Tang }, 2661021bcf4SGeliang Tang #endif 267fe1d4758SKees Cook { } 2688cfc8ddcSGeliang Tang }; 2698cfc8ddcSGeliang Tang 2708cfc8ddcSGeliang Tang static int pstore_compress(const void *in, void *out, 271cb3bee03SGeliang Tang unsigned int inlen, unsigned int outlen) 2728cfc8ddcSGeliang Tang { 273cb3bee03SGeliang Tang int ret; 274cb3bee03SGeliang Tang 27519d8e914SJiri Bohac if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS)) 276fd49e032SMatteo Croce return -EINVAL; 277fd49e032SMatteo Croce 27840158dbfSGuilherme G. Piccoli ret = crypto_comp_compress(tfm, in, inlen, out, &outlen); 279cb3bee03SGeliang Tang if (ret) { 280cb3bee03SGeliang Tang pr_err("crypto_comp_compress failed, ret = %d!\n", ret); 281cb3bee03SGeliang Tang return ret; 2828cfc8ddcSGeliang Tang } 2838cfc8ddcSGeliang Tang 284cb3bee03SGeliang Tang return outlen; 285cb3bee03SGeliang Tang } 286cb3bee03SGeliang Tang 2878cfc8ddcSGeliang Tang static void allocate_buf_for_compression(void) 2888cfc8ddcSGeliang Tang { 28940158dbfSGuilherme G. Piccoli struct crypto_comp *ctx; 29095047b05SKees Cook int size; 29195047b05SKees Cook char *buf; 29295047b05SKees Cook 29395047b05SKees Cook /* Skip if not built-in or compression backend not selected yet. */ 294e698aaf3STobias Regnery if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !zbackend) 295cb3bee03SGeliang Tang return; 296cb3bee03SGeliang Tang 29795047b05SKees Cook /* Skip if no pstore backend yet or compression init already done. */ 29895047b05SKees Cook if (!psinfo || tfm) 29995047b05SKees Cook return; 30095047b05SKees Cook 30140158dbfSGuilherme G. Piccoli if (!crypto_has_comp(zbackend->name, 0, 0)) { 30295047b05SKees Cook pr_err("Unknown compression: %s\n", zbackend->name); 303cb3bee03SGeliang Tang return; 304cb3bee03SGeliang Tang } 305cb3bee03SGeliang Tang 30695047b05SKees Cook size = zbackend->zbufsize(psinfo->bufsize); 30795047b05SKees Cook if (size <= 0) { 30895047b05SKees Cook pr_err("Invalid compression size for %s: %d\n", 30995047b05SKees Cook zbackend->name, size); 310cb3bee03SGeliang Tang return; 311cb3bee03SGeliang Tang } 312cb3bee03SGeliang Tang 31395047b05SKees Cook buf = kmalloc(size, GFP_KERNEL); 31495047b05SKees Cook if (!buf) { 31595047b05SKees Cook pr_err("Failed %d byte compression buffer allocation for: %s\n", 31695047b05SKees Cook size, zbackend->name); 317cb3bee03SGeliang Tang return; 3188cfc8ddcSGeliang Tang } 31995047b05SKees Cook 32040158dbfSGuilherme G. Piccoli ctx = crypto_alloc_comp(zbackend->name, 0, 0); 32140158dbfSGuilherme G. Piccoli if (IS_ERR_OR_NULL(ctx)) { 32295047b05SKees Cook kfree(buf); 32395047b05SKees Cook pr_err("crypto_alloc_comp('%s') failed: %ld\n", zbackend->name, 32440158dbfSGuilherme G. Piccoli PTR_ERR(ctx)); 32595047b05SKees Cook return; 32695047b05SKees Cook } 32795047b05SKees Cook 32895047b05SKees Cook /* A non-NULL big_oops_buf indicates compression is available. */ 32940158dbfSGuilherme G. Piccoli tfm = ctx; 33095047b05SKees Cook big_oops_buf_sz = size; 33195047b05SKees Cook big_oops_buf = buf; 33295047b05SKees Cook 3330eed84ffSKees Cook pr_info("Using crash dump compression: %s\n", zbackend->name); 3348cfc8ddcSGeliang Tang } 3358cfc8ddcSGeliang Tang 3368cfc8ddcSGeliang Tang static void free_buf_for_compression(void) 3378cfc8ddcSGeliang Tang { 338a9fb94a9SPi-Hsun Shih if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && tfm) { 33940158dbfSGuilherme G. Piccoli crypto_free_comp(tfm); 340a9fb94a9SPi-Hsun Shih tfm = NULL; 341a9fb94a9SPi-Hsun Shih } 342cb3bee03SGeliang Tang kfree(big_oops_buf); 343cb3bee03SGeliang Tang big_oops_buf = NULL; 344cb3bee03SGeliang Tang big_oops_buf_sz = 0; 345ee1d2674SGeliang Tang } 346ee1d2674SGeliang Tang 347b0aad7a9SAruna Balakrishnaiah /* 348b0aad7a9SAruna Balakrishnaiah * Called when compression fails, since the printk buffer 349b0aad7a9SAruna Balakrishnaiah * would be fetched for compression calling it again when 350b0aad7a9SAruna Balakrishnaiah * compression fails would have moved the iterator of 351b0aad7a9SAruna Balakrishnaiah * printk buffer which results in fetching old contents. 352b0aad7a9SAruna Balakrishnaiah * Copy the recent messages from big_oops_buf to psinfo->buf 353b0aad7a9SAruna Balakrishnaiah */ 354b0aad7a9SAruna Balakrishnaiah static size_t copy_kmsg_to_buffer(int hsize, size_t len) 355b0aad7a9SAruna Balakrishnaiah { 356b0aad7a9SAruna Balakrishnaiah size_t total_len; 357b0aad7a9SAruna Balakrishnaiah size_t diff; 358b0aad7a9SAruna Balakrishnaiah 359b0aad7a9SAruna Balakrishnaiah total_len = hsize + len; 360b0aad7a9SAruna Balakrishnaiah 361b0aad7a9SAruna Balakrishnaiah if (total_len > psinfo->bufsize) { 362b0aad7a9SAruna Balakrishnaiah diff = total_len - psinfo->bufsize + hsize; 363b0aad7a9SAruna Balakrishnaiah memcpy(psinfo->buf, big_oops_buf, hsize); 364b0aad7a9SAruna Balakrishnaiah memcpy(psinfo->buf + hsize, big_oops_buf + diff, 365b0aad7a9SAruna Balakrishnaiah psinfo->bufsize - hsize); 366b0aad7a9SAruna Balakrishnaiah total_len = psinfo->bufsize; 367b0aad7a9SAruna Balakrishnaiah } else 368b0aad7a9SAruna Balakrishnaiah memcpy(psinfo->buf, big_oops_buf, total_len); 369b0aad7a9SAruna Balakrishnaiah 370b0aad7a9SAruna Balakrishnaiah return total_len; 371b0aad7a9SAruna Balakrishnaiah } 372b0aad7a9SAruna Balakrishnaiah 373e581ca81SKees Cook void pstore_record_init(struct pstore_record *record, 374e581ca81SKees Cook struct pstore_info *psinfo) 375e581ca81SKees Cook { 376e581ca81SKees Cook memset(record, 0, sizeof(*record)); 377e581ca81SKees Cook 378e581ca81SKees Cook record->psi = psinfo; 379c7f3c595SKees Cook 380c7f3c595SKees Cook /* Report zeroed timestamp if called before timekeeping has resumed. */ 3817aaa822eSKees Cook record->time = ns_to_timespec64(ktime_get_real_fast_ns()); 382e581ca81SKees Cook } 383e581ca81SKees Cook 384ca01d6ddSTony Luck /* 3850eed84ffSKees Cook * callback from kmsg_dump. Save as much as we can (up to kmsg_bytes) from the 3860eed84ffSKees Cook * end of the buffer. 387ca01d6ddSTony Luck */ 388ca01d6ddSTony Luck static void pstore_dump(struct kmsg_dumper *dumper, 389e2ae715dSKay Sievers enum kmsg_dump_reason reason) 390ca01d6ddSTony Luck { 391f9f3f02dSJohn Ogness struct kmsg_dump_iter iter; 392e2ae715dSKay Sievers unsigned long total = 0; 393381b872cSSeiji Aguchi const char *why; 394b94fdd07SMatthew Garrett unsigned int part = 1; 3958126b1c7SJann Horn unsigned long flags = 0; 396*38b91847SGuilherme G. Piccoli int saved_ret = 0; 397e2ae715dSKay Sievers int ret; 398ca01d6ddSTony Luck 399fb13cb8aSKees Cook why = kmsg_dump_reason_str(reason); 4009f6af27fSTony Luck 4018126b1c7SJann Horn if (pstore_cannot_block_path(reason)) { 4028126b1c7SJann Horn if (!spin_trylock_irqsave(&psinfo->buf_lock, flags)) { 4038126b1c7SJann Horn pr_err("dump skipped in %s path because of concurrent dump\n", 404ea84b580SKees Cook in_nmi() ? "NMI" : why); 405959217c8SLi Pengcheng return; 4069f244e9cSSeiji Aguchi } 4078126b1c7SJann Horn } else { 4088126b1c7SJann Horn spin_lock_irqsave(&psinfo->buf_lock, flags); 409ea84b580SKees Cook } 410ea84b580SKees Cook 411f9f3f02dSJohn Ogness kmsg_dump_rewind(&iter); 412f9f3f02dSJohn Ogness 413ca01d6ddSTony Luck oopscount++; 414ca01d6ddSTony Luck while (total < kmsg_bytes) { 415e2ae715dSKay Sievers char *dst; 41676cc9580SKees Cook size_t dst_size; 41776cc9580SKees Cook int header_size; 418b0aad7a9SAruna Balakrishnaiah int zipped_len = -1; 41976cc9580SKees Cook size_t dump_size; 420e581ca81SKees Cook struct pstore_record record; 421e581ca81SKees Cook 422e581ca81SKees Cook pstore_record_init(&record, psinfo); 423e581ca81SKees Cook record.type = PSTORE_TYPE_DMESG; 424e581ca81SKees Cook record.count = oopscount; 425e581ca81SKees Cook record.reason = reason; 426e581ca81SKees Cook record.part = part; 427e581ca81SKees Cook record.buf = psinfo->buf; 428e2ae715dSKay Sievers 429ea84b580SKees Cook if (big_oops_buf) { 430b0aad7a9SAruna Balakrishnaiah dst = big_oops_buf; 43176cc9580SKees Cook dst_size = big_oops_buf_sz; 432235f6d15SNamhyung Kim } else { 433235f6d15SNamhyung Kim dst = psinfo->buf; 43476cc9580SKees Cook dst_size = psinfo->bufsize; 435235f6d15SNamhyung Kim } 436235f6d15SNamhyung Kim 43776cc9580SKees Cook /* Write dump header. */ 43876cc9580SKees Cook header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why, 43976cc9580SKees Cook oopscount, part); 44076cc9580SKees Cook dst_size -= header_size; 441b0aad7a9SAruna Balakrishnaiah 44276cc9580SKees Cook /* Write dump contents. */ 443f9f3f02dSJohn Ogness if (!kmsg_dump_get_buffer(&iter, true, dst + header_size, 44476cc9580SKees Cook dst_size, &dump_size)) 445b0aad7a9SAruna Balakrishnaiah break; 446b0aad7a9SAruna Balakrishnaiah 447ea84b580SKees Cook if (big_oops_buf) { 448b0aad7a9SAruna Balakrishnaiah zipped_len = pstore_compress(dst, psinfo->buf, 44976cc9580SKees Cook header_size + dump_size, 45076cc9580SKees Cook psinfo->bufsize); 451b0aad7a9SAruna Balakrishnaiah 452b0aad7a9SAruna Balakrishnaiah if (zipped_len > 0) { 45376cc9580SKees Cook record.compressed = true; 45476cc9580SKees Cook record.size = zipped_len; 455b0aad7a9SAruna Balakrishnaiah } else { 45676cc9580SKees Cook record.size = copy_kmsg_to_buffer(header_size, 45776cc9580SKees Cook dump_size); 458b0aad7a9SAruna Balakrishnaiah } 459b0aad7a9SAruna Balakrishnaiah } else { 46076cc9580SKees Cook record.size = header_size + dump_size; 461b0aad7a9SAruna Balakrishnaiah } 462b0aad7a9SAruna Balakrishnaiah 46376cc9580SKees Cook ret = psinfo->write(&record); 46478c83c82SKees Cook if (ret == 0 && reason == KMSG_DUMP_OOPS) { 4656dda9266SLuck, Tony pstore_new_entry = 1; 46678c83c82SKees Cook pstore_timer_kick(); 467*38b91847SGuilherme G. Piccoli } else { 468*38b91847SGuilherme G. Piccoli /* Preserve only the first non-zero returned value. */ 469*38b91847SGuilherme G. Piccoli if (!saved_ret) 470*38b91847SGuilherme G. Piccoli saved_ret = ret; 47178c83c82SKees Cook } 472e2ae715dSKay Sievers 47376cc9580SKees Cook total += record.size; 47456280682SMatthew Garrett part++; 475ca01d6ddSTony Luck } 4768126b1c7SJann Horn spin_unlock_irqrestore(&psinfo->buf_lock, flags); 477*38b91847SGuilherme G. Piccoli 478*38b91847SGuilherme G. Piccoli if (saved_ret) { 479*38b91847SGuilherme G. Piccoli pr_err_once("backend (%s) writing error (%d)\n", psinfo->name, 480*38b91847SGuilherme G. Piccoli saved_ret); 481*38b91847SGuilherme G. Piccoli } 482ca01d6ddSTony Luck } 483ca01d6ddSTony Luck 484ca01d6ddSTony Luck static struct kmsg_dumper pstore_dumper = { 485ca01d6ddSTony Luck .dump = pstore_dump, 486ca01d6ddSTony Luck }; 487ca01d6ddSTony Luck 488306e5c2aSGeliang Tang /* 489306e5c2aSGeliang Tang * Register with kmsg_dump to save last part of console log on panic. 490306e5c2aSGeliang Tang */ 49118730411SGeliang Tang static void pstore_register_kmsg(void) 49218730411SGeliang Tang { 49318730411SGeliang Tang kmsg_dump_register(&pstore_dumper); 49418730411SGeliang Tang } 49518730411SGeliang Tang 496ee1d2674SGeliang Tang static void pstore_unregister_kmsg(void) 497ee1d2674SGeliang Tang { 498ee1d2674SGeliang Tang kmsg_dump_unregister(&pstore_dumper); 499ee1d2674SGeliang Tang } 500ee1d2674SGeliang Tang 501f29e5956SAnton Vorontsov #ifdef CONFIG_PSTORE_CONSOLE 502f29e5956SAnton Vorontsov static void pstore_console_write(struct console *con, const char *s, unsigned c) 503f29e5956SAnton Vorontsov { 504e581ca81SKees Cook struct pstore_record record; 505f29e5956SAnton Vorontsov 5064c6c4d34SYue Hu if (!c) 5074c6c4d34SYue Hu return; 5084c6c4d34SYue Hu 509e581ca81SKees Cook pstore_record_init(&record, psinfo); 510e581ca81SKees Cook record.type = PSTORE_TYPE_CONSOLE; 511e581ca81SKees Cook 512b10b4711SKees Cook record.buf = (char *)s; 513b10b4711SKees Cook record.size = c; 5144c9ec219SKees Cook psinfo->write(&record); 515f29e5956SAnton Vorontsov } 516f29e5956SAnton Vorontsov 517f29e5956SAnton Vorontsov static struct console pstore_console = { 518f29e5956SAnton Vorontsov .write = pstore_console_write, 519f29e5956SAnton Vorontsov .index = -1, 520f29e5956SAnton Vorontsov }; 521f29e5956SAnton Vorontsov 522f29e5956SAnton Vorontsov static void pstore_register_console(void) 523f29e5956SAnton Vorontsov { 524d195c390SKees Cook /* Show which backend is going to get console writes. */ 525d195c390SKees Cook strscpy(pstore_console.name, psinfo->name, 526d195c390SKees Cook sizeof(pstore_console.name)); 527b7753fc7SKees Cook /* 528b7753fc7SKees Cook * Always initialize flags here since prior unregister_console() 529b7753fc7SKees Cook * calls may have changed settings (specifically CON_ENABLED). 530b7753fc7SKees Cook */ 531b7753fc7SKees Cook pstore_console.flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME; 532f29e5956SAnton Vorontsov register_console(&pstore_console); 533f29e5956SAnton Vorontsov } 534ee1d2674SGeliang Tang 535ee1d2674SGeliang Tang static void pstore_unregister_console(void) 536ee1d2674SGeliang Tang { 537ee1d2674SGeliang Tang unregister_console(&pstore_console); 538ee1d2674SGeliang Tang } 539f29e5956SAnton Vorontsov #else 540f29e5956SAnton Vorontsov static void pstore_register_console(void) {} 541ee1d2674SGeliang Tang static void pstore_unregister_console(void) {} 542f29e5956SAnton Vorontsov #endif 543f29e5956SAnton Vorontsov 5444c9ec219SKees Cook static int pstore_write_user_compat(struct pstore_record *record, 545fdd03118SKees Cook const char __user *buf) 5465bf6d1b9SMark Salyzyn { 54730800d99SKees Cook int ret = 0; 5485bf6d1b9SMark Salyzyn 54930800d99SKees Cook if (record->buf) 55030800d99SKees Cook return -EINVAL; 5515bf6d1b9SMark Salyzyn 552077090afSGeliang Tang record->buf = memdup_user(buf, record->size); 553dfd6fa39SHirofumi Nakagawa if (IS_ERR(record->buf)) { 554077090afSGeliang Tang ret = PTR_ERR(record->buf); 55530800d99SKees Cook goto out; 5565bf6d1b9SMark Salyzyn } 55730800d99SKees Cook 5584c9ec219SKees Cook ret = record->psi->write(record); 55930800d99SKees Cook 56030800d99SKees Cook kfree(record->buf); 561077090afSGeliang Tang out: 56230800d99SKees Cook record->buf = NULL; 56330800d99SKees Cook 56430800d99SKees Cook return unlikely(ret < 0) ? ret : record->size; 5655bf6d1b9SMark Salyzyn } 5665bf6d1b9SMark Salyzyn 567ca01d6ddSTony Luck /* 568ca01d6ddSTony Luck * platform specific persistent storage driver registers with 569ca01d6ddSTony Luck * us here. If pstore is already mounted, call the platform 570ca01d6ddSTony Luck * read function right away to populate the file system. If not 571ca01d6ddSTony Luck * then the pstore mount code will call us later to fill out 572ca01d6ddSTony Luck * the file system. 573ca01d6ddSTony Luck */ 574ca01d6ddSTony Luck int pstore_register(struct pstore_info *psi) 575ca01d6ddSTony Luck { 5760d7cd09aSKees Cook if (backend && strcmp(backend, psi->name)) { 577d85644dcSGuilherme G. Piccoli pr_warn("backend '%s' already in use: ignoring '%s'\n", 578d85644dcSGuilherme G. Piccoli backend, psi->name); 579d85644dcSGuilherme G. Piccoli return -EBUSY; 5800d7cd09aSKees Cook } 5818e48b1a8SLenny Szubowicz 5824c9ec219SKees Cook /* Sanity check flags. */ 5834c9ec219SKees Cook if (!psi->flags) { 5844c9ec219SKees Cook pr_warn("backend '%s' must support at least one frontend\n", 5854c9ec219SKees Cook psi->name); 5864c9ec219SKees Cook return -EINVAL; 5874c9ec219SKees Cook } 5884c9ec219SKees Cook 5894c9ec219SKees Cook /* Check for required functions. */ 5904c9ec219SKees Cook if (!psi->read || !psi->write) { 5914c9ec219SKees Cook pr_warn("backend '%s' must implement read() and write()\n", 5924c9ec219SKees Cook psi->name); 5934c9ec219SKees Cook return -EINVAL; 5944c9ec219SKees Cook } 5954c9ec219SKees Cook 596cab12fd0SKees Cook mutex_lock(&psinfo_lock); 597ca01d6ddSTony Luck if (psinfo) { 5980d7cd09aSKees Cook pr_warn("backend '%s' already loaded: ignoring '%s'\n", 5990d7cd09aSKees Cook psinfo->name, psi->name); 600cab12fd0SKees Cook mutex_unlock(&psinfo_lock); 601ca01d6ddSTony Luck return -EBUSY; 602ca01d6ddSTony Luck } 603dee28e72SMatthew Garrett 6044c9ec219SKees Cook if (!psi->write_user) 6054c9ec219SKees Cook psi->write_user = pstore_write_user_compat; 606ca01d6ddSTony Luck psinfo = psi; 607f6f82851SKees Cook mutex_init(&psinfo->read_mutex); 6088126b1c7SJann Horn spin_lock_init(&psinfo->buf_lock); 609ca01d6ddSTony Luck 6108880fa32SKees Cook if (psi->flags & PSTORE_FLAGS_DMESG) 611b0aad7a9SAruna Balakrishnaiah allocate_buf_for_compression(); 612b0aad7a9SAruna Balakrishnaiah 6136dda9266SLuck, Tony pstore_get_records(0); 614ca01d6ddSTony Luck 6153524e688SPavel Tatashin if (psi->flags & PSTORE_FLAGS_DMESG) { 6163524e688SPavel Tatashin pstore_dumper.max_reason = psinfo->max_reason; 61718730411SGeliang Tang pstore_register_kmsg(); 6183524e688SPavel Tatashin } 619c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_CONSOLE) 620f29e5956SAnton Vorontsov pstore_register_console(); 621c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_FTRACE) 62265f8c95eSAnton Vorontsov pstore_register_ftrace(); 623c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_PMSG) 6249d5438f4SMark Salyzyn pstore_register_pmsg(); 625ca01d6ddSTony Luck 6266330d553SKees Cook /* Start watching for new records, if desired. */ 62778c83c82SKees Cook pstore_timer_kick(); 6286dda9266SLuck, Tony 62942222c2aSWang Long /* 63042222c2aSWang Long * Update the module parameter backend, so it is visible 63142222c2aSWang Long * through /sys/module/pstore/parameters/backend 63242222c2aSWang Long */ 633563ca40dSKees Cook backend = kstrdup(psi->name, GFP_KERNEL); 63442222c2aSWang Long 635ef748853SFabian Frederick pr_info("Registered %s as persistent store backend\n", psi->name); 6368e48b1a8SLenny Szubowicz 6376248a066SKees Cook mutex_unlock(&psinfo_lock); 638ca01d6ddSTony Luck return 0; 639ca01d6ddSTony Luck } 640ca01d6ddSTony Luck EXPORT_SYMBOL_GPL(pstore_register); 641ca01d6ddSTony Luck 642ee1d2674SGeliang Tang void pstore_unregister(struct pstore_info *psi) 643ee1d2674SGeliang Tang { 6446248a066SKees Cook /* It's okay to unregister nothing. */ 6456248a066SKees Cook if (!psi) 6466248a066SKees Cook return; 6476248a066SKees Cook 6486248a066SKees Cook mutex_lock(&psinfo_lock); 6496248a066SKees Cook 6506248a066SKees Cook /* Only one backend can be registered at a time. */ 6516248a066SKees Cook if (WARN_ON(psi != psinfo)) { 6526248a066SKees Cook mutex_unlock(&psinfo_lock); 6536248a066SKees Cook return; 6546248a066SKees Cook } 6556248a066SKees Cook 65678c83c82SKees Cook /* Unregister all callbacks. */ 657c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_PMSG) 658ee1d2674SGeliang Tang pstore_unregister_pmsg(); 659c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_FTRACE) 660ee1d2674SGeliang Tang pstore_unregister_ftrace(); 661c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_CONSOLE) 662ee1d2674SGeliang Tang pstore_unregister_console(); 663c950fd6fSNamhyung Kim if (psi->flags & PSTORE_FLAGS_DMESG) 664ee1d2674SGeliang Tang pstore_unregister_kmsg(); 665ee1d2674SGeliang Tang 66678c83c82SKees Cook /* Stop timer and make sure all work has finished. */ 66778c83c82SKees Cook del_timer_sync(&pstore_timer); 66878c83c82SKees Cook flush_work(&pstore_work); 66978c83c82SKees Cook 670609e28bbSKees Cook /* Remove all backend records from filesystem tree. */ 671609e28bbSKees Cook pstore_put_backend_records(psi); 672609e28bbSKees Cook 673ee1d2674SGeliang Tang free_buf_for_compression(); 674ee1d2674SGeliang Tang 675ee1d2674SGeliang Tang psinfo = NULL; 676563ca40dSKees Cook kfree(backend); 677ee1d2674SGeliang Tang backend = NULL; 6786a14f198SGuilherme G. Piccoli 6796a14f198SGuilherme G. Piccoli pr_info("Unregistered %s as persistent store backend\n", psi->name); 6806248a066SKees Cook mutex_unlock(&psinfo_lock); 681ee1d2674SGeliang Tang } 682ee1d2674SGeliang Tang EXPORT_SYMBOL_GPL(pstore_unregister); 683ee1d2674SGeliang Tang 684634f8f51SKees Cook static void decompress_record(struct pstore_record *record) 685634f8f51SKees Cook { 686bdabc8e7SKees Cook int ret; 687634f8f51SKees Cook int unzipped_len; 688bdabc8e7SKees Cook char *unzipped, *workspace; 689634f8f51SKees Cook 69019d8e914SJiri Bohac if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !record->compressed) 6914a16d1cbSAnkit Kumar return; 6924a16d1cbSAnkit Kumar 693634f8f51SKees Cook /* Only PSTORE_TYPE_DMESG support compression. */ 6944a16d1cbSAnkit Kumar if (record->type != PSTORE_TYPE_DMESG) { 695634f8f51SKees Cook pr_warn("ignored compressed record type %d\n", record->type); 696634f8f51SKees Cook return; 697634f8f51SKees Cook } 698634f8f51SKees Cook 699bdabc8e7SKees Cook /* Missing compression buffer means compression was not initialized. */ 700634f8f51SKees Cook if (!big_oops_buf) { 701bdabc8e7SKees Cook pr_warn("no decompression method initialized!\n"); 702634f8f51SKees Cook return; 703634f8f51SKees Cook } 704634f8f51SKees Cook 705bdabc8e7SKees Cook /* Allocate enough space to hold max decompression and ECC. */ 706bdabc8e7SKees Cook unzipped_len = big_oops_buf_sz; 707bdabc8e7SKees Cook workspace = kmalloc(unzipped_len + record->ecc_notice_size, 7087e8cc8dcSKees Cook GFP_KERNEL); 709bdabc8e7SKees Cook if (!workspace) 710bdabc8e7SKees Cook return; 711bdabc8e7SKees Cook 712bdabc8e7SKees Cook /* After decompression "unzipped_len" is almost certainly smaller. */ 71340158dbfSGuilherme G. Piccoli ret = crypto_comp_decompress(tfm, record->buf, record->size, 71440158dbfSGuilherme G. Piccoli workspace, &unzipped_len); 715bdabc8e7SKees Cook if (ret) { 71640158dbfSGuilherme G. Piccoli pr_err("crypto_comp_decompress failed, ret = %d!\n", ret); 717bdabc8e7SKees Cook kfree(workspace); 7187e8cc8dcSKees Cook return; 7197e8cc8dcSKees Cook } 7207e8cc8dcSKees Cook 7217e8cc8dcSKees Cook /* Append ECC notice to decompressed buffer. */ 722bdabc8e7SKees Cook memcpy(workspace + unzipped_len, record->buf + record->size, 723634f8f51SKees Cook record->ecc_notice_size); 7247e8cc8dcSKees Cook 725bdabc8e7SKees Cook /* Copy decompressed contents into an minimum-sized allocation. */ 726bdabc8e7SKees Cook unzipped = kmemdup(workspace, unzipped_len + record->ecc_notice_size, 727bdabc8e7SKees Cook GFP_KERNEL); 728bdabc8e7SKees Cook kfree(workspace); 729bdabc8e7SKees Cook if (!unzipped) 730bdabc8e7SKees Cook return; 731bdabc8e7SKees Cook 732bdabc8e7SKees Cook /* Swap out compressed contents with decompressed contents. */ 733634f8f51SKees Cook kfree(record->buf); 734bdabc8e7SKees Cook record->buf = unzipped; 735634f8f51SKees Cook record->size = unzipped_len; 736634f8f51SKees Cook record->compressed = false; 737634f8f51SKees Cook } 738634f8f51SKees Cook 739ca01d6ddSTony Luck /* 7403a7d2fd1SKees Cook * Read all the records from one persistent store backend. Create 7416dda9266SLuck, Tony * files in our filesystem. Don't warn about -EEXIST errors 7426dda9266SLuck, Tony * when we are re-scanning the backing store looking to add new 7436dda9266SLuck, Tony * error records. 744ca01d6ddSTony Luck */ 7453a7d2fd1SKees Cook void pstore_get_backend_records(struct pstore_info *psi, 7463a7d2fd1SKees Cook struct dentry *root, int quiet) 747ca01d6ddSTony Luck { 7482a2b0acfSKees Cook int failed = 0; 749656de42eSKees Cook unsigned int stop_loop = 65536; 750ca01d6ddSTony Luck 7513a7d2fd1SKees Cook if (!psi || !root) 752ca01d6ddSTony Luck return; 753ca01d6ddSTony Luck 754f6f82851SKees Cook mutex_lock(&psi->read_mutex); 7552174f6dfSKees Cook if (psi->open && psi->open(psi)) 75606cf91b4SChen Gong goto out; 75706cf91b4SChen Gong 7581dfff7ddSKees Cook /* 7591dfff7ddSKees Cook * Backend callback read() allocates record.buf. decompress_record() 7601dfff7ddSKees Cook * may reallocate record.buf. On success, pstore_mkfile() will keep 7611dfff7ddSKees Cook * the record.buf, so free it only on failure. 7621dfff7ddSKees Cook */ 763656de42eSKees Cook for (; stop_loop; stop_loop--) { 7642a2b0acfSKees Cook struct pstore_record *record; 7652a2b0acfSKees Cook int rc; 7662a2b0acfSKees Cook 7672a2b0acfSKees Cook record = kzalloc(sizeof(*record), GFP_KERNEL); 7682a2b0acfSKees Cook if (!record) { 7692a2b0acfSKees Cook pr_err("out of memory creating record\n"); 7702a2b0acfSKees Cook break; 7712a2b0acfSKees Cook } 772e581ca81SKees Cook pstore_record_init(record, psi); 7732a2b0acfSKees Cook 7742a2b0acfSKees Cook record->size = psi->read(record); 7752a2b0acfSKees Cook 7762a2b0acfSKees Cook /* No more records left in backend? */ 777f6525b96SDouglas Anderson if (record->size <= 0) { 778f6525b96SDouglas Anderson kfree(record); 7792a2b0acfSKees Cook break; 780f6525b96SDouglas Anderson } 7812a2b0acfSKees Cook 7822a2b0acfSKees Cook decompress_record(record); 7833a7d2fd1SKees Cook rc = pstore_mkfile(root, record); 7841dfff7ddSKees Cook if (rc) { 78583f70f07SKees Cook /* pstore_mkfile() did not take record, so free it. */ 7862a2b0acfSKees Cook kfree(record->buf); 7878ca869b2SArd Biesheuvel kfree(record->priv); 78883f70f07SKees Cook kfree(record); 7891dfff7ddSKees Cook if (rc != -EEXIST || !quiet) 7901dfff7ddSKees Cook failed++; 7911dfff7ddSKees Cook } 792ca01d6ddSTony Luck } 7932174f6dfSKees Cook if (psi->close) 79406cf91b4SChen Gong psi->close(psi); 79506cf91b4SChen Gong out: 796f6f82851SKees Cook mutex_unlock(&psi->read_mutex); 797ca01d6ddSTony Luck 798ca01d6ddSTony Luck if (failed) 799656de42eSKees Cook pr_warn("failed to create %d record(s) from '%s'\n", 800ca01d6ddSTony Luck failed, psi->name); 801656de42eSKees Cook if (!stop_loop) 802656de42eSKees Cook pr_err("looping? Too many records seen from '%s'\n", 803656de42eSKees Cook psi->name); 804ca01d6ddSTony Luck } 805ca01d6ddSTony Luck 8066dda9266SLuck, Tony static void pstore_dowork(struct work_struct *work) 8076dda9266SLuck, Tony { 8086dda9266SLuck, Tony pstore_get_records(1); 8096dda9266SLuck, Tony } 8106dda9266SLuck, Tony 81124ed960aSKees Cook static void pstore_timefunc(struct timer_list *unused) 8126dda9266SLuck, Tony { 8136dda9266SLuck, Tony if (pstore_new_entry) { 8146dda9266SLuck, Tony pstore_new_entry = 0; 8156dda9266SLuck, Tony schedule_work(&pstore_work); 8166dda9266SLuck, Tony } 8176dda9266SLuck, Tony 81878c83c82SKees Cook pstore_timer_kick(); 8196dda9266SLuck, Tony } 8206dda9266SLuck, Tony 8218d82cee2SBen Dooks (Codethink) static void __init pstore_choose_compression(void) 822fe1d4758SKees Cook { 823fe1d4758SKees Cook const struct pstore_zbackend *step; 824fe1d4758SKees Cook 825fe1d4758SKees Cook if (!compress) 826fe1d4758SKees Cook return; 827fe1d4758SKees Cook 828fe1d4758SKees Cook for (step = zbackends; step->name; step++) { 829fe1d4758SKees Cook if (!strcmp(compress, step->name)) { 830fe1d4758SKees Cook zbackend = step; 831fe1d4758SKees Cook return; 832fe1d4758SKees Cook } 833fe1d4758SKees Cook } 834fe1d4758SKees Cook } 835fe1d4758SKees Cook 836cb095afdSKees Cook static int __init pstore_init(void) 837cb095afdSKees Cook { 838cb095afdSKees Cook int ret; 839cb095afdSKees Cook 840cb095afdSKees Cook pstore_choose_compression(); 841cb095afdSKees Cook 84241603165SJoel Fernandes (Google) /* 84341603165SJoel Fernandes (Google) * Check if any pstore backends registered earlier but did not 84441603165SJoel Fernandes (Google) * initialize compression because crypto was not ready. If so, 84541603165SJoel Fernandes (Google) * initialize compression now. 84641603165SJoel Fernandes (Google) */ 84741603165SJoel Fernandes (Google) allocate_buf_for_compression(); 84841603165SJoel Fernandes (Google) 849cb095afdSKees Cook ret = pstore_init_fs(); 850cb095afdSKees Cook if (ret) 8518a57d6d4Schenqiwu free_buf_for_compression(); 852cb095afdSKees Cook 8538a57d6d4Schenqiwu return ret; 854cb095afdSKees Cook } 85541603165SJoel Fernandes (Google) late_initcall(pstore_init); 856cb095afdSKees Cook 857cb095afdSKees Cook static void __exit pstore_exit(void) 858cb095afdSKees Cook { 859cb095afdSKees Cook pstore_exit_fs(); 860cb095afdSKees Cook } 861cb095afdSKees Cook module_exit(pstore_exit) 862cb095afdSKees Cook 863cb095afdSKees Cook MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>"); 864cb095afdSKees Cook MODULE_LICENSE("GPL"); 865