OSDN Git Service

printk: Collapse shutdown types into a single dump reason
[tomoyo/tomoyo-test1.git] / fs / pstore / platform.c
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
9 #define pr_fmt(fmt) "pstore: " fmt
10
11 #include <linux/atomic.h>
12 #include <linux/types.h>
13 #include <linux/errno.h>
14 #include <linux/init.h>
15 #include <linux/kmsg_dump.h>
16 #include <linux/console.h>
17 #include <linux/module.h>
18 #include <linux/pstore.h>
19 #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
20 #include <linux/lzo.h>
21 #endif
22 #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
23 #include <linux/lz4.h>
24 #endif
25 #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
26 #include <linux/zstd.h>
27 #endif
28 #include <linux/crypto.h>
29 #include <linux/string.h>
30 #include <linux/timer.h>
31 #include <linux/slab.h>
32 #include <linux/uaccess.h>
33 #include <linux/jiffies.h>
34 #include <linux/workqueue.h>
35
36 #include "internal.h"
37
38 /*
39  * We defer making "oops" entries appear in pstore - see
40  * whether the system is actually still running well enough
41  * to let someone see the entry
42  */
43 static int pstore_update_ms = -1;
44 module_param_named(update_ms, pstore_update_ms, int, 0600);
45 MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content "
46                  "(default is -1, which means runtime updates are disabled; "
47                  "enabling this option may not be safe; it may lead to further "
48                  "corruption on Oopses)");
49
50 /* Names should be in the same order as the enum pstore_type_id */
51 static const char * const pstore_type_names[] = {
52         "dmesg",
53         "mce",
54         "console",
55         "ftrace",
56         "rtas",
57         "powerpc-ofw",
58         "powerpc-common",
59         "pmsg",
60         "powerpc-opal",
61 };
62
63 static int pstore_new_entry;
64
65 static void pstore_timefunc(struct timer_list *);
66 static DEFINE_TIMER(pstore_timer, pstore_timefunc);
67
68 static void pstore_dowork(struct work_struct *);
69 static DECLARE_WORK(pstore_work, pstore_dowork);
70
71 /*
72  * psinfo_lock protects "psinfo" during calls to
73  * pstore_register(), pstore_unregister(), and
74  * the filesystem mount/unmount routines.
75  */
76 static DEFINE_MUTEX(psinfo_lock);
77 struct pstore_info *psinfo;
78
79 static char *backend;
80 module_param(backend, charp, 0444);
81 MODULE_PARM_DESC(backend, "specific backend to use");
82
83 static char *compress =
84 #ifdef CONFIG_PSTORE_COMPRESS_DEFAULT
85                 CONFIG_PSTORE_COMPRESS_DEFAULT;
86 #else
87                 NULL;
88 #endif
89 module_param(compress, charp, 0444);
90 MODULE_PARM_DESC(compress, "compression to use");
91
92 /* Compression parameters */
93 static struct crypto_comp *tfm;
94
95 struct pstore_zbackend {
96         int (*zbufsize)(size_t size);
97         const char *name;
98 };
99
100 static char *big_oops_buf;
101 static size_t big_oops_buf_sz;
102
103 /* How much of the console log to snapshot */
104 unsigned long kmsg_bytes = PSTORE_DEFAULT_KMSG_BYTES;
105
106 void pstore_set_kmsg_bytes(int bytes)
107 {
108         kmsg_bytes = bytes;
109 }
110
111 /* Tag each group of saved records with a sequence number */
112 static int      oopscount;
113
114 const char *pstore_type_to_name(enum pstore_type_id type)
115 {
116         BUILD_BUG_ON(ARRAY_SIZE(pstore_type_names) != PSTORE_TYPE_MAX);
117
118         if (WARN_ON_ONCE(type >= PSTORE_TYPE_MAX))
119                 return "unknown";
120
121         return pstore_type_names[type];
122 }
123 EXPORT_SYMBOL_GPL(pstore_type_to_name);
124
125 enum pstore_type_id pstore_name_to_type(const char *name)
126 {
127         int i;
128
129         for (i = 0; i < PSTORE_TYPE_MAX; i++) {
130                 if (!strcmp(pstore_type_names[i], name))
131                         return i;
132         }
133
134         return PSTORE_TYPE_MAX;
135 }
136 EXPORT_SYMBOL_GPL(pstore_name_to_type);
137
138 static const char *get_reason_str(enum kmsg_dump_reason reason)
139 {
140         switch (reason) {
141         case KMSG_DUMP_PANIC:
142                 return "Panic";
143         case KMSG_DUMP_OOPS:
144                 return "Oops";
145         case KMSG_DUMP_EMERG:
146                 return "Emergency";
147         case KMSG_DUMP_SHUTDOWN:
148                 return "Shutdown";
149         default:
150                 return "Unknown";
151         }
152 }
153
154 static void pstore_timer_kick(void)
155 {
156         if (pstore_update_ms < 0)
157                 return;
158
159         mod_timer(&pstore_timer, jiffies + msecs_to_jiffies(pstore_update_ms));
160 }
161
162 /*
163  * Should pstore_dump() wait for a concurrent pstore_dump()? If
164  * not, the current pstore_dump() will report a failure to dump
165  * and return.
166  */
167 static bool pstore_cannot_wait(enum kmsg_dump_reason reason)
168 {
169         /* In NMI path, pstore shouldn't block regardless of reason. */
170         if (in_nmi())
171                 return true;
172
173         switch (reason) {
174         /* In panic case, other cpus are stopped by smp_send_stop(). */
175         case KMSG_DUMP_PANIC:
176         /* Emergency restart shouldn't be blocked. */
177         case KMSG_DUMP_EMERG:
178                 return true;
179         default:
180                 return false;
181         }
182 }
183
184 #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
185 static int zbufsize_deflate(size_t size)
186 {
187         size_t cmpr;
188
189         switch (size) {
190         /* buffer range for efivars */
191         case 1000 ... 2000:
192                 cmpr = 56;
193                 break;
194         case 2001 ... 3000:
195                 cmpr = 54;
196                 break;
197         case 3001 ... 3999:
198                 cmpr = 52;
199                 break;
200         /* buffer range for nvram, erst */
201         case 4000 ... 10000:
202                 cmpr = 45;
203                 break;
204         default:
205                 cmpr = 60;
206                 break;
207         }
208
209         return (size * 100) / cmpr;
210 }
211 #endif
212
213 #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
214 static int zbufsize_lzo(size_t size)
215 {
216         return lzo1x_worst_compress(size);
217 }
218 #endif
219
220 #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
221 static int zbufsize_lz4(size_t size)
222 {
223         return LZ4_compressBound(size);
224 }
225 #endif
226
227 #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
228 static int zbufsize_842(size_t size)
229 {
230         return size;
231 }
232 #endif
233
234 #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
235 static int zbufsize_zstd(size_t size)
236 {
237         return ZSTD_compressBound(size);
238 }
239 #endif
240
241 static const struct pstore_zbackend *zbackend __ro_after_init;
242
243 static const struct pstore_zbackend zbackends[] = {
244 #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
245         {
246                 .zbufsize       = zbufsize_deflate,
247                 .name           = "deflate",
248         },
249 #endif
250 #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
251         {
252                 .zbufsize       = zbufsize_lzo,
253                 .name           = "lzo",
254         },
255 #endif
256 #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS)
257         {
258                 .zbufsize       = zbufsize_lz4,
259                 .name           = "lz4",
260         },
261 #endif
262 #if IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
263         {
264                 .zbufsize       = zbufsize_lz4,
265                 .name           = "lz4hc",
266         },
267 #endif
268 #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
269         {
270                 .zbufsize       = zbufsize_842,
271                 .name           = "842",
272         },
273 #endif
274 #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
275         {
276                 .zbufsize       = zbufsize_zstd,
277                 .name           = "zstd",
278         },
279 #endif
280         { }
281 };
282
283 static int pstore_compress(const void *in, void *out,
284                            unsigned int inlen, unsigned int outlen)
285 {
286         int ret;
287
288         ret = crypto_comp_compress(tfm, in, inlen, out, &outlen);
289         if (ret) {
290                 pr_err("crypto_comp_compress failed, ret = %d!\n", ret);
291                 return ret;
292         }
293
294         return outlen;
295 }
296
297 static void allocate_buf_for_compression(void)
298 {
299         struct crypto_comp *ctx;
300         int size;
301         char *buf;
302
303         /* Skip if not built-in or compression backend not selected yet. */
304         if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !zbackend)
305                 return;
306
307         /* Skip if no pstore backend yet or compression init already done. */
308         if (!psinfo || tfm)
309                 return;
310
311         if (!crypto_has_comp(zbackend->name, 0, 0)) {
312                 pr_err("Unknown compression: %s\n", zbackend->name);
313                 return;
314         }
315
316         size = zbackend->zbufsize(psinfo->bufsize);
317         if (size <= 0) {
318                 pr_err("Invalid compression size for %s: %d\n",
319                        zbackend->name, size);
320                 return;
321         }
322
323         buf = kmalloc(size, GFP_KERNEL);
324         if (!buf) {
325                 pr_err("Failed %d byte compression buffer allocation for: %s\n",
326                        size, zbackend->name);
327                 return;
328         }
329
330         ctx = crypto_alloc_comp(zbackend->name, 0, 0);
331         if (IS_ERR_OR_NULL(ctx)) {
332                 kfree(buf);
333                 pr_err("crypto_alloc_comp('%s') failed: %ld\n", zbackend->name,
334                        PTR_ERR(ctx));
335                 return;
336         }
337
338         /* A non-NULL big_oops_buf indicates compression is available. */
339         tfm = ctx;
340         big_oops_buf_sz = size;
341         big_oops_buf = buf;
342
343         pr_info("Using crash dump compression: %s\n", zbackend->name);
344 }
345
346 static void free_buf_for_compression(void)
347 {
348         if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && tfm) {
349                 crypto_free_comp(tfm);
350                 tfm = NULL;
351         }
352         kfree(big_oops_buf);
353         big_oops_buf = NULL;
354         big_oops_buf_sz = 0;
355 }
356
357 /*
358  * Called when compression fails, since the printk buffer
359  * would be fetched for compression calling it again when
360  * compression fails would have moved the iterator of
361  * printk buffer which results in fetching old contents.
362  * Copy the recent messages from big_oops_buf to psinfo->buf
363  */
364 static size_t copy_kmsg_to_buffer(int hsize, size_t len)
365 {
366         size_t total_len;
367         size_t diff;
368
369         total_len = hsize + len;
370
371         if (total_len > psinfo->bufsize) {
372                 diff = total_len - psinfo->bufsize + hsize;
373                 memcpy(psinfo->buf, big_oops_buf, hsize);
374                 memcpy(psinfo->buf + hsize, big_oops_buf + diff,
375                                         psinfo->bufsize - hsize);
376                 total_len = psinfo->bufsize;
377         } else
378                 memcpy(psinfo->buf, big_oops_buf, total_len);
379
380         return total_len;
381 }
382
383 void pstore_record_init(struct pstore_record *record,
384                         struct pstore_info *psinfo)
385 {
386         memset(record, 0, sizeof(*record));
387
388         record->psi = psinfo;
389
390         /* Report zeroed timestamp if called before timekeeping has resumed. */
391         record->time = ns_to_timespec64(ktime_get_real_fast_ns());
392 }
393
394 /*
395  * callback from kmsg_dump. Save as much as we can (up to kmsg_bytes) from the
396  * end of the buffer.
397  */
398 static void pstore_dump(struct kmsg_dumper *dumper,
399                         enum kmsg_dump_reason reason)
400 {
401         unsigned long   total = 0;
402         const char      *why;
403         unsigned int    part = 1;
404         int             ret;
405
406         why = get_reason_str(reason);
407
408         if (down_trylock(&psinfo->buf_lock)) {
409                 /* Failed to acquire lock: give up if we cannot wait. */
410                 if (pstore_cannot_wait(reason)) {
411                         pr_err("dump skipped in %s path: may corrupt error record\n",
412                                 in_nmi() ? "NMI" : why);
413                         return;
414                 }
415                 if (down_interruptible(&psinfo->buf_lock)) {
416                         pr_err("could not grab semaphore?!\n");
417                         return;
418                 }
419         }
420
421         oopscount++;
422         while (total < kmsg_bytes) {
423                 char *dst;
424                 size_t dst_size;
425                 int header_size;
426                 int zipped_len = -1;
427                 size_t dump_size;
428                 struct pstore_record record;
429
430                 pstore_record_init(&record, psinfo);
431                 record.type = PSTORE_TYPE_DMESG;
432                 record.count = oopscount;
433                 record.reason = reason;
434                 record.part = part;
435                 record.buf = psinfo->buf;
436
437                 if (big_oops_buf) {
438                         dst = big_oops_buf;
439                         dst_size = big_oops_buf_sz;
440                 } else {
441                         dst = psinfo->buf;
442                         dst_size = psinfo->bufsize;
443                 }
444
445                 /* Write dump header. */
446                 header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why,
447                                  oopscount, part);
448                 dst_size -= header_size;
449
450                 /* Write dump contents. */
451                 if (!kmsg_dump_get_buffer(dumper, true, dst + header_size,
452                                           dst_size, &dump_size))
453                         break;
454
455                 if (big_oops_buf) {
456                         zipped_len = pstore_compress(dst, psinfo->buf,
457                                                 header_size + dump_size,
458                                                 psinfo->bufsize);
459
460                         if (zipped_len > 0) {
461                                 record.compressed = true;
462                                 record.size = zipped_len;
463                         } else {
464                                 record.size = copy_kmsg_to_buffer(header_size,
465                                                                   dump_size);
466                         }
467                 } else {
468                         record.size = header_size + dump_size;
469                 }
470
471                 ret = psinfo->write(&record);
472                 if (ret == 0 && reason == KMSG_DUMP_OOPS) {
473                         pstore_new_entry = 1;
474                         pstore_timer_kick();
475                 }
476
477                 total += record.size;
478                 part++;
479         }
480
481         up(&psinfo->buf_lock);
482 }
483
484 static struct kmsg_dumper pstore_dumper = {
485         .dump = pstore_dump,
486 };
487
488 /*
489  * Register with kmsg_dump to save last part of console log on panic.
490  */
491 static void pstore_register_kmsg(void)
492 {
493         kmsg_dump_register(&pstore_dumper);
494 }
495
496 static void pstore_unregister_kmsg(void)
497 {
498         kmsg_dump_unregister(&pstore_dumper);
499 }
500
501 #ifdef CONFIG_PSTORE_CONSOLE
502 static void pstore_console_write(struct console *con, const char *s, unsigned c)
503 {
504         struct pstore_record record;
505
506         if (!c)
507                 return;
508
509         pstore_record_init(&record, psinfo);
510         record.type = PSTORE_TYPE_CONSOLE;
511
512         record.buf = (char *)s;
513         record.size = c;
514         psinfo->write(&record);
515 }
516
517 static struct console pstore_console = {
518         .write  = pstore_console_write,
519         .index  = -1,
520 };
521
522 static void pstore_register_console(void)
523 {
524         /* Show which backend is going to get console writes. */
525         strscpy(pstore_console.name, psinfo->name,
526                 sizeof(pstore_console.name));
527         /*
528          * Always initialize flags here since prior unregister_console()
529          * calls may have changed settings (specifically CON_ENABLED).
530          */
531         pstore_console.flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME;
532         register_console(&pstore_console);
533 }
534
535 static void pstore_unregister_console(void)
536 {
537         unregister_console(&pstore_console);
538 }
539 #else
540 static void pstore_register_console(void) {}
541 static void pstore_unregister_console(void) {}
542 #endif
543
544 static int pstore_write_user_compat(struct pstore_record *record,
545                                     const char __user *buf)
546 {
547         int ret = 0;
548
549         if (record->buf)
550                 return -EINVAL;
551
552         record->buf = memdup_user(buf, record->size);
553         if (IS_ERR(record->buf)) {
554                 ret = PTR_ERR(record->buf);
555                 goto out;
556         }
557
558         ret = record->psi->write(record);
559
560         kfree(record->buf);
561 out:
562         record->buf = NULL;
563
564         return unlikely(ret < 0) ? ret : record->size;
565 }
566
567 /*
568  * platform specific persistent storage driver registers with
569  * us here. If pstore is already mounted, call the platform
570  * read function right away to populate the file system. If not
571  * then the pstore mount code will call us later to fill out
572  * the file system.
573  */
574 int pstore_register(struct pstore_info *psi)
575 {
576         if (backend && strcmp(backend, psi->name)) {
577                 pr_warn("ignoring unexpected backend '%s'\n", psi->name);
578                 return -EPERM;
579         }
580
581         /* Sanity check flags. */
582         if (!psi->flags) {
583                 pr_warn("backend '%s' must support at least one frontend\n",
584                         psi->name);
585                 return -EINVAL;
586         }
587
588         /* Check for required functions. */
589         if (!psi->read || !psi->write) {
590                 pr_warn("backend '%s' must implement read() and write()\n",
591                         psi->name);
592                 return -EINVAL;
593         }
594
595         mutex_lock(&psinfo_lock);
596         if (psinfo) {
597                 pr_warn("backend '%s' already loaded: ignoring '%s'\n",
598                         psinfo->name, psi->name);
599                 mutex_unlock(&psinfo_lock);
600                 return -EBUSY;
601         }
602
603         if (!psi->write_user)
604                 psi->write_user = pstore_write_user_compat;
605         psinfo = psi;
606         mutex_init(&psinfo->read_mutex);
607         sema_init(&psinfo->buf_lock, 1);
608
609         if (psi->flags & PSTORE_FLAGS_DMESG)
610                 allocate_buf_for_compression();
611
612         pstore_get_records(0);
613
614         if (psi->flags & PSTORE_FLAGS_DMESG)
615                 pstore_register_kmsg();
616         if (psi->flags & PSTORE_FLAGS_CONSOLE)
617                 pstore_register_console();
618         if (psi->flags & PSTORE_FLAGS_FTRACE)
619                 pstore_register_ftrace();
620         if (psi->flags & PSTORE_FLAGS_PMSG)
621                 pstore_register_pmsg();
622
623         /* Start watching for new records, if desired. */
624         pstore_timer_kick();
625
626         /*
627          * Update the module parameter backend, so it is visible
628          * through /sys/module/pstore/parameters/backend
629          */
630         backend = kstrdup(psi->name, GFP_KERNEL);
631
632         pr_info("Registered %s as persistent store backend\n", psi->name);
633
634         mutex_unlock(&psinfo_lock);
635         return 0;
636 }
637 EXPORT_SYMBOL_GPL(pstore_register);
638
639 void pstore_unregister(struct pstore_info *psi)
640 {
641         /* It's okay to unregister nothing. */
642         if (!psi)
643                 return;
644
645         mutex_lock(&psinfo_lock);
646
647         /* Only one backend can be registered at a time. */
648         if (WARN_ON(psi != psinfo)) {
649                 mutex_unlock(&psinfo_lock);
650                 return;
651         }
652
653         /* Unregister all callbacks. */
654         if (psi->flags & PSTORE_FLAGS_PMSG)
655                 pstore_unregister_pmsg();
656         if (psi->flags & PSTORE_FLAGS_FTRACE)
657                 pstore_unregister_ftrace();
658         if (psi->flags & PSTORE_FLAGS_CONSOLE)
659                 pstore_unregister_console();
660         if (psi->flags & PSTORE_FLAGS_DMESG)
661                 pstore_unregister_kmsg();
662
663         /* Stop timer and make sure all work has finished. */
664         del_timer_sync(&pstore_timer);
665         flush_work(&pstore_work);
666
667         /* Remove all backend records from filesystem tree. */
668         pstore_put_backend_records(psi);
669
670         free_buf_for_compression();
671
672         psinfo = NULL;
673         kfree(backend);
674         backend = NULL;
675         mutex_unlock(&psinfo_lock);
676 }
677 EXPORT_SYMBOL_GPL(pstore_unregister);
678
679 static void decompress_record(struct pstore_record *record)
680 {
681         int ret;
682         int unzipped_len;
683         char *unzipped, *workspace;
684
685         if (!record->compressed)
686                 return;
687
688         /* Only PSTORE_TYPE_DMESG support compression. */
689         if (record->type != PSTORE_TYPE_DMESG) {
690                 pr_warn("ignored compressed record type %d\n", record->type);
691                 return;
692         }
693
694         /* Missing compression buffer means compression was not initialized. */
695         if (!big_oops_buf) {
696                 pr_warn("no decompression method initialized!\n");
697                 return;
698         }
699
700         /* Allocate enough space to hold max decompression and ECC. */
701         unzipped_len = big_oops_buf_sz;
702         workspace = kmalloc(unzipped_len + record->ecc_notice_size,
703                             GFP_KERNEL);
704         if (!workspace)
705                 return;
706
707         /* After decompression "unzipped_len" is almost certainly smaller. */
708         ret = crypto_comp_decompress(tfm, record->buf, record->size,
709                                           workspace, &unzipped_len);
710         if (ret) {
711                 pr_err("crypto_comp_decompress failed, ret = %d!\n", ret);
712                 kfree(workspace);
713                 return;
714         }
715
716         /* Append ECC notice to decompressed buffer. */
717         memcpy(workspace + unzipped_len, record->buf + record->size,
718                record->ecc_notice_size);
719
720         /* Copy decompressed contents into an minimum-sized allocation. */
721         unzipped = kmemdup(workspace, unzipped_len + record->ecc_notice_size,
722                            GFP_KERNEL);
723         kfree(workspace);
724         if (!unzipped)
725                 return;
726
727         /* Swap out compressed contents with decompressed contents. */
728         kfree(record->buf);
729         record->buf = unzipped;
730         record->size = unzipped_len;
731         record->compressed = false;
732 }
733
734 /*
735  * Read all the records from one persistent store backend. Create
736  * files in our filesystem.  Don't warn about -EEXIST errors
737  * when we are re-scanning the backing store looking to add new
738  * error records.
739  */
740 void pstore_get_backend_records(struct pstore_info *psi,
741                                 struct dentry *root, int quiet)
742 {
743         int failed = 0;
744         unsigned int stop_loop = 65536;
745
746         if (!psi || !root)
747                 return;
748
749         mutex_lock(&psi->read_mutex);
750         if (psi->open && psi->open(psi))
751                 goto out;
752
753         /*
754          * Backend callback read() allocates record.buf. decompress_record()
755          * may reallocate record.buf. On success, pstore_mkfile() will keep
756          * the record.buf, so free it only on failure.
757          */
758         for (; stop_loop; stop_loop--) {
759                 struct pstore_record *record;
760                 int rc;
761
762                 record = kzalloc(sizeof(*record), GFP_KERNEL);
763                 if (!record) {
764                         pr_err("out of memory creating record\n");
765                         break;
766                 }
767                 pstore_record_init(record, psi);
768
769                 record->size = psi->read(record);
770
771                 /* No more records left in backend? */
772                 if (record->size <= 0) {
773                         kfree(record);
774                         break;
775                 }
776
777                 decompress_record(record);
778                 rc = pstore_mkfile(root, record);
779                 if (rc) {
780                         /* pstore_mkfile() did not take record, so free it. */
781                         kfree(record->buf);
782                         kfree(record);
783                         if (rc != -EEXIST || !quiet)
784                                 failed++;
785                 }
786         }
787         if (psi->close)
788                 psi->close(psi);
789 out:
790         mutex_unlock(&psi->read_mutex);
791
792         if (failed)
793                 pr_warn("failed to create %d record(s) from '%s'\n",
794                         failed, psi->name);
795         if (!stop_loop)
796                 pr_err("looping? Too many records seen from '%s'\n",
797                         psi->name);
798 }
799
800 static void pstore_dowork(struct work_struct *work)
801 {
802         pstore_get_records(1);
803 }
804
805 static void pstore_timefunc(struct timer_list *unused)
806 {
807         if (pstore_new_entry) {
808                 pstore_new_entry = 0;
809                 schedule_work(&pstore_work);
810         }
811
812         pstore_timer_kick();
813 }
814
815 static void __init pstore_choose_compression(void)
816 {
817         const struct pstore_zbackend *step;
818
819         if (!compress)
820                 return;
821
822         for (step = zbackends; step->name; step++) {
823                 if (!strcmp(compress, step->name)) {
824                         zbackend = step;
825                         return;
826                 }
827         }
828 }
829
830 static int __init pstore_init(void)
831 {
832         int ret;
833
834         pstore_choose_compression();
835
836         /*
837          * Check if any pstore backends registered earlier but did not
838          * initialize compression because crypto was not ready. If so,
839          * initialize compression now.
840          */
841         allocate_buf_for_compression();
842
843         ret = pstore_init_fs();
844         if (ret)
845                 free_buf_for_compression();
846
847         return ret;
848 }
849 late_initcall(pstore_init);
850
851 static void __exit pstore_exit(void)
852 {
853         pstore_exit_fs();
854 }
855 module_exit(pstore_exit)
856
857 MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
858 MODULE_LICENSE("GPL");